You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用SSIS C#脚本为XLSX格式Excel文件设置密码

在SSIS脚本任务中用C#为XLSX文件设置密码

方案一:使用EPPlus实现原生Excel密码保护(推荐)

这是最贴合需求的方案,直接操作Excel文件结构,设置的密码是Excel原生支持的打开/修改密码,用户可直接在Excel中输入密码访问文件。

前置准备

  1. 在SSIS脚本任务的项目里,右键点击「引用」→「添加引用」,浏览添加EPPlus.dll(推荐版本4.5.3.3,适配VS2015的.NET 4.5环境,商业使用需注意对应许可条款)。
  2. 确认脚本任务的目标.NET版本设置为4.5或更高。

可直接使用的C#代码

using System;
using System.IO;
using OfficeOpenXml;

public class ScriptMain
{
    public void Main()
    {
        // 替换为你的目标Excel文件路径
        string excelPath = @"C:\Data\LoadFile.xlsx";
        // 替换为要设置的密码
        string protectPassword = "YourStrongPassword123";

        try
        {
            FileInfo excelFile = new FileInfo(excelPath);
            using (ExcelPackage excelPackage = new ExcelPackage(excelFile))
            {
                // 设置文件打开密码
                excelPackage.Encryption.Password = protectPassword;
                // 可选:单独设置只读密码(打开无需密码,但修改需要)
                // excelPackage.Encryption.SetPassword(protectPassword, readOnlyPassword: "ReadOnlyPass456");
                
                // 保存修改
                excelPackage.Save();
            }

            Dts.TaskResult = (int)ScriptResults.Success;
        }
        catch (Exception ex)
        {
            // 抛出错误到SSIS日志
            Dts.Events.FireError(0, "Excel密码设置失败", ex.Message, string.Empty, 0);
            Dts.TaskResult = (int)ScriptResults.Failure;
        }
    }

    enum ScriptResults
    {
        Success = 0,
        Failure = 1
    }
}

方案二:将XLSX作为Zip包加密(仅简单加密场景)

XLSX本质是压缩包,此方案通过加密Zip包实现文件保护,但Excel不会识别为原生密码,需要先解压才能打开,适合仅需文件级加密的场景。

前置准备

添加DotNetZip.dll的引用(适配.NET 4.5版本)。

可直接使用的C#代码

using System;
using System.IO;
using Ionic.Zip;

public class ScriptMain
{
    public void Main()
    {
        string sourceExcel = @"C:\Data\LoadFile.xlsx";
        string tempEncrypted = @"C:\Data\Temp_Encrypted.xlsx";
        string zipPassword = "YourZipPassword789";

        try
        {
            // 删除已存在的临时加密文件
            if (File.Exists(tempEncrypted)) File.Delete(tempEncrypted);

            // 创建加密Zip包(重命名为XLSX后缀)
            using (ZipFile zip = new ZipFile())
            {
                zip.Password = zipPassword;
                zip.AddFile(sourceExcel, "");
                zip.Save(tempEncrypted);
            }

            // 替换原文件
            File.Delete(sourceExcel);
            File.Move(tempEncrypted, sourceExcel);

            Dts.TaskResult = (int)ScriptResults.Success;
        }
        catch (Exception ex)
        {
            Dts.Events.FireError(0, "Zip加密失败", ex.Message, string.Empty, 0);
            Dts.TaskResult = (int)ScriptResults.Failure;
        }
    }

    enum ScriptResults
    {
        Success = 0,
        Failure = 1
    }
}

注意事项

  • 方案一中,EPPlus 5.x及以上版本采用非商业许可,若用于商业项目需确认许可合规性。
  • 方案二中,DotNetZip的加密仅作用于Zip包,并非Excel原生保护,无法在Excel中直接输入密码打开。

内容的提问来源于stack exchange,提问作者Mouli Thota

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 07:27:29