vue javaバックグラウンドExcel互換ieのエクスポート

4315 ワード

//js
export function exportExcel(params,url) {
  let form = document.createElement("form")
  let  cloneObj=function(obj) {
    let newObj = {};
    if (obj instanceof Array) {
      newObj = [];
    }
    for (let key in obj) {
      let val = obj[key];
      newObj[key] = typeof val === 'object' ? cloneObj(val): val;
    }
    return newObj;
  }
  let submitParams = cloneObj(params);
  form.style.display = 'none'
  form.action = process.env.VUE_APP_BASE_API + url
  form.method = "post"
  form.target="_blank"
  document.body.appendChild(form)
  for(let key in submitParams){
    let input = document.createElement("input")
    input.type = "hidden"
    input.name = key
    input.value = submitParams[key]
    form.appendChild(input)
  }
  form.submit()
  form.remove()
}

//java
    @RequestMapping("/getExcel.do")     @ResponseBody     public void getStartEndExcel(HttpServletRequest request, HttpServletResponse response) throws Exception{         String reportDate = request.getParameter("reportDateStr");         SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");         String year = reportDate.substring(0,4);         String month = reportDate.substring(4, 6);         reportDate=StringUtils.isBlank(reportDate)?sdf.format(new Date()):reportDate;         List  list = this.getlist(reportDate,year,month);         String[] headers = {"beanName","beanName","beanName","beanName","beanName","beanName","beanName","beanName","beanName"};
        Workbook book  = new HSSFWorkbook(ExcelUtil.getExcelFile("excelmodel/Excel.xls"));         Sheet sheet = book.getSheetAt(0);         Row excelRow = sheet.getRow(0); String file Name="テーブル("+reportDate+");        CellStyle TitleStyle = ExcelUtil.getCellStyle(book, (short)26, true, true, true,true);         CellStyle cellStyle = ExcelUtil.getCellStyle(book, (short)10, false, true, true,true);         ExcelUtil.createCellAndSetValue(excelRow, TitleStyle, 0, fileName);         this.commonStringExcelSetCellValue(sheet, cellStyle, 3, list, headers, false);         this.commonResponseWrite(response, fileName, book);     }
protected void commonStringExcelSetCellValue(Sheet sheet, CellStyle cellStyle, int rowIdx, List list, String[] fields, boolean needIndex, Integer inputIndex){         int index = inputIndex == null?1:inputIndex;         DozerBeanMapper dozerBeanMapper = RuntimeApplicationContext.getBean(DozerBeanMapper.class);         for (Object obj : list) {             Map map = null;             if (obj instanceof Map) {                 map = (Map) obj;             } else {                 map = dozerBeanMapper.map(obj, Map.class);             }             Row row = sheet.createRow(rowIdx++);             int colIdx=0;             for (int i = 0; i < fields.length; i++) {                 Cell cell = row.createCell(i);                 cell.setCellStyle(cellStyle);                 Object value;                  if(i==0 && needIndex) {                     value = index++;                 }else {                     value = map.get(fields[i]);                     if ("null".equals(value)||value==null) {                         value = "";                     }                 }                 if (value instanceof BigDecimal) {                     cell.setCellValue(((BigDecimal)value).doubleValue());                 }else if(value instanceof Integer){                     cell.setCellValue(((Integer)value).doubleValue());                 }else{                     cell.setCellValue(""+value);                 }             }         }     }
         protected void commonResponseWrite(HttpServletResponse response, String fileName, Workbook book) throws Exception {         OutputStream out;         fileName = new String(fileName.getBytes(),"ISO8859-1");           response.setContentType("application/x-msdownload");         response.setHeader("Content-Disposition", "attachment;filename="+fileName+".xls");         out = response.getOutputStream();         book.write(out);         out.flush();         out.close();     }