在C#中使用OpenXML从DTO写入单元格

在C#中使用OpenXML从DTO写入单元格,c#,openxml,C#,Openxml,正如标题所示,以下是DTO: public class PropertyQuery { public string UPRN { get; set; } public DateTime? DateAdded { get; set; } } 然后是OpenXML类,该类使用一个方法创建电子表格并向下写入字段a和B: using System; using System.Collections.Generic; using System.Linq; using System.Te

正如标题所示,以下是DTO:

public class PropertyQuery
{
    public string UPRN { get; set; }

    public DateTime? DateAdded { get; set; }
}
然后是OpenXML类,该类使用一个方法创建电子表格并向下写入字段a和B:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;

public class OpenXML
{

    public static void BuildWorkbook(string filename, List<PropertyQuery> query)
    {
        using (SpreadsheetDocument xl = SpreadsheetDocument.Create(filename, SpreadsheetDocumentType.Workbook))
        {
            WorkbookPart wbp = xl.AddWorkbookPart();
            WorksheetPart wsp = wbp.AddNewPart<WorksheetPart>();
            Workbook wb = new Workbook();
            FileVersion fv = new FileVersion();
            fv.ApplicationName = "Microsoft Office Excel";

            Worksheet ws = new Worksheet();
            SheetData sd = new SheetData();

            Row r1 = new Row();
            Row r2 = new Row();
            Cell c1 = new Cell();
            Cell c2 = new Cell();

            uint count = 0;

            foreach (var i in query)
            {
                r1 = new Row();
                r2 = new Row();
                c1 = new Cell();
                c2 = new Cell();

                string UPRN = i.UPRN.ToString();
                string dateAdded = i.DateAdded.ToString();

                r1.RowIndex = count;
                r2.RowIndex = count;

                c1.CellReference = "A" + r1.RowIndex;
                c2.CellReference = "B" + r2.RowIndex;

                c1.DataType = CellValues.String;
                c2.DataType = CellValues.String;

                c1.CellValue = new CellValue(UPRN);
                c2.CellValue = new CellValue(dateAdded);

                r1.Append(c1);
                r2.Append(c2);

                sd.Append(r1);
                sd.Append(r2);

                count++;
            }

            ws.Append(sd);
            wsp.Worksheet = ws;

            wsp.Worksheet.Save();
            Sheets sheets = new Sheets();
            Sheet sheet = new Sheet();
            sheet.Name = "Data";
            sheet.SheetId = 1;
            sheet.Id = wbp.GetIdOfPart(wsp);
            sheets.Append(sheet);
            wb.Append(fv);
            wb.Append(sheets);

            xl.WorkbookPart.Workbook = wb;
            xl.WorkbookPart.Workbook.Save();
            xl.Close();
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用DocumentFormat.OpenXml;
使用DocumentFormat.OpenXml.Packaging;
使用DocumentFormat.OpenXml.Spreadsheet;
公共类OpenXML
{
公共静态void build工作簿(字符串文件名,列表查询)
{
使用(SpreadsheetDocument xl=SpreadsheetDocument.Create(文件名,SpreadsheetDocumentType.工作簿))
{
WorkbookPart wbp=xl.AddWorkbookPart();
工作表部件wsp=wbp.AddNewPart();
工作簿wb=新工作簿();
FileVersion fv=新的FileVersion();
fv.ApplicationName=“Microsoft Office Excel”;
工作表ws=新工作表();
SheetData sd=新的SheetData();
行r1=新行();
行r2=新行();
单元c1=新单元();
单元c2=新单元();
单位计数=0;
foreach(查询中的变量i)
{
r1=新行();
r2=新行();
c1=新单元();
c2=新单元();
字符串UPRN=i.UPRN.ToString();
字符串dateAdded=i.dateAdded.ToString();
r1.RowIndex=计数;
r2.RowIndex=计数;
c1.CellReference=“A”+r1.RowIndex;
c2.CellReference=“B”+r2.RowIndex;
c1.DataType=CellValues.String;
c2.DataType=CellValues.String;
c1.CellValue=新的CellValue(UPRN);
c2.CellValue=新的CellValue(已添加日期);
r1.附加(c1);
r2.追加(c2);
sd.Append(r1);
sd.Append(r2);
计数++;
}
ws.Append(sd);
wsp.Worksheet=ws;
wsp.Worksheet.Save();
板材=新板材();
板材=新板材();
sheet.Name=“数据”;
sheet.SheetId=1;
sheet.Id=wbp.GetIdOfPart(wsp);
附页(页);
wb.Append(fv);
wb.追加(张);
xl.WorkbookPart.Workbook=wb;
xl.WorkbookPart.Workbook.Save();
xl.Close();
}
}
}
我的代码运行并创建了电子表格,但是当我试图打开它时,会通知我它已损坏。DTO将返回大约600多行


任何建议都将不胜感激。谢谢

你太接近了。您遇到的问题是
行索引必须大于0,但您的行索引从0开始


修正是微不足道的;将
count
的起始值从
0
更改为
1

当时我的大脑几乎没有功能,我看不见,哈哈。非常感谢!哈哈,不用担心@Liviu Adrian。如果它有帮助,请考虑把它标记为(这完全是可选的)。