public HSSFWorkbook exportNotice(HttpServletResponse response) throws IOException { Map<String, String> param = new HashMap<>(); List<XcNotice> list = queryForList("XcNoticeMapping.getNoticeList", param, XcNotice.class); HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet("题库"); HSSFFont font = wb.createFont(); font.setFontHeightInPoints((short) 12); font.setFontName("新宋体"); HSSFCellStyle style = wb.createCellStyle(); style.setFont(font); // 调用字体样式对象 style.setWrapText(true); style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中 style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中 style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框 style.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框 style.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框 style.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框 HSSFRow row = sheet.createRow(0); HSSFCell cell = row.createCell(0); cell.setCellStyle(style); cell.setCellValue("序号"); HSSFCell cell1 = row.createCell(1); cell1.setCellStyle(style); cell1.setCellValue("id"); HSSFCell cell2 = row.createCell(2); cell2.setCellStyle(style); cell2.setCellValue("巡察计划id"); HSSFCell cell3 = row.createCell(3); cell3.setCellStyle(style); cell3.setCellValue("公告标题"); sheet.setColumnWidth(0, 3766); sheet.setColumnWidth(1, 3766); sheet.setColumnWidth(2, 3766); sheet.setColumnWidth(3, 3766); for (int i = 0; i < list.size(); i++) { XcNotice xcNotice = list.get(i); if(xcNotice == null){ continue; } HSSFRow rowx = sheet.createRow(i + 1); rowx.setHeightInPoints(20); HSSFCell cell00 = rowx.createCell(0); cell00.setCellStyle(style); cell00.setCellValue(i + 1);// 序号 HSSFCell cell01 = rowx.createCell(1); cell01.setCellStyle(style); cell01.setCellValue(xcNotice.getId()); HSSFCell cell02 = rowx.createCell(2); cell02.setCellStyle(style); cell02.setCellValue(xcNotice.getXcjhId()); HSSFCell cell03 = rowx.createCell(3); cell03.setCellStyle(style); cell03.setCellValue(xcNotice.getTitle()); } //浏览器下载 OutputStream output = response.getOutputStream(); response.reset(); response.setHeader("Content-disposition", "attachment; filename=xcNotice.xls"); response.setContentType("application/msexcel"); wb.write(output); output.close(); return wb; }
Java Excel 导出
最新推荐文章于 2023-06-09 16:42:49 发布