1. 程式人生 > 其它 >使用springboot+angular匯出excel

使用springboot+angular匯出excel

技術標籤:angular

前端html

  <!--          匯出excel-->
              <div class="col-sm-1" >
                <button   style="" type="button" class="btn btn-warning q7" data-toggle="modal" data-target="" (click)="exportExcel()"
>
{{lang['universal.exportExcel']}} </button> </div>

前端ts


  exportExcel() {
    //使用window.location.href連結跳轉
    window.location.href = url() + '/dhTask/myExportExcel?time='+this.time+'&searchMsg='+this.searchMsg;
  }

後端程式碼

 //PC端匯出excel
    @RequestMapping
("/myExportExcel") public void pcExportExcel(HttpServletRequest request, HttpServletResponse response, String time, String searchMsg) { time = request.getParameter("time"); searchMsg = request.getParameter("searchMsg"); //獲取查詢後的任務list List<
Map<String, Object>> list = dhTaskService.pcGetBatchTaskMsg(searchMsg, time); List<Map<String, Object>> allList = new ArrayList<>(); //遍歷任務list獲取其詳細的資訊,並將其加到allList中 for (Map<String, Object> map : list) { Object production_task_id = map.get("production_task_id"); List<Map<String, Object>> partList = dhTaskService.getMsgByBatchTaskId(Long.parseLong(production_task_id.toString())); allList.addAll(partList); } //文件 HSSFWorkbook wb = new HSSFWorkbook(); //表名 HSSFSheet sheet = wb.createSheet("批次任務"); //第一行 HSSFRow row0 = sheet.createRow(0); row0.createCell(0).setCellValue("任務名稱"); row0.createCell(1).setCellValue("產品名稱"); row0.createCell(2).setCellValue("供應商名稱"); row0.createCell(3).setCellValue("等級"); row0.createCell(4).setCellValue("等級名稱"); row0.createCell(5).setCellValue("下限"); row0.createCell(6).setCellValue("上限"); row0.createCell(7).setCellValue("數量"); row0.createCell(8).setCellValue("重量"); row0.createCell(9).setCellValue("開始時間"); row0.createCell(10).setCellValue("結束時間"); String end_time=""; for (int i = 0; i < allList.size(); i++) { HSSFRow tempRow = sheet.createRow(i + 1); if(allList.get(i).get("production_task_name")!=null){ tempRow.createCell(0).setCellValue(allList.get(i).get("production_task_name").toString()); }else{ tempRow.createCell(0).setCellValue(""); } tempRow.createCell(1).setCellValue(allList.get(i).get("product_name").toString()); tempRow.createCell(2).setCellValue(allList.get(i).get("company_name").toString()); tempRow.createCell(3).setCellValue(allList.get(i).get("rank_num").toString()); tempRow.createCell(4).setCellValue(allList.get(i).get("rank_name").toString()); tempRow.createCell(5).setCellValue(allList.get(i).get("lower_limit").toString()); tempRow.createCell(6).setCellValue(allList.get(i).get("upper_limit").toString()); tempRow.createCell(7).setCellValue(allList.get(i).get("number").toString()); tempRow.createCell(8).setCellValue(allList.get(i).get("weight").toString()); tempRow.createCell(9).setCellValue(allList.get(i).get("start_time").toString()); if(allList.get(i).get("end_time")!=null){ tempRow.createCell(10).setCellValue(allList.get(i).get("end_time").toString()); }else{ tempRow.createCell(10).setCellValue(""); } } // 輸出Excel檔案 try { OutputStream output = response.getOutputStream(); response.setHeader("Content-Disposition", "attchement;filename=" + new String("批次任務".getBytes("utf-8"), "ISO-8859-1") + ".xls"); response.setContentType("application/msexcel"); wb.write(output); output.close(); } catch (IOException e) { } finally { if(wb!=null){ try { wb.close(); } catch (IOException e) { e.printStackTrace(); } } } } ```