using JNPF.DependencyInjection;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.Data;
namespace JNPF.Common.Helper
{
///
/// Excel导入操作类
/// 版 本:V3.2.0
/// 版 权:拓通智联科技有限公司(http://www.tuotong-tech.com)
/// 日 期:2017.03.12.
///
[SuppressSniffer]
public class ExcelImportHelper
{
///
/// 从Excel中获取数据到DataTable.
///
/// Excel文件全路径(服务器路径).
/// 要获取数据的工作表序号(从0开始).
/// 工作表标题行所在行号(从0开始).
///
public static DataTable ToDataTable(string filePath, int SheetIndex = 0, int HeaderRowIndex = 0)
{
using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
IWorkbook workbook = null;
// 2003
if (filePath.IndexOf(".xlsx") == -1)
workbook = new HSSFWorkbook(file);
else
workbook = new XSSFWorkbook(file);
string SheetName = workbook.GetSheetName(SheetIndex);
return ToDataTable(workbook, SheetName, HeaderRowIndex);
}
}
///
/// 从Excel中获取数据到DataTable.
///
/// Excel文件全路径(服务器路径).
/// 要获取数据的工作表序号(从0开始).
/// 工作表标题行所在行号(从0开始).
///
public static DataTable ToDataTable(string filePath, Stream sr, int SheetIndex = 0, int HeaderRowIndex = 0)
{
IWorkbook workbook = null;
if (filePath.IndexOf(".xlsx") == -1)//2003
{
workbook = new HSSFWorkbook(sr);
}
else
{
workbook = new XSSFWorkbook(sr);
}
string SheetName = workbook.GetSheetName(SheetIndex);
return ToDataTable(workbook, SheetName, HeaderRowIndex);
}
///
/// 从Excel中获取数据到DataTable.
///
/// 要处理的工作薄.
/// 要获取数据的工作表名称.
/// 工作表标题行所在行号(从0开始).
///
public static DataTable ToDataTable(IWorkbook workbook, string SheetName, int HeaderRowIndex)
{
ISheet sheet = workbook.GetSheet(SheetName);
DataTable table = new DataTable();
try
{
IRow headerRow = sheet.GetRow(HeaderRowIndex);
int cellCount = headerRow.LastCellNum;
for (int i = headerRow.FirstCellNum; i < cellCount; i++)
{
if (headerRow.GetCell(i) != null && headerRow.GetCell(i).StringCellValue != null)
{
DataColumn column = new DataColumn(headerRow.GetCell(i).StringCellValue);
table.Columns.Add(column);
}
}
int rowCount = sheet.LastRowNum;
if (rowCount > 0)
{
#region 循环各行各列,写入数据到DataTable
for (int i = (sheet.FirstRowNum); i <= sheet.LastRowNum; i++)
{
IRow row = sheet.GetRow(i);
DataRow dataRow = table.NewRow();
for (int j = row.FirstCellNum; j < cellCount; j++)
{
ICell cell = row.GetCell(j);
if (cell == null)
{
dataRow[j] = null;
}
else
{
//if (cell.CellType == CellType.Numeric && DateUtil.IsCellDateFormatted(cell))
//{
// dataRow[j] = cell.DateCellValue.ToString("yyyy/MM/dd").Trim();
//}
//else
//{
dataRow[j] = cell.ToString().Trim();
//}
}
}
table.Rows.Add(dataRow);
}
#endregion
}
}
catch (System.Exception ex)
{
table.Clear();
table.Columns.Clear();
table.Columns.Add("出错了");
DataRow dr = table.NewRow();
dr[0] = ex.Message;
table.Rows.Add(dr);
return table;
}
finally
{
// sheet.Dispose();
workbook = null;
sheet = null;
}
#region 清除最后的空行
for (int i = table.Rows.Count-1; i >= 0; i--)
{
bool isnull = true;
for (int j = 0; j < table.Columns.Count; j++)
{
if (table.Rows[i][j] != null)
{
if (table.Rows[i][j].ToString() != "")
{
isnull = false;
break;
}
}
}
if (isnull)
{
table.Rows[i].Delete();
}
}
#endregion
return table;
}
}
}