From 89245cd0a3ec2874c7c8779898ac2577f224e068 Mon Sep 17 00:00:00 2001 From: zhuangjiaju Date: Tue, 6 Aug 2019 10:01:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=AF=BB=E5=86=99=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 47 ++++++++------ .../write/builder/ExcelWriterBuilder.java | 2 +- .../easyexcel/test/demo/web/DownloadData.java | 22 +++++++ .../easyexcel/test/demo/web/UploadData.java | 17 +++++ .../test/demo/web/UploadDataListener.java | 49 ++++++++++++++ .../easyexcel/test/demo/web/WebTest.java | 64 +++++++++++++++++++ .../easyexcel/test/demo/write/DemoData.java | 9 +-- 7 files changed, 182 insertions(+), 28 deletions(-) create mode 100644 src/test/java/com/alibaba/easyexcel/test/demo/web/DownloadData.java create mode 100644 src/test/java/com/alibaba/easyexcel/test/demo/web/UploadData.java create mode 100644 src/test/java/com/alibaba/easyexcel/test/demo/web/UploadDataListener.java create mode 100644 src/test/java/com/alibaba/easyexcel/test/demo/web/WebTest.java diff --git a/README.md b/README.md index 4c5ec78..1358e53 100644 --- a/README.md +++ b/README.md @@ -67,27 +67,36 @@ DEMO代码地址:[https://github.com/alibaba/easyexcel/blob/master/src/test/ja } ``` -### web下载实例写法 -``` -public class Down { - @GetMapping("/a.htm") - public void cooperation(HttpServletRequest request, HttpServletResponse response) { - ServletOutputStream out = response.getOutputStream(); - response.setContentType("multipart/form-data"); +### web上传、下载 +DEMO代码地址:[https://github.com/alibaba/easyexcel/blob/master/src/test/java/com/alibaba/easyexcel/test/demo/web/WebTest.java](/src/test/java/com/alibaba/easyexcel/test/demo/web/WebTest.java) +```java + /** + * 文件下载 + *
  • 1. 创建excel对应的实体对象 参照{@link DownloadData} + *
  • 2. 设置返回的 参数 + *
  • 3. 直接写,这里注意,finish的时候会自动关闭OutputStream,当然你外面再关闭异常问题不大 + */ + @GetMapping("download") + public void download(HttpServletResponse response) throws IOException { + response.setContentType("application/vnd.ms-excel"); response.setCharacterEncoding("utf-8"); - response.setHeader("Content-disposition", "attachment;filename="+fileName+".xlsx"); - ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX, true); - String fileName = new String(("UserInfo " + new SimpleDateFormat("yyyy-MM-dd").format(new Date())) - .getBytes(), "UTF-8"); - Sheet sheet1 = new Sheet(1, 0); - sheet1.setSheetName("第一个sheet"); - writer.write0(getListString(), sheet1); - writer.finish(); - - out.flush(); - } + response.setHeader("Content-disposition", "attachment;filename=demo.xlsx"); + EasyExcelFactory.write(response.getOutputStream(), DownloadData.class).sheet("模板").doWrite(data()).finish(); + } + + /** + * 文件上传 + *
  • 1. 创建excel对应的实体对象 参照{@link UploadData} + *
  • 2. 由于默认异步读取excel,所以需要创建excel一行一行的回调监听器,参照{@link UploadDataListener} + *
  • 3. 直接读即可 + */ + @PostMapping("upload") + @ResponseBody + public String upload(MultipartFile file) throws IOException { + EasyExcelFactory.read(file.getInputStream(), UploadData.class, new UploadDataListener()).sheet().doRead() + .finish(); + return "success"; } -} ``` ### 联系我们 有问题阿里同事可以通过钉钉找到我,阿里外同学可以通过git留言。其他技术非技术相关的也欢迎一起探讨。 diff --git a/src/main/java/com/alibaba/excel/write/builder/ExcelWriterBuilder.java b/src/main/java/com/alibaba/excel/write/builder/ExcelWriterBuilder.java index bb1c4dd..76606da 100644 --- a/src/main/java/com/alibaba/excel/write/builder/ExcelWriterBuilder.java +++ b/src/main/java/com/alibaba/excel/write/builder/ExcelWriterBuilder.java @@ -192,7 +192,7 @@ public class ExcelWriterBuilder { if (sheetName != null) { excelWriterSheetBuilder.sheetName(sheetName); } - return new ExcelWriterSheetBuilder(excelWriter); + return excelWriterSheetBuilder; } } diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/web/DownloadData.java b/src/test/java/com/alibaba/easyexcel/test/demo/web/DownloadData.java new file mode 100644 index 0000000..8e5a1e3 --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/demo/web/DownloadData.java @@ -0,0 +1,22 @@ +package com.alibaba.easyexcel.test.demo.web; + +import java.util.Date; + +import com.alibaba.excel.annotation.ExcelProperty; + +import lombok.Data; + +/** + * 基础数据类 + * + * @author zhuangjiaju + **/ +@Data +public class DownloadData { + @ExcelProperty("字符串标题") + private String string; + @ExcelProperty("日期标题") + private Date date; + @ExcelProperty("数字标题") + private Double doubleData; +} diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/web/UploadData.java b/src/test/java/com/alibaba/easyexcel/test/demo/web/UploadData.java new file mode 100644 index 0000000..ee3dead --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/demo/web/UploadData.java @@ -0,0 +1,17 @@ +package com.alibaba.easyexcel.test.demo.web; + +import java.util.Date; + +import lombok.Data; + +/** + * 基础数据类 + * + * @author zhuangjiaju + **/ +@Data +public class UploadData { + private String string; + private Date date; + private Double doubleData; +} diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/web/UploadDataListener.java b/src/test/java/com/alibaba/easyexcel/test/demo/web/UploadDataListener.java new file mode 100644 index 0000000..bd8457f --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/demo/web/UploadDataListener.java @@ -0,0 +1,49 @@ +package com.alibaba.easyexcel.test.demo.web; + +import java.util.ArrayList; +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.alibaba.excel.context.AnalysisContext; +import com.alibaba.excel.event.AnalysisEventListener; +import com.alibaba.fastjson.JSON; + +/** + * 模板的读取类 + * + * @author zhuangjiaju + */ +public class UploadDataListener extends AnalysisEventListener { + private static final Logger LOGGER = LoggerFactory.getLogger(UploadDataListener.class); + /** + * 每隔5条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收 + */ + private static final int BATCH_COUNT = 5; + List list = new ArrayList(); + + @Override + public void invoke(UploadData data, AnalysisContext context) { + LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data)); + list.add(data); + if (list.size() >= BATCH_COUNT) { + saveData(); + list.clear(); + } + } + + @Override + public void doAfterAllAnalysed(AnalysisContext context) { + saveData(); + LOGGER.info("所有数据解析完成!"); + } + + /** + * 加上存储数据库 + */ + private void saveData() { + LOGGER.info("{}条数据,开始存储数据库!", list.size()); + LOGGER.info("存储数据库成功!"); + } +} diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/web/WebTest.java b/src/test/java/com/alibaba/easyexcel/test/demo/web/WebTest.java new file mode 100644 index 0000000..00fc168 --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/demo/web/WebTest.java @@ -0,0 +1,64 @@ +package com.alibaba.easyexcel.test.demo.web; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import javax.servlet.http.HttpServletResponse; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.multipart.MultipartFile; + +import com.alibaba.excel.EasyExcelFactory; + +/** + * web读写案例 + * + * @author zhuangjiaju + **/ +@Controller +public class WebTest { + /** + * 文件下载 + *
  • 1. 创建excel对应的实体对象 参照{@link DownloadData} + *
  • 2. 设置返回的 参数 + *
  • 3. 直接写,这里注意,finish的时候会自动关闭OutputStream,当然你外面再关闭异常问题不大 + */ + @GetMapping("download") + public void download(HttpServletResponse response) throws IOException { + response.setContentType("application/vnd.ms-excel"); + response.setCharacterEncoding("utf-8"); + response.setHeader("Content-disposition", "attachment;filename=demo.xlsx"); + EasyExcelFactory.write(response.getOutputStream(), DownloadData.class).sheet("模板").doWrite(data()).finish(); + } + + /** + * 文件上传 + *
  • 1. 创建excel对应的实体对象 参照{@link UploadData} + *
  • 2. 由于默认异步读取excel,所以需要创建excel一行一行的回调监听器,参照{@link UploadDataListener} + *
  • 3. 直接读即可 + */ + @PostMapping("upload") + @ResponseBody + public String upload(MultipartFile file) throws IOException { + EasyExcelFactory.read(file.getInputStream(), UploadData.class, new UploadDataListener()).sheet().doRead() + .finish(); + return "success"; + } + + private List data() { + List list = new ArrayList(); + for (int i = 0; i < 10; i++) { + DownloadData data = new DownloadData(); + data.setString("字符串" + 0); + data.setDate(new Date()); + data.setDoubleData(0.56); + list.add(data); + } + return list; + } +} diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/write/DemoData.java b/src/test/java/com/alibaba/easyexcel/test/demo/write/DemoData.java index 143a4e4..39840b0 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/write/DemoData.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/write/DemoData.java @@ -7,23 +7,16 @@ import com.alibaba.excel.annotation.ExcelProperty; import lombok.Data; /** - * 基础数据类.行高 列宽可以忽略 + * 基础数据类 * * @author zhuangjiaju **/ @Data public class DemoData { - @ExcelProperty("字符串标题") private String string; - /** - * 指定写到excel的格式 - */ @ExcelProperty("日期标题") private Date date; - /** - * 数字转成百分比,默认数字存到excel是数字,不存在格式,现在要变成百分比就变成文本了,所以要指定转换器 - */ @ExcelProperty("数字标题") private Double doubleData; }