zoukankan      html  css  js  c++  java
  • 根据模板导出excel

     1     @RequestMapping(value = "/export", method = RequestMethod.GET)
     2     public void exportApprovalsLog(
     3             @RequestParam(value = "department", required = false) String department,
     4             @RequestParam(value = "approver_ids", required = false) List<String> approverIds,
     5             @RequestParam(value = "start_time", required = false) Long startTime,
     6             @RequestParam(value = "end_time", required = false) Long endTime,
     7             @RequestParam(value = "case_code", required = false) String caseCode,
     8             HttpServletRequest request, HttpServletResponse response) {
     9 
    10         if (CollectionUtils.isEmpty(approverIds)) {
    11             approverIds = ImmutableList.of("777");
    12         }
    13 
    14         List<ApproveLog> list = service.getApprovalsLog(department, approverIds, startTime, endTime, caseCode, null,null);
    15 
    16         try {
    17             String path = request.getServletContext().getRealPath("")
    18                     + "/excelTemplate/审核记录.xls";   //这个是我的excel模板 根目录是wabapp
    19             InputStream in = new FileInputStream(new File(path));
    20             HSSFWorkbook work = new HSSFWorkbook(in);
    21             HSSFSheet sheet = work.getSheetAt(0);  // 获取sheet
    22             // 插入数据
    23             for (int i = 1; i < list.size() + 1; i++) { // 表头占1行 i=1; 表头占2行 i=2
    24                 ApproveLog log = list.get(i - 1);
    25                 HSSFRow row = sheet.createRow(i);
    26 
    27                 HSSFCell cell = row.createCell(0);
    28                 cell.setCellValue(log.getCaseId());
    29                 cell1 = row.createCell(1);
    30                 cell.setCellValue(log.getCaseName());
    31                 // 其他数据
    32             }
    33 
    34             String fileName = ExportExcelUtils.makeFileName("审核记录", department,
    35                     ExportExcelUtils.getNamesByIds(constantService, approverIds), startTime, endTime);
    36             OutputStream os = response.getOutputStream();// 取得输出流
    37             response.setCharacterEncoding("UTF-8");
    38             response.setContentType("application/vnd.ms-excel");
    39             response.setHeader("Content-disposition",
    40                     "attachment;filename=" + new String(fileName.getBytes("UTF-8"), "iso8859-1") + ".xls"); // 防止中文乱码
    41             work.write(os);
    42             os.close();
    43         } catch (FileNotFoundException e) {
    44             throw new BizException(ErrorCode.DATA_NOT_EXIST, "文件路径错误");
    45         } catch (IOException e) {
    46             e.printStackTrace();
    47             throw new BizException(ErrorCode.DATA_NOT_EXIST, "文件输入流错误");
    48         }
    49     }
  • 相关阅读:
    Selenium上传文件方法总结
    Maven安装配置
    selenium 3+java 配置全
    使用Eclipse创建Maven项目
    maven 学习
    解决Tomcatt下连接数据库的classNoFount问题
    Cookie的使用
    登录验证
    Servlet概述
    计算机科学学院静态网页
  • 原文地址:https://www.cnblogs.com/wihainan/p/6113478.html
Copyright © 2011-2022 走看看