/// <summary>
///
/// </summary>
/// <param name="FilePath"></param>
/// <param name="TargetSheetIndex"></param>
/// <param name="ColumnNames"></param>
/// <returns></returns>
public static DataTable GetExcelData(String FilePath, int TargetSheetIndex, String[] ColumnNames)
{
Workbook Book = new Workbook(FilePath);
Worksheet Sheet = Book.Worksheets[TargetSheetIndex];
for (int i = 0; i < ColumnNames.Length; i++)
{
if (!ColumnNames[i].Equals(Sheet.Cells.GetCell(0, i).StringValue))
{
throw new Exception("Wrong Excel Format");
}
}
//获取数据内容
DataTable Dt = Sheet.Cells.ExportDataTableAsString(1, 0, Sheet.Cells.MaxDataRow, Sheet.Cells.MaxColumn + 1);
//更新表头的列名
for (int i = 0; i < Dt.Columns.Count; i++)
{
Dt.Columns[i].ColumnName = Sheet.Cells[0, i].StringValue;
}
//设置Table名为sheet名
Dt.TableName = Sheet.Name;
return Dt;
}
///
/// </summary>
/// <param name="FilePath"></param>
/// <param name="TargetSheetIndex"></param>
/// <param name="ColumnNames"></param>
/// <returns></returns>
public static DataTable GetExcelData(String FilePath, int TargetSheetIndex, String[] ColumnNames)
{
Workbook Book = new Workbook(FilePath);
Worksheet Sheet = Book.Worksheets[TargetSheetIndex];
for (int i = 0; i < ColumnNames.Length; i++)
{
if (!ColumnNames[i].Equals(Sheet.Cells.GetCell(0, i).StringValue))
{
throw new Exception("Wrong Excel Format");
}
}
//获取数据内容
DataTable Dt = Sheet.Cells.ExportDataTableAsString(1, 0, Sheet.Cells.MaxDataRow, Sheet.Cells.MaxColumn + 1);
//更新表头的列名
for (int i = 0; i < Dt.Columns.Count; i++)
{
Dt.Columns[i].ColumnName = Sheet.Cells[0, i].StringValue;
}
//设置Table名为sheet名
Dt.TableName = Sheet.Name;
return Dt;
}