在添加数据时打开XML不保存C#

我从以下链接做了一个完整的副本: https : //msdn.microsoft.com/en-us/library/dd452407(v=office.12).aspx

从模板复制工作正常, FixChartData()方法正常工作。 但是,输出文件不包含任何数据。 我确实看到contentRow包含通过debugging器的数据,但是当我打开文件时,excel表格没有数据。

非常令人沮丧。 任何帮助,将不胜感激。

 public void Create() { string appPath = System.IO.Path.GetDirectoryName(System.IO.Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory())); string templateFile = appPath + @"\Templates\ChartExample.xlsx"; string saveFile = appPath + @"\Documents\Generated.xlsx"; File.Copy(templateFile, saveFile, true); //open copied template. using(SpreadsheetDocument myWorkbook = SpreadsheetDocument.Open(saveFile, true)) { //this is the workbook contains all the worksheets WorkbookPart workbookPart = myWorkbook.WorkbookPart; //we know that the first worksheet contains the data for the graph WorksheetPart worksheetPart = workbookPart.WorksheetParts.First(); //getting the first worksheet //the shhet data contains the information we are looking to alter SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>(); int index = 2;//Row the data for the graph starts on //var qry = from t in db.SEL_SE_DEATHS() FudgeData fudge = new FudgeData(); var qry = fudge.Fudged(); foreach(var item in qry) { int Year = item.EventYear; int PSQ = item.PSQReviewable; int death = item.Deaths; Row contentRow = CreateContentRow(index, Year, PSQ, death); index++; //contentRow.RowIndex = (UInt32)index; sheetData.AppendChild(contentRow); } //(<x:cr="A2" xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><x:v>2014</x:v></x:c><x:cr="B2" xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><x:v>21</x:v></x:c><x:cr="C2" xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><x:v>4</x:v></x:c>) FixChartData(workbookPart, index); worksheetPart.Worksheet.Save(); myWorkbook.Close(); myWorkbook.Dispose(); } } string[] headerColumns = new string[] { "A", "B", "C" }; //the columns being accessed public Row CreateContentRow(int index, int year, int pSQ, int death) { Row r = new Row(); r.RowIndex = (UInt32)index; //skipping the text add function //we are createing a cell for each column (headerColumns), //for each cell we are adding a value. //we then append the value to the cell and append the cell to the row - wich is returned. for(int i =0; i <headerColumns.Length; i++) { Cell c = new Cell(); c.CellReference = headerColumns[i] + index; CellValue v = new CellValue(); if(i == 0) { v.Text = year.ToString(); }else if(i == 1) { v.Text = pSQ.ToString(); }else if(i == 2) { v.Text = death.ToString(); } c.AppendChild(v); r.AppendChild(c); } return r; } //Method for when the datatype is text based public Cell CreateTextCell(string header, string text, int index) { //Create a new inline string cell. Cell c = new Cell(); c.DataType = CellValues.InlineString; c.CellReference = header + index; //Add text to the text cell. InlineString inlineString = new InlineString(); Text t = new Text(); t.Text = text; inlineString.AppendChild(t); c.AppendChild(inlineString); return c; } //fix the chart Data Regions public void FixChartData(WorkbookPart workbookPart, int totalCount) { var wsparts = workbookPart.WorksheetParts.ToArray(); foreach(WorksheetPart wsp in wsparts) { if(wsp.DrawingsPart != null) { ChartPart chartPart = wsp.DrawingsPart.ChartParts.First(); ////change the ranges to accomodate the newly inserted data. foreach (DocumentFormat.OpenXml.Drawing.Charts.Formula formula in chartPart.ChartSpace.Descendants<DocumentFormat.OpenXml.Drawing.Charts.Formula>()) { if (formula.Text.Contains("$2")) { string s = formula.Text.Split('$')[1]; formula.Text += ":$" + s + "$" + totalCount; } } chartPart.ChartSpace.Save(); } } //ChartPart chartPart = workbookPart.ChartsheetParts.First().DrawingsPart.ChartParts.First(); ////change the ranges to accomodate the newly inserted data. //foreach(DocumentFormat.OpenXml.Drawing.Charts.Formula formula in chartPart.ChartSpace.Descendants<DocumentFormat.OpenXml.Drawing.Charts.Formula>()) //{ // if (formula.Text.Contains("$2")) // { // string s = formula.Text.Split('$')[1]; // formula.Text += ":$" + s + "$" + totalCount; // } //} //chartPart.ChartSpace.Save(); } 

大卫,我让你的代码工作正常。 这里是我的控制台应用程序的链接。 。 我把它上传到Github,并稍作修改。 我做了2个改变:

1)我无法从您提供的链接下载样本。 于是我用Excel2016创build了一个空白的空白电子表格,并将其保存在该目录中。

2)Fu​​dge数据丢失,所以我通过自我模拟对象生成了一些样本数据。

电子表格从模板中复制并且您的代码使用fudge数据填充它。 以下是最终的结果:

在这里输入图像说明

下载后,您需要创build一个TemplateDocument子目录。 然后将我的ChartExample.xslx文件放在Template目录中并运行。