c# openxml 删除xlsx、xls的外链

要删除一个 Excel 文件(.xlsx)中的外部链接(external links),你可以使用 OpenXML SDK。外部链接通常包含在 `externalReferences` 元素中。以下是一个简单的 C# 代码示例,演示如何使用 OpenXML SDK 删除外部链接:

cs 复制代码
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;

class Program
{
    static void Main()
    {
        string filePath = "your_file_path.xlsx";

        // 备份文件,以防需要还原
        string backupFilePath = "backup_file_path.xlsx";
        System.IO.File.Copy(filePath, backupFilePath, true);

        // 删除外部链接
        RemoveExternalLinks(filePath);

        Console.WriteLine("External links removed successfully.");
    }

    static void RemoveExternalLinks(string filePath)
    {
        using (SpreadsheetDocument spreadsheetDoc = SpreadsheetDocument.Open(filePath, true))
        {
            WorkbookPart workbookPart = spreadsheetDoc.WorkbookPart;

            // 删除 externalReferences 元素
            ExternalReferences externalReferences = workbookPart.Workbook.Descendants<ExternalReferences>().FirstOrDefault();
            if (externalReferences != null)
            {
                externalReferences.Remove();
            }

            // 保存更改
            workbookPart.Workbook.Save();
        }
    }
}

请确保将文件路径替换为你实际的文件路径。此代码将备份原始文件,并在原始文件上删除外部链接。如果需要还原,可以使用备份文件。记得在使用此代码前备份你的数据,以免不小心删除了重要的信息。

对于删除 Excel 文件(.xls)中的外部链接,你需要使用 NPOI(一个用于处理 Office 文件的开源库)或其他类似的库,因为 OpenXML SDK 主要用于处理 Office 2007及更高版本(.xlsx)的文件。

以下是使用 NPOI 的示例代码,用于删除 .xls 文件中的外部链接:

cs 复制代码
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System;
using System.IO;

class Program
{
    static void Main()
    {
        string filePath = "your_file_path.xls";

        // 备份文件,以防需要还原
        string backupFilePath = "backup_file_path.xls";
        File.Copy(filePath, backupFilePath, true);

        // 删除外部链接
        RemoveExternalLinks(filePath);

        Console.WriteLine("External links removed successfully.");
    }

    static void RemoveExternalLinks(string filePath)
    {
        using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite))
        {
            HSSFWorkbook workbook = new HSSFWorkbook(fileStream);

            // 删除外部链接
            workbook.ExternalLinksTable.RemoveAggregate();

            // 保存更改
            workbook.Write(fileStream);
        }
    }
}

请确保将文件路径替换为你实际的文件路径。这段代码将备份原始文件并在原始文件上删除外部链接。如果需要还原,可以使用备份文件。同样,请在使用此代码之前备份你的数据,以免不小心删除了重要的信息。

相关推荐
开开心心_Every1 小时前
Word转PDF工具,免费生成图片型文档
网络·笔记·pdf·word·powerpoint·excel·azure
云中飞鸿1 小时前
wpf 类图
c#
世洋Blog2 小时前
SiYangUnityEventSystem,一个Unity中的事件系统
观察者模式·unity·c#·游戏引擎·事件系统
切糕师学AI2 小时前
如何用 VS Code + C# Dev Kit 创建类库项目并在主项目中引用它?
开发语言·c#
William_cl3 小时前
【CSDN 专栏】C# ASP.NET控制器过滤器:自定义 ActionFilterAttribute 实战(避坑 + 图解)
c#·asp.net·状态模式
William_cl3 小时前
【CSDN 专栏】C# ASP.NET Razor 视图引擎实战:.cshtml 从入门到避坑(图解 + 案例)
开发语言·c#·asp.net
isyoungboy3 小时前
c++使用win新api替代DirectShow驱动uvc摄像头,可改c#驱动
开发语言·c++·c#
技术支持者python,php4 小时前
USB摄像头采集数据
人工智能·c#
c#上位机15 小时前
halcon刚性变换(平移+旋转)——vector_to_rigid
图像处理·人工智能·计算机视觉·c#·halcon
Miss_SQ15 小时前
Webgl打包后删除StreamingAssets文件夹下多余资源
unity·c#·webgl