using System;
using System.Collections.Generic;
using NPOI.XSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.HSSF.UserModel;
using System.IO;
using System.Data;
using System.Web;
namespace Eson.Utils.ExcelHelper
{
///
/// 导出Excel帮助类(内存流形式)
///
public class ExcelStreamExport
{
private static ExcelStreamExport _instance;
///
/// 实例化导出Excel帮助类
///
///
public static ExcelStreamExport GetInstance()
{
if (_instance == null)
_instance = new ExcelStreamExport();
return _instance;
}
public IWorkbook workBook;
public ISheet workSheet;
public MemoryStream ms;
#region Excel文件基本操作
///
/// 初始化工作簿(Book)
///
/// 文件名称,也作为工作簿名称
/// 是否导出2007以上版本的Excel
public void InitializeWorkbook(string strSaveName, bool is07Excel)
{
//内存流导出
workBook = is07Excel ? (new XSSFWorkbook() as IWorkbook) : (new HSSFWorkbook() as IWorkbook);
ms = new MemoryStream();
workSheet = workBook.CreateSheet(strSaveName);
}
///
/// 初始化工作簿(Book)
///
/// 文件名称,也作为工作簿名称
/// 是否导出2007以上版本的Excel(默认是)
/// 是否第一次创建Excel的Sheet页(默认是)
public void InitializeWorkSheet(string strSaveName, bool is07Excel = true, bool isFirstCreateSheet = true)
{
//内存流导出
workBook = is07Excel ? (new XSSFWorkbook() as IWorkbook) : (new HSSFWorkbook() as IWorkbook);
if (isFirstCreateSheet)
{
ms = new MemoryStream();
}
workSheet = workBook.CreateSheet(strSaveName);
}
///
/// 按序号打开工作表(Sheet)
///
/// 工作表序号,为空时默认第一个工作表
public void GetSheetByIndex(int? intSheetIndex)
{
if (intSheetIndex == null)
workSheet = workBook.GetSheetAt(0);
else
workSheet = workBook.GetSheetAt(Convert.ToInt32(intSheetIndex));
}
///
/// 按名称打开工作表(Sheet)
///
/// 工作表名称
public void GetSheetByName(string strSheetName)
{
workSheet = workBook.GetSheet(strSheetName);
}
///
/// 初始化工作簿,自动打开第intSheetIndex个Sheet(intSheetIndex默认为0)
///
/// 原Excel文件路径
/// 第几个Sheet,默认为0
/// 是否导出2007以上版本的Excel
public void GetSheetFromExcel(string strExcelFilePath, int? intSheetIndex, bool is07Excel)
{
InitializeWorkbook(strExcelFilePath, is07Excel);
GetSheetByIndex(intSheetIndex);
}
///
/// 将工作簿写入文件
/// 如果目标文件已经存在,先删除
///
/// 要保存的文件地址
public void WriteToFile(string strSaveFilePath)
{
FileInfo fi = new FileInfo(strSaveFilePath);
if (fi.Exists)
fi.Delete();
FileStream fs = new FileStream(strSaveFilePath, FileMode.Create);
workBook.Write(fs);
fs.Close();
}
#endregion
#region 写数据到excel文件
///
/// 导出Excel,传入泛型数据
///
/// T类型
/// 泛型
/// 想要转换的属性名称列表[key值对应Model属性,value值对应Excel显示的列名]
/// 保存名称
/// 是否导出2007以上版本的Excel,默认是
//public void WriteListDate(List lst, Dictionary dicProperties, string strSaveName, bool is07Excel = true)
// where T : class
//{
// #region 初始化工作簿
// InitializeWorkbook(strSaveName, is07Excel);
// //转换数据
// DataTable dtbl = ExtensiveHelper.ConvertListToTable(lst, dicProperties);
// //初始化行
// int rowIndex = 0;
// int intColumn = 0;
// IRow rowExcel = workSheet.CreateRow(rowIndex);
// ICell cellExcel;
// //初始化列
// foreach (KeyValuePair property in dicProperties)
// {
// cellExcel = rowExcel.CreateCell(intColumn);
// cellExcel.SetCellValue(property.Value);
// ++intColumn;
// }
// #endregion
// #region 写入内容
// foreach (DataRow rowInfo in dtbl.Rows)
// {
// ++rowIndex;
// rowExcel = workSheet.CreateRow(rowIndex);
// int intColumnContent = 0;
// foreach (KeyValuePair property in dicProperties)
// {
// cellExcel = rowExcel.CreateCell(intColumnContent);
// cellExcel.SetCellValue(rowInfo[property.Value].ToString());
// ++intColumnContent;
// }
// }
// #endregion
// //内存流导出
// ExportExcel(ms, strSaveName, is07Excel);
//}
///
/// 输出Excel文件
///
/// MemoryStream内存流
/// 文件保存名称
/// 是否导出2007以上版本的Excel
//private void ExportExcel(MemoryStream ms, string strSaveName, bool is07Excel)
//{
// try
// {
// ms.Flush();
// ms.Position = 0;
// workBook.Write(ms);
// workBook = null;
// HttpContext current = HttpContext.Current;
// current.Response.ContentType = "application/ms-excel";
// current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + UrlOper.GetToExcelName(strSaveName) + (is07Excel ? ".xlsx" : ".xls"));
// current.Response.BinaryWrite(ms.ToArray());
// ms.Close();
// ms = null;
// current.Response.Flush();
// current.Response.End();
// //current.Response.Close();
// }
// catch (Exception ex) { }
//}
#region 导出Excel,传入DataTable内存表
///
/// 导出Excel,传入DataTable内存表
///
/// DataTabke内存表
/// 想要转换的属性名称列表[key值对应Model属性,value值对应Excel显示的列名]
/// 保存名称
/// 是否导出2007以上版本的Excel,默认是
//public void WriteListDate(DataTable dtbl, Dictionary dicProperties, string strSaveName, bool is07Excel = true)
//{
// #region 初始化工作簿
// InitializeWorkbook(strSaveName, is07Excel);
// //初始化行
// int rowIndex = 0;
// int intColumn = 0;
// IRow rowExcel = workSheet.CreateRow(rowIndex);
// ICell cellExcel;
// //初始化列
// foreach (KeyValuePair property in dicProperties)
// {
// cellExcel = rowExcel.CreateCell(intColumn);
// cellExcel.SetCellValue(property.Value);
// ++intColumn;
// }
// #endregion
// #region 写入内容
// foreach (DataRow rowInfo in dtbl.Rows)
// {
// ++rowIndex;
// rowExcel = workSheet.CreateRow(rowIndex);
// int intColumnContent = 0;
// foreach (KeyValuePair property in dicProperties)
// {
// cellExcel = rowExcel.CreateCell(intColumnContent);
// cellExcel.SetCellValue(GetColumValue(dtbl, rowInfo, property.Key));
// ++intColumnContent;
// }
// }
// #endregion
// //内存流导出
// ExportExcel(ms, strSaveName, is07Excel);
//}
#endregion
#region 传入多张DataTable内存表数据,导出Excel
///
/// 传入多张DataTable内存表数据,导出Excel
///
/// DataTabke内存表泛型数据
/// 保存名称
/// 导出excel每页数据的Sheet名称(不传入则按照dtblList的序号展示)
/// 是否导出2007以上版本的Excel,默认是
//public void WriteListDate(List dtblList, string strSaveName = null, List sheetNameList = null, bool is07Excel = true)
//{
// #region Excel每月Sheet名称初始化
// if (sheetNameList == null || (sheetNameList != null && dtblList.Count < sheetNameList.Count))
// {
// sheetNameList = new List();
// for (var i = 1; i < dtblList.Count + 1; i++)
// {
// sheetNameList.Add(string.Format("数据表_{0}", i));
// }
// }
// if (string.IsNullOrEmpty(strSaveName))
// {
// strSaveName = DateTime.Now.ToString("yyyyMMddHHmmss");
// }
// #endregion
// #region 遍历数据表源
// var dtCount = 0;
// workBook = is07Excel ? (new XSSFWorkbook() as IWorkbook) : (new HSSFWorkbook() as IWorkbook);
// //创建流对象
// ms = new MemoryStream();
// foreach (var tb in dtblList)
// {
// if (tb != null && tb.Rows.Count > 0)
// {
// #region 获取表头列名称
// Dictionary dicProperties = new Dictionary();
// foreach (DataColumn column in tb.Columns)
// {
// dicProperties.Add(column.ColumnName, column.ColumnName);
// }
// #endregion
// var currSheet = workBook.CreateSheet(sheetNameList[dtCount]);
// //初始化行
// int rowIndex = 0;
// int intColumn = 0;
// IRow rowExcel = currSheet.CreateRow(rowIndex);
// ICell cellExcel;
// //初始化列
// foreach (KeyValuePair property in dicProperties)
// {
// cellExcel = rowExcel.CreateCell(intColumn);
// cellExcel.SetCellValue(property.Value);
// ++intColumn;
// }
// #region 写入内容
// foreach (DataRow rowInfo in tb.Rows)
// {
// ++rowIndex;
// rowExcel = currSheet.CreateRow(rowIndex);
// int intColumnContent = 0;
// foreach (KeyValuePair property in dicProperties)
// {
// cellExcel = rowExcel.CreateCell(intColumnContent);
// cellExcel.SetCellValue(GetColumValue(tb, rowInfo, property.Key));
// ++intColumnContent;
// }
// }
// #endregion
// }
// dtCount++;
// }
// #endregion
// //内存流导出
// ExportExcel(ms, strSaveName, is07Excel);
//}
#endregion
///
/// 通过提供的列名和具体行,返回DataTable中具体的某一列的值
///
/// 内存表
/// 具体行
/// 列名
/// 具体某列的值
public string GetColumValue(DataTable dtbl, DataRow rowInfo, string columName)
{
string clumValue = "";
foreach (DataColumn clum in dtbl.Columns)
{
if (clum.ColumnName.Equals(columName))
{
clumValue = rowInfo[clum].ToString();
break;
}
}
return clumValue;
}
///
/// 将DataTable导出成Excel文件
///
/// DataTabke内存表泛型
/// 字典泛型,想要转换的属性名称列表[key值对应Model属性,value值对应Excel显示的列名]
/// 保存名称
/// 换行数,表之间的间距,不换行可以默认输入null,也可以输入0
/// 是否导出2007以上版本的Excel,默认是
//public void WriteListDate(List lstDtbl, List> lstDicProperties, string strSaveName, int? intLineCount, bool is07Excel = true)
//{
// #region 初始化工作簿
// InitializeWorkbook(strSaveName, is07Excel);
// //初始化行
// int rowIndex = 0;
// IRow rowExcel;
// ICell cellExcel;
// #endregion
// #region 写内容
// for (int i = 0; i < lstDicProperties.Count; i++)
// {
// //写表头
// rowExcel = workSheet.CreateRow(rowIndex);
// int intColumn = 0;
// foreach (KeyValuePair property in lstDicProperties[i])
// {
// cellExcel = rowExcel.CreateCell(intColumn);
// cellExcel.SetCellValue(property.Value);
// ++intColumn;
// }
// //写入内容
// foreach (DataRow rowInfo in lstDtbl[i].Rows)
// {
// ++rowIndex;
// rowExcel = workSheet.CreateRow(rowIndex);
// int intColumnContent = 0;
// foreach (KeyValuePair property in lstDicProperties[i])
// {
// cellExcel = rowExcel.CreateCell(intColumnContent);
// cellExcel.SetCellValue(GetColumValue(lstDtbl[i], rowInfo, property.Key));
// ++intColumnContent;
// }
// }
// ++rowIndex;
// //换行
// rowIndex += intLineCount == null ? 0 : Convert.ToInt32(intLineCount);
// }
// #endregion
// //内存流导出
// ExportExcel(ms, strSaveName, is07Excel);
//}
#endregion
#region 读取Excel2007数据
///
/// 将Excel2007以上版本文件中的数据读出到DataTable中(xlsx)
///
/// Excel2007版本以上文件地址
/// 需读取Sheet的Index,Sheet1的index是0,默认0
///
public DataTable ConvertExcelToTable(string filePath, int sheetIndex = 0)
{
DataTable dt = new DataTable();
string extension = System.IO.Path.GetExtension(filePath).ToLower();
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
if (extension.Equals(".xls"))
{
workBook = new HSSFWorkbook(fs) as IWorkbook;
workSheet = workBook.GetSheetAt(sheetIndex);
#region 表头
IRow header = workSheet.GetRow(workSheet.FirstRowNum);
List columns = new List();
for (int i = 0; i < header.LastCellNum; i++)
{
object obj = GetValueTypeForXLS(header.GetCell(i) as HSSFCell);
if (obj == null || obj.ToString() == string.Empty)
{
dt.Columns.Add(new DataColumn("Columns" + i.ToString()));
}
else
dt.Columns.Add(new DataColumn(obj.ToString()));
columns.Add(i);
}
#endregion
#region 数据
for (int i = workSheet.FirstRowNum + 1; i <= workSheet.LastRowNum; i++)
{
DataRow dr = dt.NewRow();
int count = columns.Count; ;
foreach (int j in columns)
{
object obj = GetValueTypeForXLS(workSheet.GetRow(i).GetCell(j) as HSSFCell);
if (obj == null || string.IsNullOrEmpty(obj.ToString()))
{
count--;
dr[j] = "";
}
else
{
dr[j] = obj;
}
}
if (count != 0)
{
dt.Rows.Add(dr);
}
}
#endregion
}
else
{
try
{
workBook = new XSSFWorkbook(fs) as IWorkbook;
workSheet = workBook.GetSheetAt(sheetIndex);
#region 表头
IRow header = workSheet.GetRow(workSheet.FirstRowNum);
List columns = new List();
for (int i = 0; i < header.LastCellNum; i++)
{
object obj = GetValueTypeForXLSX(header.GetCell(i) as XSSFCell);
if (obj == null || obj.ToString() == string.Empty)
{
dt.Columns.Add(new DataColumn("Columns" + i.ToString()));
}
else
dt.Columns.Add(new DataColumn(obj.ToString()));
columns.Add(i);
}
#endregion
#region 数据
for (int i = workSheet.FirstRowNum + 1; i <= workSheet.LastRowNum; i++)
{
DataRow dr = dt.NewRow();
int count = columns.Count;
foreach (int j in columns)
{
object obj = GetValueTypeForXLSX(workSheet.GetRow(i).GetCell(j) as XSSFCell);
if (obj == null || string.IsNullOrEmpty(obj.ToString()))
{
count--;
dr[j] = "";
}
else
{
dr[j] = obj;
}
}
if (count != 0)
{
dt.Rows.Add(dr);
}
}
#endregion
}
catch (Exception)
{
workBook = new HSSFWorkbook(fs) as IWorkbook;
workSheet = workBook.GetSheetAt(sheetIndex);
#region 表头
IRow header = workSheet.GetRow(workSheet.FirstRowNum);
List columns = new List();
for (int i = 0; i < header.LastCellNum; i++)
{
object obj = GetValueTypeForXLS(header.GetCell(i) as HSSFCell);
if (obj == null || obj.ToString() == string.Empty)
{
dt.Columns.Add(new DataColumn("Columns" + i.ToString()));
}
else
dt.Columns.Add(new DataColumn(obj.ToString()));
columns.Add(i);
}
#endregion
#region 数据
for (int i = workSheet.FirstRowNum + 1; i <= workSheet.LastRowNum; i++)
{
DataRow dr = dt.NewRow();
int count = columns.Count; ;
foreach (int j in columns)
{
object obj = GetValueTypeForXLS(workSheet.GetRow(i).GetCell(j) as HSSFCell);
if (obj == null || string.IsNullOrEmpty(obj.ToString()))
{
count--;
dr[j] = "";
}
else
{
dr[j] = obj;
}
}
if (count != 0)
{
dt.Rows.Add(dr);
}
}
#endregion
}
}
}
return dt;
}
#endregion
#region 获取单元格类型
///
/// 获取单元格类型(xlsx)
///
/// 单元格
///
private static object GetValueTypeForXLSX(XSSFCell cell)
{
if (cell == null)
return null;
switch (cell.CellType)
{
case CellType.Blank: //BLANK:
return null;
case CellType.Boolean: //BOOLEAN:
return cell.BooleanCellValue;
case CellType.Numeric: //NUMERIC:
//short format = cell.CellStyle.DataFormat;
//if (format == 14 || format == 31 || format == 57 || format == 58 || format == 178)
//{
// return cell.DateCellValue;
//}
//else
//{
// return cell.NumericCellValue;
//}
//NPOI中数字和日期都是NUMERIC类型的,这里对其进行判断是否是日期类型
if (HSSFDateUtil.IsCellDateFormatted(cell))//日期类型
{
return cell.DateCellValue;
}
else//其他数字类型
{
return cell.NumericCellValue;
}
case CellType.String: //STRING:
return cell.StringCellValue;
case CellType.Error: //ERROR:
return cell.ErrorCellValue;
case CellType.Formula: //FORMULA:
default:
return "=" + cell.CellFormula;
}
}
///
/// 获取单元格类型(xls)
///
/// 单元格
///
private static object GetValueTypeForXLS(HSSFCell cell)
{
if (cell == null)
return null;
switch (cell.CellType)
{
case CellType.Blank: //BLANK:
return null;
case CellType.Boolean: //BOOLEAN:
return cell.BooleanCellValue;
case CellType.Numeric: //NUMERIC:
//short format = cell.CellStyle.DataFormat;
//if (format == 14 || format == 31 || format == 57 || format == 58 || format == 178)
//{
// return cell.DateCellValue;
//}
//else
//{
// return cell.NumericCellValue;
//}
//NPOI中数字和日期都是NUMERIC类型的,这里对其进行判断是否是日期类型
if (HSSFDateUtil.IsCellDateFormatted(cell))//日期类型
{
return cell.DateCellValue;
}
else//其他数字类型
{
return cell.NumericCellValue;
}
case CellType.String: //STRING:
return cell.StringCellValue;
case CellType.Error: //ERROR:
return cell.ErrorCellValue;
case CellType.Formula: //FORMULA:
default:
return "=" + cell.CellFormula;
}
}
#endregion
}
}