JavaWeb项目导出Excel文件并弹出下载框

来源:互联网 发布:淘宝造物节h5 源码 编辑:程序博客网 时间:2024/05/17 07:41

引言

在Java Web开发中经常涉及到报表,最近做的项目中需要实现将数据库中的数据显示为表格,并且实现导出为Excel文件的功能。

相关jar包

使用POI可以很好的解决Excel的导入和导出的问题,POI下载地址:
poi-3.6-20091214.jar

关键代码

首先导入上述jar包。
在生成excel时一般数据源形式为一个List,下面把生成Excel格式的代码贴出来:

/**             * 以下为生成Excel操作             */            // 1.创建一个workbook,对应一个Excel文件            HSSFWorkbook wb = new HSSFWorkbook();            // 2.在workbook中添加一个sheet,对应Excel中的一个sheet            HSSFSheet sheet = wb.createSheet("XXX表");            // 3.在sheet中添加表头第0行,老版本poi对excel行数列数有限制short            HSSFRow row = sheet.createRow((int) 0);            // 4.创建单元格,设置值表头,设置表头居中            HSSFCellStyle style = wb.createCellStyle();            // 居中格式            style.setAlignment(HSSFCellStyle.ALIGN_CENTER);            // 设置表头            HSSFCell cell = row.createCell(0);            cell.setCellValue("表头1");            cell.setCellStyle(style);            cell = row.createCell(1);            cell.setCellValue("表头2");            cell.setCellStyle(style);            cell = row.createCell(2);            cell.setCellValue("表头3");            cell.setCellStyle(style);            cell = row.createCell(3);            cell.setCellValue("表头4");            cell.setCellStyle(style);            cell = row.createCell(4);            cell.setCellValue("表头5");            cell.setCellStyle(style);

生成excel格式后要将数据写入excel:

// 循环将数据写入Excel            for (int i = 0; i < lists.size(); i++) {                row = sheet.createRow((int) i + 1);                List list= lists.get(i);                // 创建单元格,设置值                row.createCell(0).setCellValue(list.getXXX());                row.createCell(1).setCellValue(list.getXXX());                row.createCell(2).setCellValue(list.getXXX());                row.createCell(3).setCellValue(list.getXXX());                row.createCell(4).setCellValue(list.getXXX());            }

之后将生成的Excel以流输出。
*不弹出下载框

FileOutputStream out =new FileOutputStream("E:/XXX.xls");wb.write(out); out.close();

*弹出下载框

String fileName = "XXX表";            ByteArrayOutputStream os = new ByteArrayOutputStream();            wb.write(os);            byte[] content = os.toByteArray();            InputStream is = new ByteArrayInputStream(content);            // 设置response参数,可以打开下载页面            res.reset();            res.setContentType("application/vnd.ms-excel;charset=utf-8");            res.setHeader("Content-Disposition", "attachment;filename="                    + new String((fileName + ".xls").getBytes(), "iso-8859-1"));            ServletOutputStream out = res.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) {                // TODO: handle exception                e.printStackTrace();            } finally {                if (bis != null)                    bis.close();                if (bos != null)                    bos.close();            }

完成以上操作之后即可跳转到其他页面。
同时POI还可以将Excel上传解析显示在网页中,这个另一篇文章总结。

2 2