I found many recommendations here to use ExcelLibrary for editing excell files, but I can't find any documentation anywhere.
http://code.google.com/p/excellibrary/
This is a small C# library made to simplify reading from and writing to Excel workbooks in the Open XML file format (. xlsx). Here's an example to get you started: Workbook workbook = new Workbook(); workbook.
The Excel. Files library can be used to read and write Excel files without the need to start the actual Excel application.
Try this:
var workbook = Workbook.Load("spreadsheet.xls");
var worksheet = workbook.Worksheets[0]; // assuming only 1 worksheet
var cells = worksheet.Cells;
var dataTable = new DataTable("datatable");
// add columns
dataTable.Columns.Add("column1");
dataTable.Columns.Add("column2");
...
// add rows
for (int rowIndex = cells.FirstRowIndex + 1; rowIndex <= cells.LastRowIndex; rowIndex++)
{
var values = new List<string>();
foreach(var cell in cells.GetRow(rowIndex))
{
values.Add(cell.Value.StringValue);
}
dataTable.LoadDataRow(values.ToArray(), true);
}
It's not exactly the prettiest code but it returns a DataTable
. I recommend that you just use the values directly if possible ie. instead of converting to a DataTable
read the values directly and skip this conversion step.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With