private void downLoadFile(@RequestParam String jsonStr, HttpServletResponse response) { //轉碼 jsonStr = Utils.decodeURL(jsonStr); //將json字符傳轉換爲json對象 JSONObject jsonObj = JSONObject.parseObject(jsonStr); //拼出文件名 String name="雲端對賬賬單"+".xlsx"; //通過查詢條件 List<CrdStaInterfacePrices> Billlist = crdStaInterfacePricesService.findBill(jsonObj.getString("startTime"), jsonObj.getString("endTime")); //創建一個xlxs文件 SXSSFWorkbook wb = new SXSSFWorkbook(); XSSFCellStyle style = (XSSFCellStyle) wb.createCellStyle(); Color c = new Color(147, 208, 15); XSSFColor xssfColor = new XSSFColor(c); style.setFillPattern(FillPatternType.SOLID_FOREGROUND); style.setFillForegroundColor(xssfColor); //往excel裏面寫數據 buildSheet(wb, Billlist, style); response.setContentType("application/x-xls"); response.setHeader("Content-Disposition", "attachment; filename=" + fileName); OutputStream outputStream = null; try { outputStream = response.getOutputStream(); wb.write(outputStream); outputStream.flush(); outputStream.close(); } catch (IOException e) { log.error("文件導出失敗" + e); } }
/** * @return * @throws * @description 根據數據生成excel */ private void buildSheet(SXSSFWorkbook wb, List<CrdStaInterfacePrices> BillList, XSSFCellStyle style) { Sheet sheet = wb.createSheet("sheet1"); Row row = sheet.createRow(0); //寫表頭 setTitle(row, style); //寫數據 setData(sheet, style, BillList); }
private void setData(Sheet sheet, XSSFCellStyle style, List<CrdStaInterfacePrices> BillList) { //循環將查詢數據庫獲取到的數據寫入excel文件 for (int i = 0; i < BillList.size(); i++) { Row row = sheet.createRow(i + 1); CrdStaInterfacePrices crdStaInterfacePrices = BillList.get(i); Cell cel0 = row.createCell(0); cel0.setCellValue("子產品"); Cell cel1 = row.createCell(1); cel1.setCellValue("用戶評估報告"); Cell cel2 = row.createCell(2); cel2.setCellValue(""); Cell cel3 = row.createCell(3); cel3.setCellValue(""); Cell cel4 = row.createCell(4); cel4.setCellValue(crdStaInterfacePrices.getProdName()); Cell cel5 = row.createCell(5); cel5.setCellValue(crdStaInterfacePrices.getProdCode()); Cell cel6 = row.createCell(6); cel6.setCellValue("是"); Cell cel7 = row.createCell(7); cel7.setCellValue(crdStaInterfacePrices.getProTypename().substring(0,2)); Cell cel8 = row.createCell(8); cel8.setCellValue(crdStaInterfacePrices.getProTypename().substring(crdStaInterfacePrices.getProTypename().length()-2,crdStaInterfacePrices.getProTypename().length())); Cell cel9 = row.createCell(9); cel9.setCellValue(crdStaInterfacePrices.getNum()); Cell cel10 = row.createCell(10); cel10.setCellValue("0"); Cell cel11 = row.createCell(11); cel11.setCellValue(crdStaInterfacePrices.getNum()); Cell cel12 = row.createCell(12); cel12.setCellValue(crdStaInterfacePrices.getPrice()); Cell cel13 = row.createCell(13); cel13.setCellValue("0"); Cell cel14 = row.createCell(14); cel14.setCellValue("0"); Cell cel15 = row.createCell(15); Double price=crdStaInterfacePrices.getPrice(); if(price.compareTo(0.0)==0){ cel15.setCellValue("0.0"); }else { cel15.setCellValue(crdStaInterfacePrices.getSumPrices()); } } }
/** * @param style * @return * @throws * @description 第一行填寫表頭 */ public void setTitle(Row row, XSSFCellStyle style) { Cell cell = row.createCell(0); cell.setCellValue("子產品/打包N"); cell.setCellStyle(style); Cell cell1 = row.createCell(1); cell1.setCellValue("產品類型"); cell1.setCellStyle(style); Cell cell2 = row.createCell(2); cell2.setCellValue("數據來源"); cell2.setCellStyle(style); Cell cell3 = row.createCell(3); cell3.setCellValue("運營商"); cell3.setCellStyle(style); Cell cell4 = row.createCell(4); cell4.setCellValue("產品名稱(中文)"); cell4.setCellStyle(style); Cell cell5 = row.createCell(5); cell5.setCellValue("產品名稱(英文)"); cell5.setCellStyle(style); Cell cell6 = row.createCell(6); cell6.setCellValue("是否計費"); cell6.setCellStyle(style); Cell cell7 = row.createCell(7); cell7.setCellValue("計費邏輯1"); cell7.setCellStyle(style); Cell cell8 = row.createCell(8); cell8.setCellValue("計費邏輯2"); cell8.setCellStyle(style); Cell cell9 = row.createCell(9); cell9.setCellValue("計費量"); cell9.setCellStyle(style); Cell cell10 = row.createCell(10); cell10.setCellValue("衝減量"); cell10.setCellStyle(style); Cell cell11 = row.createCell(11); cell11.setCellValue("實際計費量"); cell11.setCellStyle(style); Cell cell12 = row.createCell(12); cell12.setCellValue("單價"); cell12.setCellStyle(style); Cell cell13 = row.createCell(13); cell13.setCellValue("免費期"); cell13.setCellStyle(style); Cell cell14 = row.createCell(14); cell14.setCellValue("免費條數"); cell14.setCellStyle(style); Cell cell15 = row.createCell(15); cell15.setCellValue("本期小計"); cell15.setCellStyle(style); }