2017-09-21
C# 读取一个CSV文件,怎么把CSV文件中的数据放到DataTabel中?
/// <summary>
/// 读取Excel文件为DataTable,使用Aspose.Cells.dll
/// </summary>
/// <param name="filepath"> Excel文件的全路径 </param>
/// <returns> DataTable</returns>
public static DataTable ExcelToDataTable(string filepath)
{
DataTable dt = new DataTable();
try
{
if (!File .Exists(filepath))
{
MessageBox.Show(@"文件不存在!" ,@"提示");
return null ;
}
Aspose.Cells. Workbook workbook = new Aspose.Cells.Workbook();
workbook.Open(filepath);
Aspose.Cells. Worksheet worksheet = workbook.Worksheets[0];
dt = worksheet.Cells.ExportDataTable(0, 0, worksheet.Cells.MaxDataRow + 1, worksheet.Cells.MaxColumn + 1, true );//showTitle
return dt;
}
catch (System.Exception e)
{
return null ;
}