1.添加Aspose.dll的引用;
2.封装方法:
/// <summary>
/// Excel文件转换为DataTable.
/// </summary>
/// <param name="filepath">文件路径</param>
/// <returns></returns>
public DataTable ExcelFileToDataTable(string filepath)
{
DataTable datatable = null;
Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook(filepath);
foreach (Worksheet worksheet in workbook.Worksheets)
{
if (worksheet.Cells.Rows.Count > 0)
{
datatable = worksheet.Cells.ExportDataTable(0, 0, worksheet.Cells.MaxDataRow + 1, worksheet.Cells.MaxDataColumn + 1, true);
}
}
return true;
}
catch (System.Exception e)
{
MessageBox.Show(e.Message);
return false;
}
}