带有模板的EPPlus不能按预期工作

我目前正在使用EPPlus项目来操纵一些.xlsx文件。 基本的想法是,我必须从给定的模板创build一个新的文件。

但是,当我从模板创build新文件时,表格中的所有计算列都会混乱。

我使用的代码如下:

static void Main(string[] args) { const string templatePath = "template_worksheet.xlsx"; // the path of the template const string resultPath = "result.xlsx"; // the path of our result using (var pck = new ExcelPackage(new FileInfo(resultPath), new FileInfo(templatePath))) // creating a package with the given template, and our result as the new stream { // note that I am not doing any work ... pck.Save(); // savin our work } } 

例如对于一个.xlsx文件(有一个3列的表,最后一个只是其他的总和),程序创build一个.xlsx文件,最后一列的值相同(只对第一个行)在所有行中。

以下图片显示了结果:

在模板中的表表中的结果

现在的问题是:这是怎么回事? 我的代码错了吗? 如何在没有意外行为的情况下完成这项任务?

那绝对是那里的东西。 我能够自己重现它。 它与你创build的表格有关。 如果打开文件并使用“表格工具”选项卡中的“转换为范围”选项将其删除,问题就会消失。

我查看了源代码,并在zip级别上提取了xml文件,并没有看到任何迹象表明它实际上是在搞乱它们 – 似乎是一个直接的副本。

非常奇怪,因为如果我们创build并保存包含EPPlus表的xlsx文件,问题就不存在了。 这工作得很好:

 [TestMethod] public void Template_Copy_Test() { //http://stackoverflow.com/questions/28722945/epplus-with-a-template-is-not-working-as-expected const string templatePath = "c:\\temp\\testtemplate.xlsx"; // the path of the template const string resultPath = "c:\\temp\\result.xlsx"; // the path of our result //Throw in some data var dtdata = new DataTable("tblData"); dtdata.Columns.Add(new DataColumn("Col1", typeof(string))); dtdata.Columns.Add(new DataColumn("Col2", typeof(int))); dtdata.Columns.Add(new DataColumn("Col3", typeof(int))); for (var i = 0; i < 20; i++) { var row = dtdata.NewRow(); row["Col1"] = "String Data " + i; row["Col2"] = i * 10; row["Col3"] = i * 100; dtdata.Rows.Add(row); } var templateFile = new FileInfo(templatePath); if (templateFile.Exists) templateFile.Delete(); using (var pck = new ExcelPackage(templateFile)) { var ws = pck.Workbook.Worksheets.Add("Data"); ws.Cells["A1"].LoadFromDataTable(dtdata, true); for (var i = 2; i <= dtdata.Rows.Count + 1; i++) ws.Cells[i, 4].Formula = String.Format("{0}*{1}", ExcelCellBase.GetAddress(i, 2), ExcelCellBase.GetAddress(i, 3)); ws.Tables.Add(ws.Cells[1, 1, dtdata.Rows.Count + 1, 4], "TestTable"); pck.Save(); } using (var pck = new ExcelPackage(new FileInfo(resultPath), templateFile)) // creating a package with the given template, and our result as the new stream { // note that I am not doing any work ... pck.Save(); // savin our work } } 

但是 …..

如果我们打开testtemplate.xlsx ,请删除表格,保存/closures文件,重新打开,然后重新插入问题所显示的完全相同的表格:

 [TestMethod] public void Template_Copy_Test2() { //http://stackoverflow.com/questions/28722945/epplus-with-a-template-is-not-working-as-expected const string templatePath = "c:\\temp\\testtemplate.xlsx"; // the path of the template const string resultPath = "c:\\temp\\result.xlsx"; // the path of our result var templateFile = new FileInfo(templatePath); using (var pck = new ExcelPackage(new FileInfo(resultPath), templateFile)) // creating a package with the given template, and our result as the new stream { // note that I am not doing any work ... pck.Save(); // savin our work } } 

它的zip拷贝方法必须被埋没,但是我什么都没有跳出来。

但至less你可以看到解决这个问题。

厄尼

尝试使用下面的代码。 此代码采取格式和其他规则,并将其作为xml节点添加到另一个文件。 Ernie在这里描述的非常好导入带有所有条件格式化规则的excel文件来进行epplus解决scheme最好的部分是,您还可以导入格式以及其他规则。 它应该把你接近你所需要的。

 //File with your rules, can be your template var existingFile = new FileInfo(@"c:\temp\temp.xlsx"); //Other file where you want the rules var existingFile2 = new FileInfo(@"c:\temp\temp2.xlsx"); using (var package = new ExcelPackage(existingFile)) using (var package2 = new ExcelPackage(existingFile2)) { //Make sure there are document element for the source var worksheet = package.Workbook.Worksheets.First(); var xdoc = worksheet.WorksheetXml; if (xdoc.DocumentElement == null) return; //Make sure there are document element for the destination var worksheet2 = package2.Workbook.Worksheets.First(); var xdoc2 = worksheet2.WorksheetXml; if (xdoc2.DocumentElement == null) return; //get the extension list node 'extLst' from the ws with the formatting var extensionlistnode = xdoc .DocumentElement .GetElementsByTagName("extLst")[0]; //Create the import node and append it to the end of the xml document var newnode = xdoc2.ImportNode(extensionlistnode, true); xdoc2.LastChild.AppendChild(newnode); package2.Save(); } } 

尝试这个

 var package = new ExcelPackage(excelFile) var excelSheet = package.Workbook.Worksheets[1]; for (var i = 1; i < 5; i++){ excelWorkSheet.InsertRow(i, 1, 1); // Use value of i or whatever is suitable for you } package.Workbook.Calculate(); 

如果最后一个prm设置为1,插入新行将复制以前的行格式及其公式