package com.lxyer.excel; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.*; import java.util.function.BiFunction; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; /** * 以下均为非poi导出使用示例 * 1、导出List示例 * 2、导出List 示例 * 3、javax.servlet web项目中开发使用示例 * 4、导出压缩的excel示例 * 5、redkale web项目导出示例 * * Created by liangxianyou at 2018/7/6 10:29. */ public class ExportKitTest { //1、导出List示例 //@Test public void exportBean(){ Map heads = new LinkedHashMap(); heads.put("姓名", "name"); heads.put("年龄", "age"); heads.put("生日", "bir"); int total = 5_0000; List rows = new ArrayList<>(total); Random random = new Random(); for (int i = 0; i < total; i++) { String name = UUID.randomUUID().toString(); int age = random.nextInt(100); rows.add(new Person(name, age, new Date())); } File file = new File("target/user.xls"); if (file.exists()) file.delete(); try { ExcelExportKit.exportExceltoFile(heads, rows, file, ExcelExportKit.TC.BEAN); } catch (IOException e) { e.printStackTrace(); } } //2、导出List 示例 //@Test public void exportMapToZip(){ Map heads = new LinkedHashMap(); heads.put("姓名", "name"); heads.put("姓名1", "name1"); heads.put("姓名2", "name2"); heads.put("年龄", "age"); int total = 5_0000; List rows = new ArrayList<>(total); Map r1 = new HashMap(); r1.put("name", "张三"); r1.put("age", 12); rows.add(r1); Random random = new Random(); //使用uuid 模拟复杂数据导出 for (int i = 0; i < total; i++) { Map r2 = new HashMap(); r2.put("name", UUID.randomUUID()); r2.put("name1", UUID.randomUUID()); r2.put("name2", UUID.randomUUID()); r2.put("age", random.nextInt(100)); rows.add(r2); } File file = new File("target/hello.zip"); if (file.exists()) file.delete(); try { exportExcelZip(heads, rows, file, ExcelExportKit.TC.MAP); } catch (IOException e) { e.printStackTrace(); } } //==================项目中使用示例,实际项目根据实际情况拷贝修改使用======================== /** * 3、javax.servlet web项目中开发使用示例 * @param head 表头 * @param rows 数据 * @param request * @param response * @param xlsName 导出的文件名称 * @return * @throws IOException */ /* public boolean exportExcel(Map head, List rows, HttpServletRequest request, HttpServletResponse response, String xlsName, BiFunction fun) throws IOException { if(request.getHeader("user-agent") != null && request.getHeader("user-agent").indexOf("MSIE") != -1) { xlsName = java.net.URLEncoder.encode(xlsName,"utf-8") + ".xls"; } else { xlsName = new String(xlsName.getBytes("utf-8"),"iso-8859-1")+ ".xls"; } CountDownLatch latch = new CountDownLatch(187); latch.countDown(); OutputStream os = response.getOutputStream(); response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-disposition", "attachment;filename="+xlsName); os.write(ExcelExportKit.exportExcel(head, rows, fun).toString().getBytes()); return true; } */ /** * 4、导出压缩的excel示例 * @param head * @param rows * @param file * @param fun * @param * @throws IOException */ public void exportExcelZip(Map head, List rows, File file, BiFunction fun) throws IOException { ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file)); BufferedOutputStream bos = new BufferedOutputStream(zos); ZipEntry zipEntry = new ZipEntry(file.getName().split("[.]")[0]+".xls"); zos.putNextEntry(zipEntry); StringBuilder buf = ExcelExportKit.exportExcel(head, rows, fun); bos.write(buf.toString().getBytes()); bos.flush(); bos.close(); zos.close(); } /** * 5、redkale web项目导出示例 * @param head * @param rows * @param response * @param xlsName * @param fun * @return * @throws IOException */ /* public static void exportExcel(Map head, List rows, HttpResponse response, String xlsName, BiFunction fun) throws IOException { xlsName = new String(xlsName.getBytes("utf-8"),"iso-8859-1")+ ".xls"; response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-disposition", "attachment;filename="+xlsName); StringBuilder buf = ExcelExportKit.exportExcel(head, rows, fun); response.finish(buf.toString().getBytes()); } */ }