导出excle文件 java_Java实现导出Excel文件

/** *

* Discription:[导出excel] *

*  * @param response * @param list 要导出的数据集合 * @param fileName * @param sheetName * @throws Exception */ public static void exportXls(HttpServletResponse response, List list, String fileName, String sheetName) throws Exception { if (list == null || list.size() == 0) { return; } // 获取总行数 int rowNum = list.size(); // 获得总列数 int cellNum = list.get(0).length; // 创建Excel文档 HSSFWorkbook hwb = new HSSFWorkbook(); // sheet对应一个工作页 HSSFSheet sheet = hwb.createSheet(sheetName); for (int i = 0; i < rowNum; i++) { // 创建一行 HSSFRow row = sheet.createRow(i); // 插入一行数据 for (int j = 0; j < cellNum; j++) { HSSFCell cell = row.createCell(j); cell.setCellValue(list.get(i)[j]); } } // 创建文件输出流,准备输出电子表格 ByteArrayOutputStream os = new ByteArrayOutputStream(); hwb.write(os); byte[] content = os.toByteArray(); InputStream is = new ByteArrayInputStream(content); 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; while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) { bos.write(buff, 0, bytesRead); } } catch (IOException e) { throw e; } finally { if (bis != null) bis.close(); if (bos != null) bos.close(); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值