C reads Excel using the Aspose. Cells control

  • 2021-09-11 21:04:32
  • OfStack

Aspose is a very powerful control, can be used to operate word, excel, ppt and other files, with this control to import, export data is very convenient. Among them, Aspose. Cells is used to operate Excel, and there are many functions. What I use is the most basic function, reading Excel data and importing it into Dataset or database. The code for reading Excel table data is as follows:

First, introduce namespaces: using Aspose. Cells;


Workbook workbook = new Workbook();
workbook.Open("C:\\test.xlsx");
Cells cells = workbook.Worksheets[0].Cells;
for (int i = 0; i < cells.MaxDataRow + 1; i++)
{
 for (int j = 0; j < cells.MaxDataColumn + 1; j++)
 {
  string s = cells[i, j].StringValue.Trim();
  //1 The code for reading data and inserting data into the database can also be written here
 }
}

Return to Datatable:


Cells cells = workbook.Worksheets[1].Cells;
System.Data.DataTable dataTable1 = cells.ExportDataTable(1, 0, cells.MaxDataRow,cells.MaxColumn);//noneTitle
System.Data.DataTable dataTable2 = cells.ExportDataTable(0, 0, cells.MaxDataRow+1,cells.MaxColumn,true);//showTitle


Related articles: