如題,模板如下:
根據該模板填寫相應的行和列導出數據,
直接上代碼:

//(填寫實際路徑) ,我這里是點擊事件,就不放傳參數的代碼了 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...