Spring Boot使用POI 根据EXCEL模板替换导出
如题,模板如下:
根据该模板填写相应的行和列导出数据,
直接上代码:
//(填写实际路径) ,我这里是点击事件,就不放传参数的代码了 var url = "sell/order/PortExecl?id="+id; $('<form method="post" target="_blank" action="' + url + '"></form>').appendTo('body').submit().remove();
@RequestMapping("/xxx") @ResponseBody public void OrderCoatingPortExecl(HttpServletResponse response,String id) { try { response.setContentType("application/octet-stream;charset=ISO8859-1"); //导出的文件名 (DateUtil为我的时间工具类,可以根据时分秒来进行导出防止文件名重复) response.setHeader("Content-Disposition", "attachment;filename="+ new String(("Execl表"+ DateUtil.toStringXiumaDateTime(new Date())).getBytes("gb2312"), "ISO8859-1") + ".xls"); response.addHeader("Pargam", "no-cache"); response.addHeader("Cache-Control", "no-cache"); } catch (UnsupportedEncodingException e) { //忽略异常 } //导出需要的数据 OrderDetailVo orderlist = orderService.selectOrderByIdEeditionExecl(id); List<OrderDetailVo>detailist = orderService.selectOrderLstByIdEeditionExecl(id); try { //把数据传到具体导出的业务逻辑层 orderService.OrderCoatingPortExecl(orderlist,detailist, response); }catch (Exception e){ e.printStackTrace(); } }
@Override public void OrderCoatingPortExecl(List<OrderDetailVo> orders,List<OrderDetailVo> detaillist, HttpServletResponse response) throws IOException { OutputStream os = null; try{ os = response.getOutputStream(); OrderDetailVo order=orders.get(0); //excel模板路径 (这里获取resources下的文件) File fi = ResourceUtils.getFile("classpath:static/excel/销售涂层模板.xls"); POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(fi)); //读取excel模板 HSSFWorkbook wb = new HSSFWorkbook(fs); //创建列的样式 (无虚线样式) HSSFCellStyle cellStyle = wb.createCellStyle(); //设置居中 cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平居中 cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 上下居中 // (虚线样式) HSSFCellStyle dotlinecellStyle = wb.createCellStyle(); //设置居中 dotlinecellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平居中 dotlinecellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 上下居中 // BORDER_DOTTED : 虚线 BORDER_THIN:实线 dotlinecellStyle.setBorderBottom(dotlinecellStyle.BORDER_DOTTED);// 下边框 dotlinecellStyle.setBorderLeft(dotlinecellStyle.BORDER_DOTTED);// 左边框 dotlinecellStyle.setBorderRight(dotlinecellStyle.BORDER_DOTTED);// 右边框 dotlinecellStyle.setBorderTop(dotlinecellStyle.BORDER_DOTTED);// 上边框 //设置字体: HSSFFont font = wb.createFont(); font.setFontHeightInPoints((short) 10);//设置字体大小 font.setFontName("宋体"); //设置字体:宋体 cellStyle.setFont(font); //选择需要用到的字体格式 //读取模板内所有sheet内容 HSSFSheet sheet = wb.getSheetAt(0); //在相应的单元格进行赋值 (行 (getRow) 与列 (getCell) 都是从0开始)、单号 HSSFCell cell0 = sheet.getRow(2).getCell(1); cell0.setCellValue(order.getOrderNum()); cell0.setCellStyle(cellStyle); //开单日期 HSSFCell cell1 = sheet.getRow(3).getCell(1); cell1.setCellValue(order.getOrderTime()); cell1.setCellStyle(cellStyle); //预交货日期 HSSFCell cell2 = sheet.getRow(3).getCell(5); cell2.setCellValue(order.getDeliveryTime()); cell2.setCellStyle(cellStyle); //客户 HSSFCell cell3 = sheet.getRow(4).getCell(1); cell3.setCellValue(order.getCustomerName()); cell3.setCellStyle(cellStyle); //合计变量 Double totalKg=0.0; //明细,需要循环生成 for (int i=0;i<detaillist.size();i++){ //找到需要插入的行数(生成新的一行), 创建新的ROW (6为固定的明细表头) sheet.shiftRows((6+i),sheet.getLastRowNum(), 1,true,false); //新建一个POI的row对象, 从第几行开始 Row row = sheet.createRow((6+i)); //序号 Cell listcell0 = row.createCell(0); listcell0.setCellValue(String.valueOf(i+1)); listcell0.setCellStyle(dotlinecellStyle); //规格 Cell listcell1 = row.createCell(1); if (detaillist.get(i).getAluminumSpecification()!=null && !"".equals(detaillist.get(i).getAluminumSpecification())){ listcell1.setCellValue(detaillist.get(i).getAluminumSpecification()); }else { listcell1.setCellValue(""); } listcell1.setCellStyle(dotlinecellStyle); //合金状态 Cell listcell2 = row.createCell(2); if (detaillist.get(i).getAluminumMetal()!=null && !"".equals(detaillist.get(i).getAluminumMetal())){ listcell2.setCellValue(detaillist.get(i).getAluminumMetal()); }else { listcell2.setCellValue(""); } listcell2.setCellStyle(dotlinecellStyle); //卷号 Cell listcell3 = row.createCell(3); if (detaillist.get(i).getOriginalAluminumNo()!=null && !"".equals(detaillist.get(i).getOriginalAluminumNo())){ listcell3.setCellValue(detaillist.get(i).getOriginalAluminumNo()); }else { listcell3.setCellValue(""); } listcell3.setCellStyle(dotlinecellStyle); //卷数 Cell listcell4 = row.createCell(4); listcell4.setCellValue("1"); listcell4.setCellStyle(dotlinecellStyle); //数量 Cell listcell5 = row.createCell(5); if (detaillist.get(i).getAluminumWeight()!=null && !"".equals(detaillist.get(i).getAluminumWeight())){ listcell5.setCellValue(detaillist.get(i).getAluminumWeight()); totalKg+=Double.valueOf(detaillist.get(i).getAluminumWeight()); }else { listcell5.setCellValue(""); } listcell5.setCellStyle(dotlinecellStyle); //备注 Cell listcell6 = row.createCell(6); if (detaillist.get(i).getComment()!=null && !"".equals(detaillist.get(i).getComment())){ listcell6.setCellValue(detaillist.get(i).getComment()); }else { listcell6.setCellValue(""); } listcell6.setCellStyle(dotlinecellStyle); } //备注 //这里 6 + 明细的长度 +2 为: 6是上面固定的行(从0开始), 长度是已经在Execl中插入了多少行, 3 为预留的行, 所以都加起来,就是下面的行 HSSFCell cell4 = sheet.getRow((6+detaillist.size()+3)).getCell(2); if (order.getRemark()!=null&&!"".equals(order.getRemark())){ cell4.setCellValue("111"); }else { cell4.setCellValue(""); } cell4.setCellStyle(cellStyle); //输出模板 wb.write(os); }catch (Exception e){ e.printStackTrace(); }finally { if (os != null) { os.close(); } } }
导出成功:
End...