public static void createConditionH(HSSFSheet sheet, int row, int lscol, string condition, HSSFCellStyle style, int height) { HSSFRow hsshRow = sheet.CreateRow(row) as HSSFRow; hsshRow.Height = (short)(height * 256); for (int i = 0; i < lscol; i++) { HSSFCell cell = hsshRow.CreateCell(i) as HSSFCell; cell.CellStyle = style; if (i == 0) { HSSFRichTextString richString; if (condition == null) { richString = new HSSFRichTextString(""); } else { richString = new HSSFRichTextString(condition); } cell.SetCellValue(richString); } else { cell.SetCellValue(""); } } ExcelUtilsForNPOI.SetCellRangeAddress(sheet, row, row, 0, lscol); }
/// <summary> /// Excel生成 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="wbName">导出的Excel的名称</param> /// <param name="condition">拼接好的筛选条件</param> /// <param name="title">表头数组string[]</param> /// <param name="data">要导出的字段的数组</param> /// <param name="dataList">要导出的实体数据List<T></param> /// <param name="colWidth">列的宽度的数组int[]</param> /// <param name="flag">是否含有序号列flag</param> public static void excelExport <T>(string wbName, string condition, string[] title, string[] fields, IList <T> dataList, int[] colWidth, bool flag) { HSSFWorkbook book = new HSSFWorkbook(); HSSFSheet sheet = book.CreateSheet(wbName) as HSSFSheet; // HSSFCellStyle conditionStyle = ExcelUtils.getConditionStytle(book);//条件样式 HSSFCellStyle titleStyle = ExcelUtilsForNPOI.getTitleStytle(book); //表头样式 HSSFCellStyle bodyStyle = ExcelUtilsForNPOI.getBodyStytle(book); //数据样式 //填充条件 // ExcelCommonUtils.createCondition(sheet, 0, title.Length - 1, condition, conditionStyle); //填充表头 ExcelBaseForNPOI.createTitle(sheet, 0, title, titleStyle); //填充数据 ExcelBaseForNPOI.setBodyValue(sheet, 1, dataList, fields, bodyStyle, flag); for (int i = 0; i < colWidth.Length; i++) { sheet.SetColumnWidth(i, colWidth[i] * 256); } MemoryStream mes = new MemoryStream(); book.Write(mes); System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xls", string.IsNullOrEmpty(wbName) ? "" : "ex-file")); System.Web.HttpContext.Current.Response.BinaryWrite(mes.ToArray()); }