From e719a6e05511c3b370aa2a13275ccf779e501c20 Mon Sep 17 00:00:00 2001 From: lxyer <237809796@qq.com> Date: Sat, 7 Jul 2018 13:36:10 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 5 +- pom.xml | 8 ++ src/excel/ExcelExportKit.java | 102 +++++---------------- test/excel/ExportKitTest.java | 168 ++++++++++++++++++++++++++++++++++ test/excel/ExportTest.java | 76 --------------- test/excel/Person.java | 14 ++- 6 files changed, 212 insertions(+), 161 deletions(-) create mode 100644 test/excel/ExportKitTest.java delete mode 100644 test/excel/ExportTest.java diff --git a/README.md b/README.md index 224e089..bfb3dc5 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,5 @@ ### 功能 1. poi的导入导出 支持多sheet导出,详见代码 ExcelKit 2. 非poi导出,导出数据量理论无上限;导出数据类型不限制List(JDK8+), -如果是jdk7及以下版本,未做支持(需要修改部分代码) -3、支持excel压缩导出节约网络资源 - +如果是jdk7及以下版本,未做支持(需要修改部分代码) +3. 支持excel压缩导出节约网络资源 \ No newline at end of file diff --git a/pom.xml b/pom.xml index ae06187..9d89a48 100644 --- a/pom.xml +++ b/pom.xml @@ -33,10 +33,18 @@ poi-ooxml 3.17 + + + org.redkale + redkale + 1.9.3 + test + org.apache.tomcat servlet-api 6.0.37 + compile junit diff --git a/src/excel/ExcelExportKit.java b/src/excel/ExcelExportKit.java index b6bd816..24e53d2 100644 --- a/src/excel/ExcelExportKit.java +++ b/src/excel/ExcelExportKit.java @@ -1,41 +1,36 @@ package excel; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.*; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; import java.lang.reflect.Field; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import java.util.Map; import java.util.function.BiFunction; -import java.util.zip.ZipEntry; -import java.util.zip.ZipOutputStream; /** * Created by liangxianyou at 2018/6/27 16:15. */ public class ExcelExportKit { - - public static void main(String[] args) throws IOException { - - } + public static final ThreadLocal SDF_THREADLOCAL = new ThreadLocal<>(); //-------------非poi导出--------------- - private static String exportExcel(Map head, List rows, BiFunction fun) { + public static StringBuilder exportExcel(Map head, List rows, BiFunction fun) { long start = System.currentTimeMillis(); - StringBuffer buf = new StringBuffer(); + StringBuilder buf = new StringBuilder(); buf.append(EXCEL_HEAD); buf.append(createBody(head, rows, fun));//body buf.append(EXCEL_END); System.out.println(String.format("数据:%s条,耗时:%s ms", rows.size(), System.currentTimeMillis() - start)); - return buf.toString(); + return buf; } - private static StringBuffer createBody(Map head, List rows, BiFunction fun) { - StringBuffer buf = new StringBuffer(); + private static StringBuilder createBody(Map head, List rows, BiFunction fun) { + StringBuilder buf = new StringBuilder(); //table-head buf.append(""); head.forEach((k,v)->{ @@ -47,13 +42,18 @@ public class ExcelExportKit { rows.forEach(x->{ buf.append(""); head.forEach((k,v)->{ - Object value = fun.apply(x, v); + Object value = fun.apply(x, (U)v); + if (value == null) value = ""; if (value instanceof Number){ buf.append(""+value+" "); }else if (value instanceof Date){ - buf.append(""+ - new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(value) + - " " + SimpleDateFormat dateFormat = SDF_THREADLOCAL.get(); + if (dateFormat == null) {//ThreadLocal 存贮 simpleDateFormat 对象 + SDF_THREADLOCAL.set(dateFormat = new SimpleDateFormat()); + } + + buf.append( + ""+ dateFormat.format(value) +" " ); }else { buf.append(""+value+" "); @@ -65,69 +65,10 @@ public class ExcelExportKit { return buf; } - public static void exportExcel(Map head, List rows, File file, BiFunction fun) throws IOException { - strToFile(exportExcel(head, rows, fun), file); - } + public static void exportExceltoFile(Map head, List rows, File file, BiFunction fun) throws IOException { + StringBuilder buf = exportExcel(head, rows, fun); - /** - * excel 压缩导出 - * @param head - * @param rows - * @param file - * @param fun - * @param - * @throws IOException - */ - public static 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); - - bos.write(exportExcel(head, rows, fun).getBytes()); - bos.flush(); - - bos.close(); - zos.close(); - } - - - /* - //使用redkale框架情况下的 导出 - public static boolean 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); - - response.finish(exportExcel(head, rows, fun).getBytes()); - return true; - }*/ - - /** - * 非poi导出excel--【javax.servlet】 - * @param head 表头 - * @param rows 数据 - * @param request - * @param response - * @param xlsName 导出的文件名称 - * @return - * @throws IOException - */ - public static 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"; - } - OutputStream os = response.getOutputStream(); - response.setContentType("application/vnd.ms-excel"); - response.setHeader("Content-disposition", "attachment;filename="+xlsName); - - os.write(exportExcel(head, rows, fun).getBytes()); - return true; + strToFile(buf.toString(), file); } public static void strToFile(String entityBody, File file) throws IOException { @@ -141,7 +82,6 @@ public class ExcelExportKit { out.close(); } - public static class TC { public static final BiFunction BEAN = (t,u)->{ Field field = null; diff --git a/test/excel/ExportKitTest.java b/test/excel/ExportKitTest.java new file mode 100644 index 0000000..65e78a5 --- /dev/null +++ b/test/excel/ExportKitTest.java @@ -0,0 +1,168 @@ +package excel; + +import org.junit.Test; +import org.redkale.net.http.HttpResponse; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.*; +import java.util.*; +import java.util.concurrent.CountDownLatch; +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()); + } +} diff --git a/test/excel/ExportTest.java b/test/excel/ExportTest.java deleted file mode 100644 index 2fe2b24..0000000 --- a/test/excel/ExportTest.java +++ /dev/null @@ -1,76 +0,0 @@ -package excel; - -import org.junit.Test; - -import java.io.File; -import java.io.IOException; -import java.util.*; - -/** - * Created by liangxianyou at 2018/7/6 10:29. - */ -public class ExportTest { - - @Test - public void exportBean(){ - Map heads = new LinkedHashMap(); - heads.put("姓名", "name"); - heads.put("年龄", "age"); - - 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)); - } - File file = new File("target/user.zip"); - if (file.exists()) file.delete(); - - try { - ExcelExportKit.exportExcelZip(heads, rows, file, ExcelExportKit.TC.BEAN); - } catch (IOException e) { - e.printStackTrace(); - } - } - - @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 { - ExcelExportKit.exportExcelZip(heads, rows, file, ExcelExportKit.TC.MAP); - } catch (IOException e) { - e.printStackTrace(); - } - } -} diff --git a/test/excel/Person.java b/test/excel/Person.java index ec13b5e..7814cdf 100644 --- a/test/excel/Person.java +++ b/test/excel/Person.java @@ -1,18 +1,22 @@ package excel; +import java.util.Date; + /** * Created by liangxianyou at 2018/7/6 10:28. */ public class Person { private String name; private int age; + private Date bir; public Person() { } - public Person(String name, int age) { + public Person(String name, int age, Date bir) { this.name = name; this.age = age; + this.bir = bir; } public String getName() { @@ -30,4 +34,12 @@ public class Person { public void setAge(int age) { this.age = age; } + + public Date getBir() { + return bir; + } + + public void setBir(Date bir) { + this.bir = bir; + } }