第一种:通过表名获取
///
/// 从Excel中获取Datatable
///
/// 路径
/// 表下标
/// 从第几行开始(表头行)
///
public static DataTable GetDataTable(string path, int sheetIndex = 0, int fieldRowIndex = 0)
{
//保存获取表格的数据
DataTable dt = new DataTable();
try
{
//获取文件流
using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
IWorkbook workbook;
//获取excel的文件类型
ExcelType type = GetExcelFileType(path).Value;
//通过不同的文件类型创建不同的读取接口(xls使用HSSFWorkbook类实现,xlsx使用XSSFWorkbook类实现)
switch (type)
{
case ExcelType.xlsx:
workbook = new XSSFWorkbook(stream);
break;
default:
workbook = new HSSFWorkbook(stream);
break;
}
//
ISheet sheet = null;
//获取工作表,默认获取第一张
if (workbook.NumberOfSheets > sheetIndex && workbook.NumberOfSheets > 0)
{
//获取工作表
sheet = workbook.GetSheetAt(sheetIndex);
}
else
{
System.Windows.Forms.MessageBox.Show("索引超出界限或该文件表格为空!");
}
//是否已经获取工作表,如果没有则直接返回
if (sheet == null)
{
return null;
}
#region//获取表头,文本类型
//默认获取第一行
if (fieldRowIndex > sheet.LastRowNum)//判断行标是否超界
{
System.Windows.Forms.MessageBox.Show("行索引超出界限!");
return null;
}
//获取表头行
IRow headRow = sheet.GetRow(fieldRowIndex);
//列数
int cellcount = headRow.LastCellNum;
for (int i = 0; i < cellcount; i++)
{
DataColumn dc = new DataColumn();
//获取行中的i个元素
ICell cell = headRow.GetCell(i);
if (cell != null)
{
dc.ColumnName = cell.ToString();
//获取cell的数据类型
dc.DataType = Type.GetType("System.String");
}
else
{
dt.Columns.Add("");
dc.DataType = Type.GetType("System.String");
}
//添加列
if (!dt.Columns.Contains(dc.ColumnName))//判断列是否已经存在
{
dt.Columns.Add(dc);
}
else
{
}
}
#endregion
#region//获取表格内容
for (int i = (fieldRowIndex + 1); i <= sheet.LastRowNum; i++)
{
//获取工作表的一行
IRow row = sheet.GetRow(i);
DataRow dataRow = dt.NewRow();
for (int j = row.FirstCellNum; j < cellcount; j++)
{
if (row.GetCell(j) != null)
{
//判断单元格是否为日期格式(如果Excel有重复行会导致DataRow索引出界)
if (row.GetCell(j).CellType == NPOI.SS.UserModel.CellType.Numeric && HSSFDateUtil.IsCellDateFormatted(row.GetCell(j)))
{
if (row.GetCell(j).DateCellValue.Year > 1000)
{
dataRow[j] = row.GetCell(j).DateCellValue.ToString();
}
else
{
dataRow[j] = row.GetCell(j).ToString();
}
}
else
{
dataRow[j] = row.GetCell(j).ToString();
}
}
}
dt.Rows.Add(dataRow);
}
#endregion
return dt;
}
}
catch
{
return null;
}
}
第二种:通过表名获取
///
/// 从Excel中获取Datatable
///
/// 路径
/// 表名
/// 从第几行开始
///
public static DataTable GetDataTable(string path, string sheetName, int fieldRowIndex = 0)
{
//新建表格保存excel数据
DataTable dt = new DataTable();
try
{
//获取文件流
using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
IWorkbook workbook;
//获取excel的文件类型
ExcelType type = GetExcelFileType(path).Value;
//通过不同的文件类型创建不同的读取接口(xls使用HSSFWorkbook类实现,xlsx使用XSSFWorkbook类实现)
switch (type)
{
case ExcelType.xlsx:
workbook = new XSSFWorkbook(stream);
break;
default:
workbook = new HSSFWorkbook(stream);
break;
}
//
ISheet sheet = null;
//获取工作表
if (string.IsNullOrWhiteSpace(sheetName))
{
//sheet = workbook.GetSheetAt(0);
System.Windows.Forms.MessageBox.Show(sheetName+"表不存在!");
}
else
{
sheet = workbook.GetSheet(sheetName);
}
//是否已经获取工作表,如果没有则直接返回
if (sheet == null)
{
return null;
}
#region//获取表头,文本类型
//默认获取第一行
if (fieldRowIndex > sheet.LastRowNum)//判断行标是否超界
{
System.Windows.Forms.MessageBox.Show("行索引超出界限!");
return null;
}
IRow headRow = sheet.GetRow(fieldRowIndex);
//列数
int cellcount = headRow.LastCellNum;
for (int i = 0; i < cellcount; i++)
{
DataColumn dc = new DataColumn();
//获取行中的i个元素
ICell cell = headRow.GetCell(i);
if (cell != null)
{
dc.ColumnName = cell.ToString();
//获取cell的数据类型
//dc.DataType = Type.GetType("System.String");
dc.DataType = GetType(cell.CellType);
}
else
{
dt.Columns.Add("");
dc.DataType = GetType(cell.CellType);
}
dt.Columns.Add(dc);
}
#endregion
#region//获取表格内容
for (int i = (fieldRowIndex + 1); i <= sheet.LastRowNum; i++)
{
//获取工作表的某一行
IRow row = sheet.GetRow(i);
DataRow dataRow = dt.NewRow();
for (int j = row.FirstCellNum; j < cellcount; j++)
{
if (row.GetCell(j) != null)
{
//判断单元格是否为日期格式
if (row.GetCell(j).CellType == NPOI.SS.UserModel.CellType.Numeric && HSSFDateUtil.IsCellDateFormatted(row.GetCell(j)))
{
if (row.GetCell(j).DateCellValue.Year > 1000)
{
dataRow[j] = row.GetCell(j).DateCellValue.ToString();
}
else
{
dataRow[j] = row.GetCell(j).ToString();
}
}
else
{
dataRow[j] = row.GetCell(j).ToString();
}
}
}
//添加列
dt.Rows.Add(dataRow);
}
#endregion
return dt;
}
}
catch
{
return null;
}
}