若依系统下载固定excel模板并填充数据

将固定模板放啊都项目路径中
若依系统下载固定excel模板并填充数据_第1张图片

模板样式:
若依系统下载固定excel模板并填充数据_第2张图片
若依系统下载固定excel模板并填充数据_第3张图片

导出方法:

 @Log(title = "作业底稿", businessType = BusinessType.EXPORT)
 @PostMapping("/export")
  @ResponseBody
  public AjaxResult export(TProjectAuditWordArchives tProjectAuditWordArchives) {
      String fileName = "作业底稿模板.xlsx";
      String realPath = String.valueOf(getClass().getProtectionDomain().getCodeSource().getLocation()).replace("file:","");
      // 文件在项目中路径
      String filePath =realPath +"/filetemplate/"+ fileName;
      FileInputStream fileInputStream = null;
      XSSFWorkbook workBook = null;
      OutputStream out = null;
      try {
          fileInputStream = new FileInputStream(filePath);  //  输入流
          workBook=new XSSFWorkbook(fileInputStream);
          XSSFSheet sheet0 = workBook.getSheetAt(0);
          XSSFSheet sheet1 = workBook.getSheetAt(1);
          // 写入数据
          List<TProjectAuditWordArchives> list = tProjectAuditWordArchivesService.selectTProjectAuditWordArchivesList(tProjectAuditWordArchives);
          for (int i = 0; i < list.size(); i++) {
          	  // 索引从0开始,模板中从第4行开始填充数据
              XSSFRow row = sheet0.createRow(3 + i);
              row.createCell(0).setCellValue(list.get(i).getId());
              row.createCell(1).setCellValue(list.get(i).getAuditCode());
              row.createCell(2).setCellValue(list.get(i).getAuditMatter());
              row.createCell(3).setCellValue(list.get(i).getArchivesId());
              row.createCell(4).setCellValue(list.get(i).getAuditProcessRecord());
              row.createCell(5).setCellValue(list.get(i).getAuditProcessResult());
          }
          for (int i = 0; i < 5; i++) {
              XSSFRow row = sheet1.createRow(3 + i);
              row.createCell(0).setCellValue(i);
              row.createCell(1).setCellValue(i);
              row.createCell(2).setCellValue(i);
              row.createCell(3).setCellValue(i);
              row.createCell(4).setCellValue(i);
              row.createCell(5).setCellValue(i);
          }
          ExcelUtil excelUtil = new ExcelUtil(null);
          out = new FileOutputStream(excelUtil.getAbsoluteFile(fileName));
          workBook.write(out);
      } catch (Exception e) {
          e.printStackTrace();
      }finally {
          if (null != fileInputStream) {
              try {
                  fileInputStream.close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
          if (null != out) {
              try {
                  out.close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
          if (null != workBook) {
              try {
                  workBook.close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
      return AjaxResult.success(fileName);
  }

返回成功后前端框架会自动调用下载文件请求:

 /**
  * 通用下载请求
  * 
  * @param fileName 文件名称
  * @param delete 是否删除
  */
 @GetMapping("common/download")
 public void fileDownload(String fileName, Boolean delete, HttpServletResponse response, HttpServletRequest request)
 {
     try
     {
         if (!FileUtils.checkAllowDownload(fileName))
         {
             throw new Exception(StringUtils.format("文件名称({})非法,不允许下载。 ", fileName));
         }
         String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
         String filePath = RuoYiConfig.getDownloadPath() + fileName;

         response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
         FileUtils.setAttachmentResponseHeader(response, realFileName);
         FileUtils.writeBytes(filePath, response.getOutputStream());
         if (delete)
         {
             FileUtils.deleteFile(filePath);
         }
     }
     catch (Exception e)
     {
         log.error("下载文件失败", e);
     }
 }

你可能感兴趣的:(java,java,poi)