using NPOI.HPSF; using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Infrastructure.Helpers.Excel { public class ExcelHelper { #region DataTable导出到Excel的MemoryStream /// /// DataTable导出到Excel的MemoryStream Export() /// /// DataTable数据源 /// 导出设置包含文件名、标题、列设置 public static MemoryStream ExportMemoryStream(DataTable dtSource, ExcelConfig excelConfig) { Dictionary DataColumnMap = new Dictionary(); //DataTable dt = dtSource.Clone(); //var columns = dt.Columns; foreach (DataColumn column in dtSource.Columns) { bool v = excelConfig.ColumnEntity.Exists(t => t.Column == column.ColumnName); if (v) { DataColumnMap.Add(column.ColumnName, column); //dtSource.Columns.Remove(column.ColumnName); } /*else { DataColumnMap.Add(column.ColumnName, column); }*/ } HSSFWorkbook workbook = new HSSFWorkbook(); ISheet sheet = workbook.CreateSheet(); #region 右击文件 属性信息 { DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation(); dsi.Company = "山东慧创信息科技有限公司"; workbook.DocumentSummaryInformation = dsi; SummaryInformation si = PropertySetFactory.CreateSummaryInformation(); si.Title = "标题信息"; //填加xls文件标题信息 si.Subject = "主题信息";//填加文件主题信息 si.CreateDateTime = System.DateTime.Now; workbook.SummaryInformation = si; } #endregion #region 设置标题样式 ICellStyle headStyle = workbook.CreateCellStyle(); int[] arrColWidth = new int[excelConfig.ColumnEntity.Count]; string[] arrColName = new string[excelConfig.ColumnEntity.Count];//列名 ICellStyle[] arryColumStyle = new ICellStyle[excelConfig.ColumnEntity.Count];//样式表 headStyle.Alignment = HorizontalAlignment.Center; // ------------------ if (excelConfig.Background != new System.Drawing.Color()) { if (excelConfig.Background != new System.Drawing.Color()) { headStyle.FillPattern = FillPattern.SolidForeground; headStyle.FillForegroundColor = GetXLColour(workbook, excelConfig.Background); } } IFont font = workbook.CreateFont(); font.FontHeightInPoints = excelConfig.TitlePoint; if (excelConfig.ForeColor != new System.Drawing.Color()) { font.Color = GetXLColour(workbook, excelConfig.ForeColor); } font.IsBold = true; headStyle.SetFont(font); #endregion #region 列头及样式 ICellStyle cHeadStyle = workbook.CreateCellStyle(); cHeadStyle.Alignment = HorizontalAlignment.Center; // ------------------ IFont cfont = workbook.CreateFont(); cfont.FontHeightInPoints = excelConfig.HeadPoint; cHeadStyle.SetFont(cfont); #endregion #region 设置内容单元格样式 int colNum = 0; foreach (var columnentity in excelConfig.ColumnEntity) { //DataColumn item = DataColumnMap[column.Column]; ICellStyle columnStyle = workbook.CreateCellStyle(); columnStyle.Alignment = HorizontalAlignment.Center; //Encoding.GetEncoding(936) arrColWidth[colNum] = Encoding.UTF8.GetBytes(columnentity.Column).Length; arrColName[colNum] = columnentity.Column.ToString(); arrColName[colNum] = columnentity.ExcelColumn; if (columnentity.Width != 0) { arrColWidth[colNum] = columnentity.Width; } if (columnentity.Background != new System.Drawing.Color()) { if (columnentity.Background != new System.Drawing.Color()) { columnStyle.FillPattern = FillPattern.SolidForeground; columnStyle.FillForegroundColor = GetXLColour(workbook, columnentity.Background); } } if (columnentity.Font != null || columnentity.Point != 0 || columnentity.ForeColor != new System.Drawing.Color()) { IFont columnFont = workbook.CreateFont(); columnFont.FontHeightInPoints = 10; if (columnentity.Font != null) { columnFont.FontName = columnentity.Font; } if (columnentity.Point != 0) { columnFont.FontHeightInPoints = columnentity.Point; } if (columnentity.ForeColor != new System.Drawing.Color()) { columnFont.Color = GetXLColour(workbook, columnentity.ForeColor); } columnStyle.SetFont(font); } columnStyle.Alignment = getAlignment(columnentity.Alignment); arryColumStyle[colNum] = columnStyle; colNum++; } if (excelConfig.IsAllSizeColumn) { #region 根据列中最长列的长度取得列宽 for (int i = 0; i < dtSource.Rows.Count; i++) { for (int j = 0; j < excelConfig.ColumnEntity.Count; j++) { if (arrColWidth[j] != 0) { //Encoding.GetEncoding(936) int intTemp = Encoding.UTF8.GetBytes(dtSource.Rows[i][excelConfig.ColumnEntity[j].Column].ToString()).Length; if (intTemp > arrColWidth[j]) { arrColWidth[j] = intTemp; } } } } #endregion } #endregion int rowIndex = 0; #region 表头及样式 if (excelConfig.Title != null) { IRow headerRow = sheet.CreateRow(rowIndex); rowIndex++; if (excelConfig.TitleHeight != 0) { headerRow.Height = (short)(excelConfig.TitleHeight * 20); } headerRow.HeightInPoints = 25; headerRow.CreateCell(0).SetCellValue(excelConfig.Title); headerRow.GetCell(0).CellStyle = headStyle; if (excelConfig.ColumnEntity.Count > 1) { sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, excelConfig.ColumnEntity.Count - 1)); // ------------------ } } #endregion #region 列头及样式 { IRow headerRow = sheet.CreateRow(rowIndex); rowIndex++; colNum = 0; #region 如果设置了列标题就按列标题定义列头,没定义直接按字段名输出 foreach (var column in excelConfig.ColumnEntity) { headerRow.CreateCell(colNum).SetCellValue(arrColName[colNum]); headerRow.GetCell(colNum).CellStyle = cHeadStyle; //设置列宽 //sheet.SetColumnWidth(column.Ordinal, (arrColWidth[column.Ordinal] + 1) * 256); int colWidth = (arrColWidth[colNum] + 1) * 256; if (colWidth < 255 * 256) { sheet.SetColumnWidth(colNum, colWidth < 3000 ? 3000 : colWidth); } else { sheet.SetColumnWidth(colNum, 6000); } colNum++; } #endregion } #endregion ICellStyle dateStyle = workbook.CreateCellStyle(); IDataFormat format = workbook.CreateDataFormat(); dateStyle.DataFormat = format.GetFormat("yyyy-mm-dd"); Dictionary dateStyleMap = new Dictionary(); dateStyleMap.Add("yyyy-mm-dd", dateStyle); foreach (DataRow row in dtSource.Rows) { #region 新建表,填充表头,填充列头,样式 if (rowIndex == 65535) { sheet = workbook.CreateSheet(); rowIndex = 0; #region 表头及样式 { if (excelConfig.Title != null) { IRow headerRow = sheet.CreateRow(rowIndex); rowIndex++; if (excelConfig.TitleHeight != 0) { headerRow.Height = (short)(excelConfig.TitleHeight * 20); } headerRow.HeightInPoints = 25; headerRow.CreateCell(0).SetCellValue(excelConfig.Title); headerRow.GetCell(0).CellStyle = headStyle; sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, excelConfig.ColumnEntity.Count - 1)); // ------------------ } } #endregion #region 列头及样式 { IRow headerRow = sheet.CreateRow(rowIndex); rowIndex++; #region 如果设置了列标题就按列标题定义列头,没定义直接按字段名输出 colNum = 0; foreach (var column in excelConfig.ColumnEntity) { headerRow.CreateCell(colNum).SetCellValue(arrColName[colNum]); headerRow.GetCell(colNum).CellStyle = cHeadStyle; //设置列宽 sheet.SetColumnWidth(colNum, (arrColWidth[colNum] + 1) * 256); colNum++; } #endregion } #endregion } #endregion #region 填充内容 IRow dataRow = sheet.CreateRow(rowIndex); colNum = 0; foreach (var columnentity in excelConfig.ColumnEntity) { ICell newCell = dataRow.CreateCell(colNum); newCell.CellStyle = arryColumStyle[colNum]; string drValue = row[columnentity.Column].ToString(); //ColumnModel columnentity = excelConfig.ColumnEntity.Find(t => t.Column == column.ColumnName); if (!string.IsNullOrEmpty(columnentity.DateFormat)) { if (!dateStyleMap.ContainsKey(columnentity.DateFormat)) { ICellStyle dateStyle2 = workbook.CreateCellStyle(); IDataFormat format2 = workbook.CreateDataFormat(); dateStyle2.DataFormat = format.GetFormat(columnentity.DateFormat); dateStyleMap.Add(columnentity.DateFormat, dateStyle2); } SetCell(newCell, dateStyleMap[columnentity.DateFormat], DataColumnMap[columnentity.Column].DataType, drValue); } else { SetCell(newCell, dateStyle, DataColumnMap[columnentity.Column].DataType, drValue); } colNum++; } #endregion rowIndex++; } MemoryStream ms = new MemoryStream(); workbook.Write(ms); ms.Flush(); ms.Position = 0; return ms; } #endregion #region 设置表格内容 private static void SetCell(ICell newCell, ICellStyle dateStyle, Type dataType, string drValue) { switch (dataType.ToString()) { case "System.String"://字符串类型 newCell.SetCellValue(drValue); break; case "System.DateTime"://日期类型 System.DateTime dateV; if (System.DateTime.TryParse(drValue, out dateV)) { newCell.SetCellValue(dateV); } else { newCell.SetCellValue(""); } newCell.CellStyle = dateStyle;//格式化显示 break; case "System.Boolean"://布尔型 bool boolV = false; bool.TryParse(drValue, out boolV); newCell.SetCellValue(boolV); break; case "System.Int16"://整型 case "System.Int32": case "System.Int64": case "System.Byte": int intV = 0; int.TryParse(drValue, out intV); newCell.SetCellValue(intV); break; case "System.Decimal"://浮点型 case "System.Double": double doubV = 0; double.TryParse(drValue, out doubV); newCell.SetCellValue(doubV); break; case "System.DBNull"://空值处理 newCell.SetCellValue(""); break; default: newCell.SetCellValue(""); break; } } #endregion #region 从Excel导入 /// /// 读取excel ,默认第一行为标头 /// /// excel文档路径 /// public static DataTable ExcelImport(string strFileName) { DataTable dt = new DataTable(); ISheet sheet = null; using (FileStream file = new FileStream(strFileName, FileMode.Open, FileAccess.Read)) { if (strFileName.IndexOf(".xlsx") == -1)//2003 { HSSFWorkbook hssfworkbook = new HSSFWorkbook(file); sheet = hssfworkbook.GetSheetAt(0); } else//2007 { XSSFWorkbook xssfworkbook = new XSSFWorkbook(file); sheet = xssfworkbook.GetSheetAt(0); } } System.Collections.IEnumerator rows = sheet.GetRowEnumerator(); IRow headerRow = sheet.GetRow(0); int cellCount = headerRow.LastCellNum; for (int j = 0; j < cellCount; j++) { ICell cell = headerRow.GetCell(j); dt.Columns.Add(cell.ToString()); } for (int i = (sheet.FirstRowNum + 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) dataRow[j] = row.GetCell(j).ToString(); } dt.Rows.Add(dataRow); } return dt; } /// /// 读取excel ,默认第一行为标头 /// /// 文件数据流 /// 文件类型 /// public static DataTable ExcelImport(Stream fileStream, string flieType) { DataTable dt = new DataTable(); ISheet sheet = null; if (flieType == ".xls") { HSSFWorkbook hssfworkbook = new HSSFWorkbook(fileStream); sheet = hssfworkbook.GetSheetAt(0); } else { XSSFWorkbook xssfworkbook = new XSSFWorkbook(fileStream); sheet = xssfworkbook.GetSheetAt(0); } System.Collections.IEnumerator rows = sheet.GetRowEnumerator(); IRow headerRow = sheet.GetRow(0); int cellCount = headerRow.LastCellNum; for (int j = 0; j < cellCount; j++) { ICell cell = headerRow.GetCell(j); dt.Columns.Add(cell.ToString()); } for (int i = (sheet.FirstRowNum + 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) dataRow[j] = row.GetCell(j).ToString(); } dt.Rows.Add(dataRow); } return dt; } #endregion #region RGB颜色转NPOI颜色 private static short GetXLColour(HSSFWorkbook workbook, System.Drawing.Color SystemColour) { short s = 0; HSSFPalette XlPalette = workbook.GetCustomPalette(); NPOI.HSSF.Util.HSSFColor XlColour = XlPalette.FindColor(SystemColour.R, SystemColour.G, SystemColour.B); if (XlColour == null) { if (NPOI.HSSF.Record.PaletteRecord.STANDARD_PALETTE_SIZE < 255) { XlColour = XlPalette.FindSimilarColor(SystemColour.R, SystemColour.G, SystemColour.B); s = XlColour.Indexed; } } else s = XlColour.Indexed; return s; } #endregion #region 设置列的对齐方式 /// /// 设置对齐方式 /// /// /// private static HorizontalAlignment getAlignment(string style) { switch (style) { case "center": return HorizontalAlignment.Center; case "left": return HorizontalAlignment.Left; case "right": return HorizontalAlignment.Right; case "fill": return HorizontalAlignment.Fill; case "justify": return HorizontalAlignment.Justify; case "centerselection": return HorizontalAlignment.CenterSelection; case "distributed": return HorizontalAlignment.Distributed; } return NPOI.SS.UserModel.HorizontalAlignment.General; } #endregion } }