在EPPlus 4.05中保存.xlsm文件时,出现错误,然后Excel删除/xl/vbaProject.bin部分

时间:2018-01-30 18:20:04

标签: .net-core epplus

尝试以下代码时出错:

       //https://github.com/JanKallman/EPPlus/issues/31
        //Otherwise, I get an EPP error
        Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
        //var excelFile = new FileInfo($"{keywordLocation.FilePath}{keywordLocation.FileName}".Replace("http://", ""));
        var excelFile = new FileInfo(@"wwwroot/SubmissionFiles/23477-1/Financial_Report_0001091596.xlsx");
        using (var source = new ExcelPackage(excelFile))
        {
            //source.Workbook.Worksheets.Add("VBA Sample");
            source.Workbook.CreateVBAProject();
            var sb = new StringBuilder();
            sb.AppendLine("Private Sub Workbook_Open()");
            sb.AppendLine("End Sub");
            source.Workbook.CodeModule.Code = sb.ToString();
            source.SaveAs(new FileInfo(@"wwwroot/SubmissionFiles/23477-1/Financial_Report_0001091596.xlsm"));
        }

这是错误:

Problem in workbook error

当我点击OK时,我收到以下错误:

Excel was able to open the file by repairing or removing the unreadable content

我也尝试过最简单的VBA代码,其中包含一条评论:sb.AppendLine("'VBA Code");

1 个答案:

答案 0 :(得分:0)

当前的EPPlus 4.5.3.2仍然存在相同的问题... 甚至不必更改宏内容

    using (var targetPackage = new ExcelPackage(new FileInfo(filename)))
    {
        var target = targetPackage.Workbook.VbaProject;
...
        targetPackage.Save();
   }

并且结果对于Excel 2016不正确。