数据大于设置的最大数据则导出多个文件压缩后返回"/>
导出数据大于设置的最大数据则导出多个文件压缩后返回
1. 导出
/*** 导出*/ private void export(HttpServletResponse response, List<T> list, String tagName, String templateName) throws Exception {String fileName = tagName + '_' + DateUtils.format(new Date(), DateUtils.DATE_PATTERN);int total = list.size();if(total <= BusinessConstant.EXPORT_MAX_SIZE) {exportExcelService.exportExcel(response, fileName + ".xlsx", templateName, list);return ;}List<T> newList = new ArrayList<>();List<byte[]> bytesList = new ArrayList<>();int i = 0;while (total > 0) {if (i + BusinessConstant.EXPORT_MAX_SIZE > list.size()) {newList = list.subList(i, list.size());} else {newList = list.subList(i, i + BusinessConstant.EXPORT_MAX_SIZE);}byte[] bytes = ExportExcelServiceImpl.exportExcelBytes(templateName, newList);bytesList.add(bytes);i = i + BusinessConstant.EXPORT_MAX_SIZE;total = total - BusinessConstant.EXPORT_MAX_SIZE;}// 批量文件流压缩ExportExcelServiceImpl.exportExcelByZip(response, fileName, bytesList); }
2. 直接导出单个文件
/*** @param response 响应* @param fileName 返回excel的文件名* @param templatePath 模板路径* @param data 表数据内容* @throws Exception*/ public void exportExcel(HttpServletResponse response, String fileName, String templatePath, List data) throws Exception {response.setContentType("application/vnd.ms-excel;charset=utf-8");response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(StandardCharsets.UTF_8), "ISO8859-1"));OutputStream outputStream = response.getOutputStream();InputStream inputStream = ExportExcelController.class.getResourceAsStream(templatePath);ExcelWrite excelWrite = new JxlsExcelWrite(inputStream, new BufferedOutputStream(outputStream));excelWrite.excelWrite(data); }
3. 返回文件字节
/*** 获取文件字节* @param templatePath 模板路径* @param data 表数据内容* @throws Exception*/ public static byte[] exportExcelBytes(String templatePath, List data) throws Exception {OutputStream outputStream = new ByteArrayOutputStream();InputStream inputStream = ExportExcelController.class.getResourceAsStream(templatePath);ExcelWrite excelWrite = new JxlsExcelWrite(inputStream, new BufferedOutputStream(outputStream));excelWrite.excelWrite(data);return ((ByteArrayOutputStream) outputStream).toByteArray(); }
4. 批量文件流压缩1
/*** 批量文件流压缩* @param response 响应* @param fileName 文件名* @param list 多个文件byte[]* @throws Exception*/ public static void exportExcelByZip(HttpServletResponse response, String fileName, List<byte[]> list) throws Exception {if (CollectionUtils.isEmpty(list)) {return;}// 1.创建字节数组输出流,用于返回压缩后的输出流字节数组ByteArrayOutputStream baos = new ByteArrayOutputStream();// 2.创建压缩输出流ZipOutputStream zipOut = new ZipOutputStream(baos);// 3.遍历要批量压缩的集合文件流ByteArrayInputStream bais = null;int number = 0;int temp = 0;for (byte[] bytes : list) {number += 1;// 3.1将需要压缩的字节输出流,转为字节数组输入流,bais = new ByteArrayInputStream(bytes);// 3.2设置ZipEntry对象,并对需要压缩的文件命名zipOut.putNextEntry(new ZipEntry(fileName + '_' + number + ".xlsx"));// 3.3读取要压缩的字节输出流,进行压缩temp = 0;while ((temp = bais.read()) != -1) {zipOut.write(temp);}// 3.4关闭流bais.close();}zipOut.close();// 清空responseresponse.reset();// 设置response的HeaderString zipFileName = new String((fileName + ".zip").getBytes(StandardCharsets.UTF_8), "ISO-8859-1");response.addHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", zipFileName));response.setContentType("application/octet-stream;charset=utf-8");OutputStream toClient = new BufferedOutputStream(response.getOutputStream());response.setContentType("application/octet-stream");toClient.write(baos.toByteArray());toClient.flush();toClient.close();baos.close(); }
5. 批量文件流压缩2
/*** 批量文件流压缩* @param list 多个文件* @throws Exception*/ public static byte[] exportExcelByZip(List<ByteParam> list, String suffix) throws Exception {if (CollectionUtils.isEmpty(list)) {return null;}// 1.创建字节数组输出流,用于返回压缩后的输出流字节数组ByteArrayOutputStream baos = new ByteArrayOutputStream();// 2.创建压缩输出流ZipOutputStream zipOut = new ZipOutputStream(baos);// 3.遍历要批量压缩的集合文件流ByteArrayInputStream bais = null;int temp = 0;for (ByteParam byteParam : list) {// 3.1将需要压缩的字节输出流,转为字节数组输入流,bais = new ByteArrayInputStream(byteParam.getBytes());// 3.2设置ZipEntry对象,并对需要压缩的文件命名zipOut.putNextEntry(new ZipEntry(byteParam.getFileName() + suffix));// 3.3读取要压缩的字节输出流,进行压缩temp = 0;while ((temp = bais.read()) != -1) {zipOut.write(temp);}// 3.4关闭流bais.close();}zipOut.close();baos.close();return baos.toByteArray(); }
更多推荐
导出数据大于设置的最大数据则导出多个文件压缩后返回
发布评论