diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7e5a035 --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +/target/ +.project +.classpath +/.settings/ +/.externalToolBuilders/ +/bin/ +*.iml +.idea/ +/.idea/ +/out/ + +*.zip +/tmp/ +*.swp +~$* \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..9871262 --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# ExcelUtil + +### 功能 +1. poi的导入导出 支持多sheet导出,详见代码 ExcelKit +2. 非poi导出,导出数据量理论无上限;导出数据类型不限制List(JDK8+), +如果是jdk7及以下版本,未做支持(需要修改部分代码) + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..ae06187 --- /dev/null +++ b/pom.xml @@ -0,0 +1,49 @@ + + + 4.0.0 + + com.lxyer + jdk-demo + 1.0-SNAPSHOT + + src + test + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + + + + org.apache.poi + poi + 3.17 + + + org.apache.poi + poi-ooxml + 3.17 + + + org.apache.tomcat + servlet-api + 6.0.37 + + + junit + junit + 4.12 + test + + + + \ No newline at end of file diff --git a/src/excel/ExcelExportKit.java b/src/excel/ExcelExportKit.java new file mode 100644 index 0000000..b6bd816 --- /dev/null +++ b/src/excel/ExcelExportKit.java @@ -0,0 +1,167 @@ +package excel; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.*; +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 { + + } + + //-------------非poi导出--------------- + private static String exportExcel(Map head, List rows, BiFunction fun) { + long start = System.currentTimeMillis(); + + StringBuffer buf = new StringBuffer(); + 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(); + } + + private static StringBuffer createBody(Map head, List rows, BiFunction fun) { + StringBuffer buf = new StringBuffer(); + //table-head + buf.append(""); + head.forEach((k,v)->{ + buf.append(""+k+" "); + }); + buf.append(""); + + //table-body + rows.forEach(x->{ + buf.append(""); + head.forEach((k,v)->{ + Object value = fun.apply(x, v); + if (value instanceof Number){ + buf.append(""+value+" "); + }else if (value instanceof Date){ + buf.append(""+ + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(value) + + " " + ); + }else { + buf.append(""+value+" "); + } + }); + buf.append(""); + }); + + return buf; + } + + public static void exportExcel(Map head, List rows, File file, BiFunction fun) throws IOException { + strToFile(exportExcel(head, rows, fun), file); + } + + /** + * 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; + } + + public static void strToFile(String entityBody, File file) throws IOException { + + if (file.exists()) file.delete(); + //throw new RuntimeException(file.getPath() + "已经存在"); + + if (!file.getParentFile().exists()) file.getParentFile().mkdirs(); + FileOutputStream out = new FileOutputStream(file); + out.write(entityBody.getBytes("UTF-8")); + out.close(); + } + + + public static class TC { + public static final BiFunction BEAN = (t,u)->{ + Field field = null; + try { + field = t.getClass().getDeclaredField((String) u); + field.setAccessible(true);//暴力反射 + Object o = field.get(t);//得到字段数据的值 + return o; + } catch (Exception e) { + e.printStackTrace(); + } + return ""; + }; + + public static final BiFunction MAP = (t,u)->{ + return ((Map)t).get(u); + }; + } + + private static String EXCEL_HEAD = "lxy-kitlxy2082052-10.1.0.72452239512045FalseFalse"; + + private static String EXCEL_END = "