• 根据模板导出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     }
  • 相关阅读:
    【OpenCV学习笔记1】OpenCV 编程简介[轉]
    latex与word比较
    【C/C++语法外功】传值&传引用&传指针
    ellen 纽奥良大学演讲
    华侨大学50年校庆校长讲话
    【专题】工业相机接口
    【C/C++参考手册】C++资源之不完全导引[轉]
    比尔盖茨_哈佛演说
    拉里.埃里森_耶鲁大学演讲
    【OpenCV学习笔记2】OpenCV 完全安装 新增VS2010+OpenCV2.1,新增VS2010+OpenCV2.3.1
  • 原文地址:https://www.cnblogs.com/wihainan/p/6113478.html
Copyright © 2020-2023  润新知