1、命名空间
using Aspose.Cells;
2、代码
#region Aspose.Cells方式
/// <summary>
/// 本机无需安装Microsoft Excel
/// 1、将Excel文件数据(多个sheet页分为不同的表)导入到DataSet临时表中
/// 2、解析Excel表格列和行的格式
/// </summary>
/// <returns></returns>
private static string fileName = string.Empty;
public static DataSet AsposeCells()
{
DataSet ds = new DataSet();
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = "c:\\";//注意这里写路径时要用c:\\而不是c:\
openFileDialog.Filter = "Excel文件|*.xls;*.xlsx";
openFileDialog.RestoreDirectory = true;
openFileDialog.FilterIndex = 1;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
fileName = openFileDialog.FileName;
//读取不同的sheet页名称
Workbook workbook = new Workbook(fileName);
int a = workbook.Worksheets.Count();
for (int i = 0; i < a; i++)
{
Cells cells = workbook.Worksheets[i].Cells;
//DataTable dataTable = cells.ExportDataTable(1, 0, cells.MaxDataRow, cells.MaxColumn);//没有列名
DataTable dataTable = cells.ExportDataTable(0, 0, cells.MaxDataRow + 1, cells.MaxColumn, true);//有列名
ds.Tables.Add(dataTable);
}
}
return ds;
}
#endregion
3、调用方法
AsposeCells();
4、更多方式请见其他博客文章~
代码途径来源于多方copy,使用无误才上传。请大家收纳~