包含非强类型数据的列

如果工作表列中的所有值都不是强类型的(这意味着列中的值可能具有不同的数据类型),那么我们可以通过调用导出工作表内容ExportDataTableAsString Cells 类的方法。ExportDataTableAsString方法采用与导出数据表将工作表数据导出为的方法数据表目的。

 //Creating a file stream containing the Excel file to be opened

FileStream fstream = new FileStream(FOD_OpenFile.FileName, FileMode.Open);

//Instantiating a Workbook object

//Opening the Excel file through the file stream

Workbook workbook = new Workbook(fstream);

//Accessing the first worksheet in the Excel file

Worksheet worksheet = workbook.Worksheets[0];

//Exporting the contents of 2 rows and 2 columns starting from 1st cell to DataTable

DataTable dataTable = worksheet.Cells.ExportDataTableAsString(0, 0, 2, 2, true);

//Binding the DataTable with DataGrid

dataGridView2.DataSource = dataTable;

//Closing the file stream to free all resources

fstream.Close();

以下是屏幕截图:

待办事项:图片_替代_文本

待办事项:图片_替代_文本

下载示例代码