Struts2 生成excel 并下载poi方式
strtus2 生成excel并下载(poi方式)
?
可以直接在流里生成并下载到本地。
if (sList != null) { int rows = 1;HSSFWorkbook wk = new HSSFWorkbook();HSSFSheet sheet = wk.createSheet("供求商机表");/*设置Excel单元格行高、列宽*/sheet.setDefaultColumnWidth((short)15);/*设置 字体 大小 颜色*/HSSFFont font = wk.createFont();font.setFontHeightInPoints((short)14);//字号font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 加粗font.setColor(HSSFColor.RED.index);//颜色/* 把font加载到样式里 */CellStyle style = wk.createCellStyle();style.setFont(font);//HSSFFont font1 = wk.createFont();font1.setFontHeightInPoints((short)12);//字号font1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 加粗font1.setColor(HSSFColor.BLACK.index);//颜色/* 把font加载到样式里 */CellStyle style1 = wk.createCellStyle();style1.setFont(font1);HSSFRow row = sheet.createRow(rows++);HSSFRow row0 = sheet.createRow(0);/* 合并单元格 */sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 4));HSSFCell cell0 = row0.createCell(0, HSSFCell.CELL_TYPE_STRING);cell0.setCellStyle(style);cell0.setCellValue(title);int column = 0;//"标 题", "联系人", "座 机", "手 机", "简 介HSSFCell cell = row.createCell(column++, HSSFCell.CELL_TYPE_STRING);cell.setCellValue("标 题");cell.setCellStyle(style1);cell = row.createCell(column++, HSSFCell.CELL_TYPE_STRING);cell.setCellValue("联系人");cell.setCellStyle(style1);cell = row.createCell(column++, HSSFCell.CELL_TYPE_STRING);cell.setCellValue("座 机");cell.setCellStyle(style1);cell = row.createCell(column++, HSSFCell.CELL_TYPE_STRING);cell.setCellValue("手 机");cell.setCellStyle(style1);cell = row.createCell(column++, HSSFCell.CELL_TYPE_STRING);cell.setCellValue("时 间");cell.setCellStyle(style1);cell = row.createCell(column++, HSSFCell.CELL_TYPE_STRING);cell.setCellValue("简 介");cell.setCellStyle(style1);for (SdProduct sdp : sList) {row = sheet.createRow(rows++);column = 0;cell = row.createCell(column++, HSSFCell.CELL_TYPE_NUMERIC);cell.setCellValue(sdp.getSdTitle().replaceAll("<[^>]*>",""));cell = row.createCell(column++, HSSFCell.CELL_TYPE_STRING);cell.setCellValue(sdp.getSdConName());cell = row.createCell(column++, HSSFCell.CELL_TYPE_STRING);cell.setCellValue(sdp.getSdTel());cell = row.createCell(column++, HSSFCell.CELL_TYPE_STRING);cell.setCellValue(sdp.getSdMobile());cell = row.createCell(column++, HSSFCell.CELL_TYPE_STRING);cell.setCellValue(sdp.getSdUpdate());cell = row.createCell(column++, HSSFCell.CELL_TYPE_STRING);cell.setCellValue(sdp.getSdComment().replaceAll("<[^>]*>","").replaceAll(" ", ""));}} else {System.out.println("统计数据不存在");}try {ByteArrayOutputStream out = new ByteArrayOutputStream();wk.write(out);requests.setAttribute("excelStream", new ByteArrayInputStream(out.toByteArray()));requests.setAttribute("filename", fileName);// 设置文件名return SUCCESS;} catch (Throwable th) {th.printStackTrace();System.out.println("无法输出Excel文件");return ERROR;}?
?