2017-03-09 53 views
1

當我做從以下鏈接的完整副本:https://msdn.microsoft.com/en-us/library/dd452407(v=office.12).aspx的Open XML不保存數據添加C​​#

從模板複製工作正常,則FixChartData()法正常工作。但是,輸出文件不包含任何數據。我確實看到contentRow包含通過調試器的數據,但是當我打開文件時,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:c r="A2" xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><x:v>2014</x:v></x:c><x:c r="B2" xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><x:v>21</x:v></x:c><x:c r="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(); 
    } 

回答

0

David, 我讓你的代碼正常工作。 Here is a link to my Console Application.。我將其上傳到Github並做了一些小改動。我做了2個更改:

1)我無法從您提供的鏈接下載樣本。所以我用Excel2016創建了一個空白的空白電子表格,並將其保存在該目錄中。

2)Fudge數據丟失,所以我通過自我模擬對象生成了一些示例數據。

電子表格從模板中複製並且您的代碼使用fudge數據填充它。這裏是最後的結果是這樣的:

enter image description here

下載後,您將需要一個TemplateDocument子目錄。然後將我的ChartExample.xslx文件放在Template目錄中並運行。