这儿采取的是将图片的二进制流导出到Excel,直接上代码:
/// <summary> /// DataTable导出到Excel的MemoryStream /// </summary> /// <param name="dtSource">源DataTable</param> /// <param name="strHeaderText">表头文本</param> /// <param name="strSheetName">工作表名称</param> public static MemoryStream Export(DataTable dtSource, string strHeaderText, string strSheetName, string[] oldColumnNames, string[] newColumnNames) { if (oldColumnNames.Length != newColumnNames.Length) { return new MemoryStream(); } HSSFWorkbook workbook = new HSSFWorkbook(); ISheet sheet = workbook.CreateSheet(strSheetName); #region 右击文件 属性信息 { DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation(); dsi.Company = "http://....../"; workbook.DocumentSummaryInformation = dsi; SummaryInformation si = PropertySetFactory.CreateSummaryInformation(); if (HttpContext.Current.Session["realname"] != null) { si.Author = HttpContext.Current.Session["realname"].ToString(); } else { if (HttpContext.Current.Session["username"] != null) { si.Author = HttpContext.Current.Session["username"].ToString(); } } //填加xls文件作者信息 si.ApplicationName = "NPOI"; //填加xls文件创建程序信息 si.LastAuthor = "OA系统"; //填加xls文件最后保存者信息 si.Comments = "OA系统自动创建文件"; //填加xls文件作者信息 si.Title = strHeaderText; //填加xls文件标题信息 si.Subject = strHeaderText; //填加文件主题信息 si.CreateDateTime = DateTime.Now; workbook.SummaryInformation = si; } #endregion ICellStyle dateStyle = workbook.CreateCellStyle(); IDataFormat format = workbook.CreateDataFormat(); dateStyle.DataFormat = format.GetFormat("yyyy-mm-dd"); #region 取得列宽 int[] arrColWidth = new int[oldColumnNames.Length]; for (int i = 0; i < oldColumnNames.Length; i++) { arrColWidth[i] = Encoding.GetEncoding(936).GetBytes(newColumnNames[i]).Length; } for (int i = 0; i < dtSource.Rows.Count; i++) { for (int j = 0; j < oldColumnNames.Length; j++) { int intTemp = Encoding.GetEncoding(936).GetBytes(dtSource.Rows[i][oldColumnNames[j]].ToString()).Length; if (intTemp > arrColWidth[j]) { arrColWidth[j] = intTemp; } } } #endregion int rowIndex = 0; foreach (DataRow row in dtSource.Rows) { #region 新建表,填充表头,填充列头,样式 if (rowIndex == 65535 || rowIndex == 0) { if (rowIndex != 0) { sheet = workbook.CreateSheet(strSheetName + ((int)rowIndex / 65535).ToString()); } #region 表头及样式 { IRow headerRow = sheet.CreateRow(0); headerRow.HeightInPoints = 25; headerRow.CreateCell(0).SetCellValue(strHeaderText); ICellStyle headStyle = workbook.CreateCellStyle(); headStyle.Alignment = HorizontalAlignment.Center; IFont font = workbook.CreateFont(); font.FontHeightInPoints = 20; font.Boldweight = 700; headStyle.SetFont(font); headerRow.GetCell(0).CellStyle = headStyle; sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, dtSource.Columns.Count - 1)); } #endregion #region 列头及样式 { IRow headerRow = sheet.CreateRow(1); ICellStyle headStyle = workbook.CreateCellStyle(); headStyle.Alignment = HorizontalAlignment.Center; IFont font = workbook.CreateFont(); font.FontHeightInPoints = 10; font.Boldweight = 700; headStyle.SetFont(font); for (int i = 0; i < oldColumnNames.Length; i++) { headerRow.CreateCell(i).SetCellValue(newColumnNames[i]); headerRow.GetCell(i).CellStyle = headStyle; //设置列宽 sheet.SetColumnWidth(i, (arrColWidth[i] + 1) * 256); } } #endregion rowIndex = 2; } #endregion #region 填充内容 IRow dataRow = sheet.CreateRow(rowIndex); for (int i = 0; i < oldColumnNames.Length; i++) { ICell newCell = dataRow.CreateCell(i); bool hasImage = false; string drValue = row[oldColumnNames[i]].ToString(); switch (dtSource.Columns[oldColumnNames[i]].DataType.ToString()) { case "System.Guid"://GUID类型 case "System.String"://字符串类型 newCell.SetCellValue(drValue); break; case "System.DateTime"://日期类型 DateTime dateV; DateTime.TryParse(drValue, out dateV); newCell.SetCellValue(dateV); 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.Byte[]"://二进制流 int pictureIdx = 0; if (row[oldColumnNames[i]] == null || string.IsNullOrEmpty(drValue)) { newCell.SetCellValue(""); break; } pictureIdx = workbook.AddPicture((byte[])row[oldColumnNames[i]], PictureType.JPEG); HSSFPatriarch patriarch = (HSSFPatriarch)sheet.CreateDrawingPatriarch(); HSSFClientAnchor anchor = new HSSFClientAnchor(10, 0, 2, 0, i, rowIndex, i + 1, rowIndex + 1); //##处理照片位置,【图片左上角为(col, row)第row+1行col+1列,右下角为( col +1, row +1)第 col +1+1行row +1+1列,第三个参数为宽,第四个参数为高 HSSFPicture pict = (HSSFPicture)patriarch.CreatePicture(anchor, pictureIdx); // pict.Resize();//这句话一定不要,这是用图片原始大小来显示 hasImage = true; 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; } if (hasImage) { dataRow.HeightInPoints = 80; } } #endregion rowIndex++; } using (MemoryStream ms = new MemoryStream()) { workbook.Write(ms); ms.Flush(); ms.Position = 0; sheet = null; workbook = null; return ms; } }
注意填充内容里的switch循环里的 System.Byte[] ,也就是判断图片流并将转换后的图片输出到Excel的方法。
时间: 2024-10-08 17:14:59