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

C# 在添加数据C时打开不保存的XML#,c#,excel,sdk,openxml,C#,Excel,Sdk,Openxml,我从以下链接复制了一份完整的副本: 从模板复制工作正常,FixChartData()方法工作正常。但是,输出文件不包含任何数据。我确实通过调试器看到contentRow包含数据,但是当我打开文件时,excel工作表中没有数据 非常令人沮丧。任何帮助都将不胜感激 public void Create() { string appPath = System.IO.Path.GetDirectoryName(System.IO.Path.GetDirectoryName(Sys

我从以下链接复制了一份完整的副本:

从模板复制工作正常,
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();
    }
public void Create()
{
字符串appPath=System.IO.Path.GetDirectoryName(System.IO.Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory());
字符串templateFile=appPath+@“\Templates\ChartExample.xlsx”;
字符串saveFile=appPath+@“\Documents\Generated.xlsx”;
Copy(templateFile,saveFile,true);
//打开复制的模板。
使用(SpreadsheetDocument myWorkbook=SpreadsheetDocument.Open(保存文件,true))
{
//这是包含所有工作表的工作簿
WorkbookPart WorkbookPart=My工作簿.WorkbookPart;
//我们知道第一个工作表包含图形的数据
WorksheetPart WorksheetPart=workbookPart.WorksheetParts.First();//获取第一张工作表
//shhet数据包含我们希望更改的信息
SheetData SheetData=worksheetPart.Worksheet.GetFirstChild();
int index=2;//图形的数据从
//var qry=从t开始,单位为db.SEL_SE_death()
FudgeData fudge=新的FudgeData();
var qry=fudge.Fudged();
foreach(qry中的var项目)
{
int Year=item.EventYear;
int PSQ=item.PSQ可查看;
int死亡=项目死亡;
Row contentRow=CreateContentRow(索引、年份、PSQ、死亡);
索引++;
//contentRow.RowIndex=(UInt32)索引;
sheetData.AppendChild(contentRow);
}
//(2014214)
FixChartData(工作簿部件、索引);
worksheetPart.Worksheet.Save();
myWorkbook.Close();
myWorkbook.Dispose();
}
}
字符串[]headerColumns=新字符串[]{“A”、“B”、“C”}//正在访问的列
公共行CreateContentRow(整数索引、整数年、整数pSQ、整数死亡)
{
行r=新行();
r、 行索引=(UInt32)索引;
//跳过文本添加函数
//我们正在为每个列(headerColumns)创建一个单元格,
//我们为每个单元格添加一个值。
//然后我们将值附加到单元格中,并将单元格附加到返回的行中。
对于(inti=0;iDavid,
我让您的代码正常工作。我将其上载到Github,并做了一些小更改。我做了2个更改:

1) 我无法从您提供的链接下载示例。因此,我使用Excel2016创建了一个空白的空电子表格,并将其保存在该目录中

2) 由于缺少伪造数据,所以我通过自嘲对象生成了一些示例数据

电子表格可以很好地从模板复制,并且您的代码会用伪造的数据填充它。以下是最终结果:

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