80 lines
3.2 KiB
C#
80 lines
3.2 KiB
C#
using ExcelDataReader;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Data;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Text;
|
||
|
||
namespace Io.Github.Kerwinxu.LibShapes.Utils
|
||
{
|
||
/// <summary>
|
||
/// 这个是一个工具类,可以读取excel数据的
|
||
/// </summary>
|
||
public class ExcelData
|
||
{
|
||
public static DataTable LoadExcel(string file_path)
|
||
{
|
||
using (var stream = File.Open(file_path, FileMode.Open, FileAccess.Read))
|
||
{
|
||
using (var reader = ExcelReaderFactory.CreateReader(stream))
|
||
{
|
||
|
||
// Gets or sets a callback to obtain configuration options for a DataTable.
|
||
var conf = new ExcelDataSetConfiguration()
|
||
{
|
||
// Gets or sets a value indicating whether to set the DataColumn.DataType
|
||
// property in a second pass.
|
||
UseColumnDataType = true,
|
||
|
||
// Gets or sets a callback to determine whether to include the current sheet
|
||
// in the DataSet. Called once per sheet before ConfigureDataTable.
|
||
FilterSheet = (tableReader, sheetIndex) => true,
|
||
|
||
// Gets or sets a callback to obtain configuration options for a DataTable.
|
||
ConfigureDataTable = (tableReader) => new ExcelDataTableConfiguration()
|
||
{
|
||
// Gets or sets a value indicating the prefix of generated column names.
|
||
EmptyColumnNamePrefix = "Column",
|
||
|
||
// Gets or sets a value indicating whether to use a row from the
|
||
// data as column names. 是否使用数据作为列名
|
||
UseHeaderRow = true,
|
||
|
||
//// Gets or sets a callback to determine which row is the header row.
|
||
//// Only called when UseHeaderRow = true.
|
||
//ReadHeaderRow = (rowReader) =>
|
||
//{
|
||
// // F.ex skip the first row and use the 2nd row as column headers:
|
||
// rowReader.Read();
|
||
//},
|
||
|
||
// Gets or sets a callback to determine whether to include the
|
||
// current row in the DataTable.
|
||
FilterRow = (rowReader) =>
|
||
{
|
||
return true;
|
||
},
|
||
|
||
// Gets or sets a callback to determine whether to include the specific
|
||
// column in the DataTable. Called once per column after reading the
|
||
// headers.
|
||
FilterColumn = (rowReader, columnIndex) =>
|
||
{
|
||
return true;
|
||
}
|
||
}
|
||
};
|
||
|
||
var ds = reader.AsDataSet(conf);
|
||
// 这里方便只是导入第一个
|
||
return ds.Tables[0];
|
||
}
|
||
|
||
}
|
||
|
||
return null;
|
||
}
|
||
}
|
||
}
|