java将数据保存到excle中浏览器下载

2019-08-01  本文已影响0人  叫我小码哥

maven依赖安排

<dependency>
      <groupId>net.sourceforge.jexcelapi</groupId>
      <artifactId>jxl</artifactId>
      <version>2.6.12</version>
    </dependency>
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml</artifactId>
      <version>3.15-beta2</version>
    </dependency>

代码安排

public static String getExcle(List<User> userArray,HttpServletResponse response) throws Exception{
        //1.创建HSSFWorkbook 对象
        HSSFWorkbook wb = new HSSFWorkbook();
        // 2.在workbook中添加一个sheet,对应Excel中的一个sheet
        HSSFSheet sheet = wb.createSheet("user");
        // 3.在sheet中添加表头第0行,老版本poi对excel行数列数有限制short
        HSSFRow row = sheet.createRow((int) 0);
        // 4.创建单元格,设置值表头,设置表头居中
        HSSFCellStyle style = wb.createCellStyle();
        // 居中格式
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

        // 5. 设置表头
        HSSFCell cell = row.createCell(0);
        cell.setCellValue("姓名");
        cell.setCellStyle(style);

        cell = row.createCell(1);
        cell.setCellValue("Id");
        cell.setCellStyle(style);

        cell = row.createCell(2);
        cell.setCellValue("地址");
        cell.setCellStyle(style);

        cell = row.createCell(3);
        cell.setCellValue("电话");
        cell.setCellStyle(style);

        cell = row.createCell(4);
        cell.setCellValue("性别");
        cell.setCellStyle(style);
        // 6.设置excle内容
        for (int i = 0; i <userArray.size(); i++) {
            row = sheet.createRow((int) i + 1);
             User user = userArray.get(i);
            // 创建单元格,设置值
            row.createCell(0).setCellValue(user.getUserName());
            row.createCell(1).setCellValue(user.getUserId());
            row.createCell(2).setCellValue(user.getUserAddress());
            row.createCell(3).setCellValue(user.getUserPhone());
            row.createCell(4).setCellValue(user.getUserSex());
        }
        String fileName = "user";
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        wb.write(os);
        byte[] content = os.toByteArray();
        InputStream is = new ByteArrayInputStream(content);
        // 设置response参数,可以打开下载页面
        response.reset();
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename="
                + new String((fileName + ".xls").getBytes(), "iso-8859-1"));
        ServletOutputStream out = response.getOutputStream();
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;

        try {
            bis = new BufferedInputStream(is);
            bos = new BufferedOutputStream(out);
            byte[] buff = new byte[2048];
            int bytesRead;
            // Simple read/write loop.
            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                bos.write(buff, 0, bytesRead);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bis != null)
                bis.close();
            if (bos != null)
                bos.close();
        }
        return "excle已成功下载";
    }
上一篇 下一篇

猜你喜欢

热点阅读