/// <summary> /// DataTable导出到Excel的MemoryStream /// </summary> /// <param name="dtSource">源DataTable</param> /// <param name="strHeaderText">表头文本</param> public static MemoryStream DataTableToExcel1(DataTable dtSource, string strHeaderText) { Helper helper = new Helper("Data Source=172.128.2.1/veims;User ID=zte;Password=zsfyqch;"); HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = (HSSFSheet)workbook.CreateSheet(); #region 右击文件 属性信息 { DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation(); dsi.Company = "NPOI"; workbook.DocumentSummaryInformation = dsi; SummaryInformation si = PropertySetFactory.CreateSummaryInformation(); si.Author = "文件作者信息"; //填加xls文件作者信息 si.ApplicationName = "创建程序信息"; //填加xls文件创建程序信息 si.LastAuthor = "最后保存者信息"; //填加xls文件最后保存者信息 si.Comments = "作者信息"; //填加xls文件作者信息 si.Title = "标题信息"; //填加xls文件标题信息 si.Subject = "主题信息"; //填加文件主题信息 si.CreateDateTime = System.DateTime.Now; workbook.SummaryInformation = si; } #endregion HSSFCellStyle dateStyle = (HSSFCellStyle)workbook.CreateCellStyle(); HSSFDataFormat format = (HSSFDataFormat)workbook.CreateDataFormat(); dateStyle.DataFormat = format.GetFormat("yyyy-mm-dd"); //取得列宽 int[] arrColWidth = new int[dtSource.Columns.Count]; foreach (DataColumn item in dtSource.Columns) { arrColWidth[item.Ordinal] = Encoding.GetEncoding(936).GetBytes(item.ColumnName.ToString()).Length; } for (int i = 0; i < dtSource.Rows.Count; i++) { for (int j = 0; j < dtSource.Columns.Count; j++) { int intTemp = Encoding.GetEncoding(936).GetBytes(dtSource.Rows[i][j].ToString()).Length; if (intTemp > arrColWidth[j]) { arrColWidth[j] = intTemp; } } } int rowIndex = 0; foreach (DataRow row in dtSource.Rows) { #region 新建表,填充表头,填充列头,样式 if (rowIndex == 65535 || rowIndex == 0) { if (rowIndex != 0) { sheet = (HSSFSheet)workbook.CreateSheet(); } #region 表头及样式 { HSSFRow headerRow = (HSSFRow)sheet.CreateRow(0); headerRow.HeightInPoints = 25; headerRow.CreateCell(0).SetCellValue(strHeaderText); HSSFCellStyle headStyle = (HSSFCellStyle)workbook.CreateCellStyle(); // headStyle.Alignment = CellHorizontalAlignment.CENTER; HSSFFont font = (HSSFFont)workbook.CreateFont(); font.FontHeightInPoints = 20; font.Boldweight = 700; headStyle.SetFont(font); headerRow.GetCell(0).CellStyle = headStyle; // sheet.AddMergedRegion(new Region(0, 0, 0, dtSource.Columns.Count - 1)); //headerRow.Dispose(); } #endregion #region 列头及样式 { HSSFRow headerRow = (HSSFRow)sheet.CreateRow(1); HSSFCellStyle headStyle = (HSSFCellStyle)workbook.CreateCellStyle(); //headStyle.Alignment = CellHorizontalAlignment.CENTER; HSSFFont font = (HSSFFont)workbook.CreateFont(); font.FontHeightInPoints = 10; font.Boldweight = 700; headStyle.SetFont(font); foreach (DataColumn column in dtSource.Columns) { headerRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName); headerRow.GetCell(column.Ordinal).CellStyle = headStyle; //设置列宽 sheet.SetColumnWidth(column.Ordinal, (arrColWidth[column.Ordinal] + 1) * 256); } // headerRow.Dispose(); } #endregion rowIndex = 2; } #endregion #region 填充内容 HSSFRow dataRow = (HSSFRow)sheet.CreateRow(rowIndex); string dtname = row[1].ToString(); #region string sql = string.Format(@"select cc.COMMENTS, cC.COLUMN_NAME, CASE WHEN c.data_type='VARCHAR2' THEN '字符串型C' WHEN c.data_type='NUMBER' THEN '数值型N' WHEN c.data_type='CHAR' THEN '字符串型C' WHEN c.data_type='DATE' THEN '日期时间型T' WHEN c.data_type='FLOAT' THEN '数值型N' WHEN c.data_type='CLOB' THEN '长文本text' WHEN c.data_type='NCHAR' THEN '字符串型C' WHEN c.data_type='RAW' THEN '字符串型C' WHEN c.data_type='NVARCHAR2' THEN '字符串型C' END data_type , c.data_length ,'无条件共享' A1, '用户数据校核、业务协同及大数据应用' A2,'共享平台方式'A3,'数据库'A4,'否'A5 from user_tab_columns c LEFT JOIN user_col_comments cc ON cc.COLUMN_NAME=c.COLUMN_NAME AND CC.Table_Name='{0}' where c.Table_Name='{0}' order by c.COLUMN_NAME asc", dtname.ToUpper()); #endregion DataTable dd = helper.ExecuteDataSet(sql).Tables[0]; int kk = 0;//列控制 0到16列、27、28列 全部合并单元格 foreach (DataColumn column in dtSource.Columns) { //第一列合并第三行到第五行 0 1 2 3 4 //sheet.AddMergedRegion(new CellRangeAddress(2, 4, 0, 0)); if (dd.Rows.Count > 0 && kk < 17) { // 0到16列全部合并单元格 // 第KK列合并第2行到第dd.Rows.Count行 sheet.AddMergedRegion(new CellRangeAddress(rowIndex, rowIndex + dd.Rows.Count, kk, kk)); } if (kk > 26 && kk < 29) { //0到16列全部合并单元格 sheet.AddMergedRegion(new CellRangeAddress(rowIndex, rowIndex + dd.Rows.Count, kk, kk)); } HSSFCell newCell; string drValue; //if ((kk > 16 && kk < 27) || (kk > 26 && kk < 29)) //{ // int f = 0; // foreach (DataRow row1 in dd.Rows) // { // rowIndex++; // foreach (DataColumn column1 in dd.Columns) // { // newCell = (HSSFCell)dataRow.CreateCell(kk+column1.Ordinal); // drValue = row1[column1].ToString(); // switch (column1.DataType.ToString()) // { // case "System.String"://字符串类型 // newCell.SetCellValue(drValue); // break; // case "System.DateTime"://日期类型 // System.DateTime dateV; // System.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.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; // } // } // f++; // } //} //else { newCell = (HSSFCell)dataRow.CreateCell(column.Ordinal); drValue = row[column].ToString(); switch (column.DataType.ToString()) { case "System.String": //字符串类型 newCell.SetCellValue(drValue); break; case "System.DateTime": //日期类型 System.DateTime dateV; System.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.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; } } kk++; } #endregion rowIndex += dd.Rows.Count; rowIndex++; } using (MemoryStream ms = new MemoryStream()) { workbook.Write(ms); ms.Flush(); ms.Position = 0; // sheet.Dispose(); //workbook.Dispose();//一般只用写这一个就OK了,他会遍历并释放所有资源,但当前版本有问题所以只释放sheet return(ms); } }