how to copy ExcelWorkSheet from template with new CodeModule

 ̄綄美尐妖づ 提交于 2019-12-01 20:09:13

It seems to be a bug. I have created an issue in the issue center of the epplus project. It has been marked as resolved

https://epplus.codeplex.com/workitem/15095

I haven't tested the new version yet. I'll give an update when that's done.

Update In the latest sources this issue is resolved.

I think you are onto something. I see the same thing - using EPP 3.1. The whole vba thing in the xlsm file format are different since they are not XML-based, they are bin files containing complied files. So, based on what I saw, it is a flaw in EPPlus and worth submitting to them. Here is a unit test they can drop right into their Solution (that is what I do when I am messing around with EPP):

[TestMethod]
public void VBAWorksheetCopyTest()
{
    var sb = new StringBuilder();
    sb.AppendLine("Private Sub Test()");
    sb.AppendLine("    Range(\"G10\").Value = \"TEST\"");
    sb.AppendLine("End Sub");

    var existingFile = new FileInfo(@"c:\temp\temp1.xlsm");
    if (existingFile.Exists)
        existingFile.Delete();

    using (var package = new ExcelPackage(existingFile))
    {
        var workbook = package.Workbook;
        workbook.CreateVBAProject();

        var worksheet = workbook.Worksheets.Add("templateSheet");

        //Module saved in the workSHEET
        worksheet.CodeModule.Code = sb.ToString();
        worksheet.CodeModule.Name = "templateSheet";

        worksheet.Cells["A1"].Value = "Col1";
        worksheet.Cells["A2"].Value = "sdf";
        worksheet.Cells["A3"].Value = "wer";

        package.Save();
    }

    //Open temp1.xlsm and copy the sheet
    using (var package = new ExcelPackage(existingFile))
    {
        var workbook = package.Workbook;
        var templateSheet = workbook.Worksheets["templateSheet"];
        var someName = workbook.Worksheets.Add("someName", templateSheet);

        //VBA code does seem to copy but dose NOT match what is seen in excel
        Assert.IsTrue(templateSheet.CodeModule.Code.Length > 0);
        Assert.IsTrue(someName.CodeModule.Code.Length > 0);

        package.Save();
    }

    //Open temp1 and try to name the modules
    using (var package = new ExcelPackage(existingFile))
    {
        var workbook = package.Workbook;
        var templateSheet = workbook.Worksheets["templateSheet"];
        var someName = workbook.Worksheets["someName"];

        //Give it a name otherwise Excel autonames it 'newsheet1'
        someName.CodeModule.Name = "someName"; //BUT will cause both CodeModule objects to go null

        //These will now fail becuase codemodule is now null for both
        Assert.IsTrue(templateSheet.CodeModule.Code.Length > 0);
        Assert.IsTrue(someName.CodeModule.Code.Length > 0);
    }
}

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!