From 1ec84746de357d37d66df8ca4d7e53df1df6a6f1 Mon Sep 17 00:00:00 2001 From: "jipengfei.jpf" Date: Tue, 13 Nov 2018 14:58:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=87=E7=BA=A7=E6=96=B0=E7=89=88=E6=9C=AC?= =?UTF-8?q?=EF=BC=8C=E4=BF=AE=E5=A4=8Dbug=EF=BC=8C=E6=8F=90=E4=BE=9B?= =?UTF-8?q?=E7=BC=96=E8=BE=91=E5=B7=A5=E5=85=B7=EF=BC=8C=E6=8F=90=E5=8D=87?= =?UTF-8?q?=E6=A8=A1=E5=9E=8B=E8=BD=AC=E6=8D=A2=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 151 +++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 103 insertions(+), 48 deletions(-) diff --git a/README.md b/README.md index d11103e..137234a 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ Java解析、生成Excel比较有名的框架有Apache poi、jxl。但他们都 ``` # 最新版本 -## VERSION : 1.1.1 +### VERSION : 1.1.2-beat1 # 维护者 姬朋飞(玉霄) @@ -31,60 +31,115 @@ Java解析、生成Excel比较有名的框架有Apache poi、jxl。但他们都 ## 读Excel +测试代码地址:[https://github.com/alibaba/easyexcel/blob/master/src/test/java/com/alibaba/easyexcel/test/ReadTest.java](/src/test/java/com/alibaba/easyexcel/test/ReadTest.java) +读07版小于1000行数据返回List> ``` -public void noModelMultipleSheet() { - InputStream inputStream = getInputStream("2007NoModelMultipleSheet.xlsx"); - try { - ExcelReader reader = new ExcelReader(inputStream, ExcelTypeEnum.XLSX, null, - new AnalysisEventListener>() { - @Override - public void invoke(List object, AnalysisContext context) { - System.out.println( - "当前sheet:" + context.getCurrentSheet().getSheetNo() + " 当前行:" + context.getCurrentRowNum() - + " data:" + object); - } - @Override - public void doAfterAllAnalysed(AnalysisContext context) { - - } - }); - - reader.read(); - } catch (Exception e) { - e.printStackTrace(); +List data = EasyExcelFactory.read(inputStream, new Sheet(1, 0)); - } finally { - try { - inputStream.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - } ``` +读07版小于1000行数据返回List +``` +List data = EasyExcelFactory.read(inputStream, new Sheet(2, 1,JavaModel.class)); + +``` +读07版大于1000行数据返回List> +``` +ExcelListener excelListener = new ExcelListener(); +EasyExcelFactory.readBySax(inputStream, new Sheet(1, 1), excelListener); +``` + +读07版大于1000行数据返回List +``` +ExcelListener excelListener = new ExcelListener(); +EasyExcelFactory.readBySax(inputStream, new Sheet(2, 1,JavaModel.class), excelListener); +``` +读03版方法同上 + + ## 写Excel +测试代码地址:[https://github.com/alibaba/easyexcel/blob/master/src/test/java/com/alibaba/easyexcel/test/WriteTest.java](/src/test/java/com/alibaba/easyexcel/test/WriteTest.java) + +没有模板 ``` -@Test -public void test1() throws FileNotFoundException { - OutputStream out = new FileOutputStream("/Users/jipengfei/78.xlsx"); - try { - ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX); - //写第一个sheet, sheet1 数据全是List 无模型映射关系 - Sheet sheet1 = new Sheet(1, 0,ExcelPropertyIndexModel.class); - writer.write(getData(), sheet1); - writer.finish(); - } catch (Exception e) { - e.printStackTrace(); - } finally { - try { - out.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - } +OutputStream out = new FileOutputStream("/Users/jipengfei/2007.xlsx"); +ExcelWriter writer = EasyExcelFactory.getWriter(out); + +//写第一个sheet, sheet1 数据全是List 无模型映射关系 +Sheet sheet1 = new Sheet(1, 3); +sheet1.setSheetName("第一个sheet"); +//设置列宽 设置每列的宽度 +Map columnWidth = new HashMap(); +columnWidth.put(0,10000);columnWidth.put(1,40000);columnWidth.put(2,10000);columnWidth.put(3,10000); +sheet1.setColumnWidthMap(columnWidth); +sheet1.setHead(createTestListStringHead()); +//or 设置自适应宽度 +//sheet1.setAutoWidth(Boolean.TRUE); +writer.write1(createTestListObject(), sheet1); + +//写第二个sheet sheet2 模型上打有表头的注解,合并单元格 +Sheet sheet2 = new Sheet(2, 3, JavaModel1.class, "第二个sheet", null); +sheet2.setTableStyle(createTableStyle()); +writer.write(createTestListJavaMode(), sheet2); + +//写第三个sheet包含多个table情况 +Sheet sheet3 = new Sheet(3, 0); +sheet3.setSheetName("第三个sheet"); +Table table1 = new Table(1); +table1.setHead(createTestListStringHead()); +writer.write1(createTestListObject(), sheet3, table1); + +//写sheet2 模型上打有表头的注解 +Table table2 = new Table(2); +table2.setTableStyle(createTableStyle()); +table2.setClazz(JavaModel1.class); +writer.write(createTestListJavaMode(), sheet3, table2); + +//关闭资源 +writer.finish(); +out.close(); +``` +有模板 ``` +InputStream inputStream = new BufferedInputStream(new FileInputStream("/Users/jipengfei/temp.xlsx")); +OutputStream out = new FileOutputStream("/Users/jipengfei/2007.xlsx"); +ExcelWriter writer = EasyExcelFactory.getWriterWithTemp(inputStream,out,ExcelTypeEnum.XLSX,true); + +//写第一个sheet, sheet1 数据全是List 无模型映射关系 +Sheet sheet1 = new Sheet(1, 3); +sheet1.setSheetName("第一个sheet"); +//设置列宽 设置每列的宽度 +Map columnWidth = new HashMap(); +columnWidth.put(0,10000);columnWidth.put(1,40000);columnWidth.put(2,10000);columnWidth.put(3,10000); +sheet1.setColumnWidthMap(columnWidth); +sheet1.setHead(createTestListStringHead()); +//or 设置自适应宽度 +//sheet1.setAutoWidth(Boolean.TRUE); +writer.write1(createTestListObject(), sheet1); + +//写第二个sheet sheet2 模型上打有表头的注解,合并单元格 +Sheet sheet2 = new Sheet(2, 3, JavaModel1.class, "第二个sheet", null); +sheet2.setTableStyle(createTableStyle()); +writer.write(createTestListJavaMode(), sheet2); + +//写第三个sheet包含多个table情况 +Sheet sheet3 = new Sheet(3, 0); +sheet3.setSheetName("第三个sheet"); +Table table1 = new Table(1); +table1.setHead(createTestListStringHead()); +writer.write1(createTestListObject(), sheet3, table1); + +//写sheet2 模型上打有表头的注解 +Table table2 = new Table(2); +table2.setTableStyle(createTableStyle()); +table2.setClazz(JavaModel1.class); +writer.write(createTestListJavaMode(), sheet3, table2); + +//关闭资源 +writer.finish(); +out.close(); +``` + # web下载实例写法 package com.alibaba.china.pte.web.seller.dingtalk.rpc;