包含非强类型数据的列

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

 //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();

以下是屏幕截图:

todo:image_alt_text

todo:image_alt_text

下载示例代码