NPOI Read Excel to DataTable
//2018-5-18 Yuan Xin NOPI
public DataTable Excel_To_DataTable(string filepath, int sheetindex)
{
// --------------------------------- //
/* REFERENCIAS:
* NPOI.dll
* NPOI.OOXML.dll
* NPOI.OpenXml4Net.dll */
// --------------------------------- //
/* USING:
* using NPOI.SS.UserModel;
* using NPOI.HSSF.UserModel;
* using NPOI.XSSF.UserModel; */
// --------------------------------- //
DataTable dt = null;
try
{
if (System.IO.File.Exists(filepath))
{
IWorkbook workbook = null;
ISheet worksheet = null;
string first_sheet_name = "";
using (FileStream FS = new FileStream(filepath, FileMode.Open, FileAccess.Read))
{
workbook = WorkbookFactory.Create(FS);
worksheet = workbook.GetSheetAt(sheetindex);
first_sheet_name = worksheet.SheetName;
dt = new DataTable(first_sheet_name);
dt.Rows.Clear();
dt.Columns.Clear();
for (int rowIndex = 0; rowIndex <= worksheet.LastRowNum; rowIndex++)
{
DataRow NewReg = null;
IRow row = worksheet.GetRow(rowIndex);
IRow row2 = null;
if (row != null)
{
if (rowIndex > 0) NewReg = dt.NewRow();
foreach (ICell cell in row.Cells)
{
object valorCell = null;
string cellType = "";
if (rowIndex == 0)
{
row2 = worksheet.GetRow(rowIndex + 1);
if (row2 != null)
{
ICell cell2 = row2.GetCell(cell.ColumnIndex);
if (cell2 != null)
{
switch (cell2.CellType)
{
case CellType.Boolean: cellType = "System.Boolean"; break;
case CellType.String: cellType = "System.String"; break;
case CellType.Numeric:
if (HSSFDateUtil.IsCellDateFormatted(cell2)) { cellType = "System.DateTime"; }
else { cellType = "System.Double"; } break;
case CellType.Formula:
switch (cell2.CachedFormulaResultType)
{
case CellType.Boolean: cellType = "System.Boolean"; break;
case CellType.String: cellType = "System.String"; break;
case CellType.Numeric:
if (HSSFDateUtil.IsCellDateFormatted(cell2)) { cellType = "System.DateTime"; }
else { cellType = "System.Double"; } break;
}
break;
default:
cellType = "System.String"; break;
}
DataColumn dc = new DataColumn(cell.StringCellValue, System.Type.GetType(cellType));
dt.Columns.Add(dc);
}
else
{
DataColumn dc = new DataColumn("", typeof(string));
dt.Columns.Add(dc);
}
}
}
else
{
if (cell == null)
{
continue;
}
else
{
switch (cell.CellType)
{
case CellType.Blank: valorCell = DBNull.Value; break;
case CellType.Boolean: valorCell = cell.BooleanCellValue; break;
case CellType.String: valorCell = cell.StringCellValue; break;
case CellType.Numeric:
if (HSSFDateUtil.IsCellDateFormatted(cell)) { valorCell = cell.DateCellValue; }
else { valorCell = cell.NumericCellValue; } break;
case CellType.Formula:
switch (cell.CachedFormulaResultType)
{
case CellType.Blank: valorCell = DBNull.Value; break;
case CellType.String: valorCell = cell.StringCellValue; break;
case CellType.Boolean: valorCell = cell.BooleanCellValue; break;
case CellType.Numeric:
if (HSSFDateUtil.IsCellDateFormatted(cell)) { valorCell = cell.DateCellValue; }
else { valorCell = cell.NumericCellValue; }
break;
}
break;
default: valorCell = cell.StringCellValue; break;
}
NewReg[cell.ColumnIndex] = valorCell;
}
}
}
}
if (rowIndex > 0) dt.Rows.Add(NewReg);
}
dt.AcceptChanges();
}
}
else
{
throw new Exception("ERROR 404:NO existe.");
}
}
catch (Exception ex)
{
throw ex;
}
return dt;
}
posted on 2018-05-23 14:58 yuanxin1991 阅读(162) 评论(0) 收藏 举报
浙公网安备 33010602011771号