##poi excel导出案例
1.ajxa导出请求没有任何反应,打断点看了workBook中也有数据,网上查阅说ajax请求导出无法接收流,换成location.href,果然可以了
2.控制器代码
response.setCharacterEncoding("UTF-8");response.setContentType("application/vnd.ms-excel");String encodedFilename = "attachment; filename*=UTF-8''" + URLEncoder.encode(filename, "UTF-8") + ".xls";response.setHeader("Content-Disposition", encodedFilename);HSSFWorkbook workbook = new HSSFWorkbook();if("00A".equals(docType)){workbook = docExportService.getDocInDetailsToExport(filename, workbook, this.getSysOrg().getOrgId(), startTime, endTime);}else{workbook = docExportService.getDocOutDetailsToExport(filename, workbook, this.getSysOrg().getOrgId(), startTime, endTime);}OutputStream outputStream = response.getOutputStream();workbook.write(outputStream);outputStream.flush();outputStream.close();
2.excel合并处理数据
try {HSSFSheet sheet = workbook.createSheet(filename);// 列头的样式HSSFFont columnHeadFont = workbook.createFont();columnHeadFont.setFontName("宋体");columnHeadFont.setFontHeightInPoints((short) 10);columnHeadFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);HSSFCellStyle columnHeadStyle = workbook.createCellStyle();columnHeadStyle.setFont(columnHeadFont);columnHeadStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);// 左右居中columnHeadStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中columnHeadStyle.setLocked(true);columnHeadStyle.setWrapText(true);columnHeadStyle.setLeftBorderColor(HSSFColor.BLACK.index);// 左边框的颜色columnHeadStyle.setBorderLeft((short) 1);// 边框的大小columnHeadStyle.setRightBorderColor(HSSFColor.BLACK.index);// 右边框的颜色columnHeadStyle.setBorderRight((short) 1);// 边框的大小columnHeadStyle.setTopBorderColor(HSSFColor.BLACK.index);columnHeadStyle.setBorderTop((short) 1);columnHeadStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体columnHeadStyle.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色// 设置单元格的背景颜色(单元格的样式会覆盖列或行的样式)columnHeadStyle.setFillForegroundColor(HSSFColor.WHITE.index);//标题样式HSSFFont headfont = workbook.createFont();headfont.setFontName("黑体");headfont.setFontHeightInPoints((short) 22);// 字体大小headfont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 加粗HSSFCellStyle headstyle = workbook.createCellStyle();headstyle.setFont(headfont);headstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中headstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中headstyle.setLocked(true);headstyle.setWrapText(true);// 自动换行//表头样式HSSFCellStyle headstyle0 = workbook.createCellStyle();headstyle0.setFont(headfont);headstyle0.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中headstyle0.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中headstyle0.setLeftBorderColor(HSSFColor.BLACK.index);// 左边框的颜色headstyle0.setBorderLeft((short) 1);// 边框的大小headstyle0.setRightBorderColor(HSSFColor.BLACK.index);// 右边框的颜色headstyle0.setBorderRight((short) 1);// 边框的大小headstyle0.setTopBorderColor(HSSFColor.BLACK.index);headstyle0.setBorderTop((short) 1);headstyle0.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体headstyle0.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色// 设置单元格的背景颜色(单元格的样式会覆盖列或行的样式)headstyle0.setFillForegroundColor(HSSFColor.GREY_80_PERCENT.index);String [] fields={"序号","登记时间","备注"};//创建表头HSSFRow header = sheet.createRow(0);header.setHeight((short) 1000);Region region = new Region(0, (short) 0, 0, (short) (fields.length-1));sheet.addMergedRegion(region);HSSFCell hc = header.createCell(0);hc.setCellValue(new HSSFRichTextString("数据统计"));hc.setCellStyle(headstyle0);//创建表头HSSFRow row = sheet.createRow(1);row.setHeight((short) 500);for (int i=0;i<fields.length;i++){HSSFCell cellA = row.createCell(i);cellA.setCellValue(fields[i]);cellA.setCellStyle(columnHeadStyle);sheet.setColumnWidth(i,4500);}Page pager=xxxService.getInxxxList(orgId,startTime,endTime, 1, Integer.MAX_VALUE);//添加数据行List<xxx > inList = (List<xxx >) pager.getData();int row0=2;for (int rowIndex = 1; rowIndex <= inList.size(); rowIndex++) {xxx detail = inList.get(rowIndex - 1);HSSFRow dataRow = sheet.createRow(row0);dataRow.setHeight((short)500);for(int m=0;m<fields.length;m++){HSSFCell cell = dataRow.createCell(m);String tempString = "";if (fields[m].equals("序号")) {tempString = String.valueOf(rowIndex);}if (fields[m].equals("登记时间")) {tempString = detail.getCreateDate();}if (fields[m].equals("备注")) {tempString ="";//todo 现在无值}cell.setCellValue(new HSSFRichTextString(tempString));cell.setCellStyle(columnHeadStyle);}row0++;}} catch (Exception e) {e.printStackTrace();}return workbook;
4.展示结果: