zoukankan      html  css  js  c++  java
  • java-下载excel

    在java程序里面处理excel,我觉得比较方便的方式是先做出一个excel的模板(比如定义表头信息、表格名称等),然后根据这个模板往里面填充数据

    我这里演示的是使用poi处理2007以上版本的excel表格

    代码:

    String contextPath = session.getServletContext().getRealPath("/excel"); //获取模板路径
    XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(contextPath + "/schedule.xlsx")); //获取模板,并装成对应类
    XSSFSheet sheet = workbook.getSheetAt(0); //获取第一张表格
    int beginRow = 2;
    int endRow = 2 + doctorVOs.size();
    for(int i = 0;beginRow < endRow; beginRow++,i++){ DoctorCountVO vo = doctorVOs.get(i); XSSFRow row = sheet.getRow(beginRow); if(row == null){ row = sheet.createRow(beginRow); XSSFCell firstCell = row.createCell(0); firstCell.setCellValue(vo.getDoctorName()); XSSFCell secondCell = row.createCell(1); secondCell.setCellValue(vo.getOutCallFullDay()); XSSFCell thirdCell = row.createCell(2); thirdCell.setCellValue(vo.getOutCallHalfDay()); XSSFCell forthCell = row.createCell(3); forthCell.setCellValue(vo.getAccompanyFullDay()); XSSFCell fifthCell = row.createCell(4); fifthCell.setCellValue(vo.getAccompanyHalfDay()); } } ByteArrayOutputStream os = new ByteArrayOutputStream(); workbook.write(os); workbook.close(); return os.toByteArray();

    方法最后会放回byte[]数组,controller层通过response放回给客户端

    byte[] bytes = scheduleService.userCountExport(contextPath, doctorVOs);
                if(bytes != null){
                    response.setContentType("application/x-msdownload");
                    response.setHeader("Content-Disposition", "attachment;filename=schedule.xlsx");
                    response.setContentLength(bytes.length);
                    response.getOutputStream().write(bytes);
                    response.getOutputStream().flush();
                    response.getOutputStream().close();
                }
  • 相关阅读:
    luogu 2478 [SDOI2010]城市规划 仙人掌上dp.
    bzoj 3782 上学路线 卢卡斯定理 容斥 中国剩余定理 dp
    bzoj 3790 神奇项链 回文串 manacher|PAM
    4.4 相交弧 容斥 平衡规划 二维数点
    4.4 省选模拟赛 拉格朗日计数 树状数组+容斥.
    4.4 省选模拟赛 修路 斯坦纳树
    带修改线性基
    CF R 630 div2 1332 F Independent Set
    4.3 省选模拟赛 石子游戏 树上博弈
    机器C盘临时区
  • 原文地址:https://www.cnblogs.com/coderhuang/p/5178147.html
Copyright © 2011-2022 走看看