Append to excel file with ClosedXML

你。 提交于 2019-12-09 05:28:04

问题


I need to append new data to existing excel file created with ClosedXML.

How can I append to an excel file using ClosedXML? How can I get the row number of the last record and append to that or is there something other?

Thanks!


回答1:


Open the existing workbook and then use the Last*Used methods:

XLWorkbook Workbook = new XLWorkbook(@"C:\existing_excel_file.xlsx");
IXLWorksheet Worksheet = Workbook.Worksheet("Name or index of the sheet to use");

int NumberOfLastRow = Worksheet.LastRowUsed().RowNumber();
IXLCell CellForNewData = Worksheet.Cell(NumberOfLastRow + 1, 1);

And then use one of the following depending on your data:

CellForNewData.SetValue(data);
CellForNewData.InsertData(data2);

CellForNewData.InsertTable(datatable);

For more information see the documentation under Inserting Data or Inserting Tables.



来源:https://stackoverflow.com/questions/22747360/append-to-excel-file-with-closedxml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!