Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append to excel file with ClosedXML

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!

like image 291
Codesmell Avatar asked Mar 30 '14 17:03

Codesmell


People also ask

Does ClosedXML support XLS?

ClosedXML is a . NET library for reading, manipulating and writing Excel 2007+ (. xlsx, . xlsm) files.

Does ClosedXML require Excel?

ClosedXML allows you to create Excel files without the Excel application.

What is the difference between OpenXml and ClosedXML?

Macros – ClosedXml doesn't support macros as its base library OpenXml also doesn't support it. Embedding – We cannot embed any file into Excel using ClosedXml, no APIs built for that, so some features of OpenXml still need to be implemented. Charts – No functionality related to charting is present.


1 Answers

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.

like image 59
Raidri Avatar answered Sep 29 '22 02:09

Raidri