how to copy ExcelWorkSheet from template with new CodeModule

后端 未结 2 699
暖寄归人
暖寄归人 2021-01-19 11:49

I am having an issue with copying a ExcelWorkSheet and the corresponding CodeModule of the copied sheet. Let me explain:

Scenario:<

相关标签:
2条回答
  • 2021-01-19 12:34

    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.

    0 讨论(0)
  • 2021-01-19 12:48

    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);
        }
    }
    

    enter image description here

    0 讨论(0)
提交回复
热议问题