一个封装好的类
//读取excel文件类
public class ExcelHelper
{
private static IWorkbook workbook = null;
private static FileStream fs = null;
///
/// 将excel中的数据导入到DataTable中
///
/// excel文件路径
/// excel工作薄sheet的名称
/// 第一行是否是DataTable的列名
/// 返回的DataTable
public static DataTable ExcelToDataTable(string fileName, string sheetName, bool isFirstRowColumn, DataTable data)
{
ISheet sheet = null;
int startRow = 0;
try
{
fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
if (fileName.IndexOf(".xlsx") > 0) // 2007版本
{
workbook = new XSSFWorkbook(fs);
}
else if (fileName.IndexOf(".xls") > 0) // 2003版本
{
workbook = new HSSFWorkbook(fs);
}
if (sheetName != null)
{
sheet = workbook.GetSheet(sheetName);
if (sheet == null) //如果没有找到指定的sheetName对应的sheet,则尝试获取第一个sheet
{
sheet = workbook.GetSheetAt(0);
}
}
else
{
sheet = workbook.GetSheetAt(0);
}
if (sheet != null)
{
IRow firstRow = sheet.GetRow(0);
int cellCount = firstRow.LastCellNum; //一行最后一个cell的编号 即总的列数
if (isFirstRowColumn)
{
for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
{
ICell cell = firstRow.GetCell(i);
if (cell != null)
{
string cellValue = cell.StringCellValue;
if (cellValue != null)
{
DataColumn column = new DataColumn(cellValue);
data.Columns.Add(column);
}
}
}
startRow = sheet.FirstRowNum + 1;
}
else
{
for (int i = firstRow.FirstCellNum; i < cellCount; i++)
{
DataColumn column = new DataColumn(i.ToString());
data.Columns.Add(column);
}
startRow = sheet.FirstRowNum;
}
//最后一列的标号
int rowCount = sheet.LastRowNum;
for (int i = startRow; i <= rowCount; ++i)
{
IRow row = sheet.GetRow(i);
if (row == null) continue; //没有数据的行默认是null
DataRow dataRow = data.NewRow();
for (int j = row.FirstCellNum; j < cellCount; ++j)
{
if (row.GetCell(j) != null) //同理,没有数据的单元格都默认是null
dataRow[j] = row.GetCell(j).ToString();
}
data.Rows.Add(dataRow);
}
}
return data;
}
catch (Exception ex)
{
Console.WriteLine("Exception: " + ex.Message);
return null;
}
}
}
直接调用
string fileName = "D:\\XXX\\XXX.xlsx";
string sheetName = "XXX";
DataTable DT = new DataTable();
ExcelHelper.ExcelToDataTable(fileName, sheetName, true, DT);
DT.DataTableToList(DT_list);
补充一下:将DataTable转为list
///
/// DataTable 转换成泛型List
///
/// 实体对象类
/// 数据DatatTable
/// List集合
/// 空字符是否需要
/// List<实体对象>
public static List<T> DataTableToList<T>(this DataTable dt, List<T> objList = null, bool isNullEmpty = true)
{
if (dt == null)
{
objList = null;
}
if (objList == null)
{
objList = new List<T>();
}
var columnNames = dt.Columns.Cast<DataColumn>().Select(c => c.ColumnName).ToList();
var properties = typeof(T).GetProperties();
object value = null;
foreach (DataRow row in dt.Rows)
{
var t = Activator.CreateInstance<T>();
{
foreach (PropertyInfo pro in properties)
{
if (!pro.CanWrite)
{
continue;
}
if (dt.Columns.Contains(pro.Name.ToUpper()))
{
value = row[pro.Name];
if (value is System.DBNull)
{
value = null;
if (pro.PropertyType.FullName == "System.String")
{
value = string.Empty;
}
}
if (isNullEmpty)
{
if (pro.PropertyType.FullName == "System.String" && string.IsNullOrEmpty(value.ToString().Trim()))
{
value = string.Empty;
}
}
if (pro.PropertyType.IsGenericType
&& pro.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>)
&& value != null)
{
pro.SetValue(t, Convert.ChangeType(value, Nullable.GetUnderlyingType(pro.PropertyType)), null);
}
else if (pro.PropertyType.IsEnum)
{
pro.SetValue(t, Convert.ChangeType(value, Enum.GetUnderlyingType(pro.PropertyType)), null);
}
else
{
pro.SetValue(t, value, null);
}
}
}
}
objList.Add(t);
}
return objList;
}