Browse Source

优化读写逻辑

developing
zhuangjiaju 5 years ago
parent
commit
89245cd0a3
  1. 47
      README.md
  2. 2
      src/main/java/com/alibaba/excel/write/builder/ExcelWriterBuilder.java
  3. 22
      src/test/java/com/alibaba/easyexcel/test/demo/web/DownloadData.java
  4. 17
      src/test/java/com/alibaba/easyexcel/test/demo/web/UploadData.java
  5. 49
      src/test/java/com/alibaba/easyexcel/test/demo/web/UploadDataListener.java
  6. 64
      src/test/java/com/alibaba/easyexcel/test/demo/web/WebTest.java
  7. 9
      src/test/java/com/alibaba/easyexcel/test/demo/write/DemoData.java

47
README.md

@ -67,27 +67,36 @@ DEMO代码地址:[https://github.com/alibaba/easyexcel/blob/master/src/test/ja
} }
``` ```
### web下载实例写法 ### 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)
public class Down { ```java
@GetMapping("/a.htm") /**
public void cooperation(HttpServletRequest request, HttpServletResponse response) { * 文件下载
ServletOutputStream out = response.getOutputStream(); * <li>1. 创建excel对应的实体对象 参照{@link DownloadData}
response.setContentType("multipart/form-data"); * <li>2. 设置返回的 参数
* <li>3. 直接写,这里注意,finish的时候会自动关闭OutputStream,当然你外面再关闭异常问题不大
*/
@GetMapping("download")
public void download(HttpServletResponse response) throws IOException {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8");
response.setHeader("Content-disposition", "attachment;filename="+fileName+".xlsx"); response.setHeader("Content-disposition", "attachment;filename=demo.xlsx");
ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX, true); EasyExcelFactory.write(response.getOutputStream(), DownloadData.class).sheet("模板").doWrite(data()).finish();
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); * <li>1. 创建excel对应的实体对象 参照{@link UploadData}
writer.finish(); * <li>2. 由于默认异步读取excel,所以需要创建excel一行一行的回调监听器,参照{@link UploadDataListener}
* <li>3. 直接读即可
out.flush(); */
} @PostMapping("upload")
@ResponseBody
public String upload(MultipartFile file) throws IOException {
EasyExcelFactory.read(file.getInputStream(), UploadData.class, new UploadDataListener()).sheet().doRead()
.finish();
return "success";
} }
}
``` ```
### 联系我们 ### 联系我们
有问题阿里同事可以通过钉钉找到我,阿里外同学可以通过git留言。其他技术非技术相关的也欢迎一起探讨。 有问题阿里同事可以通过钉钉找到我,阿里外同学可以通过git留言。其他技术非技术相关的也欢迎一起探讨。

2
src/main/java/com/alibaba/excel/write/builder/ExcelWriterBuilder.java

@ -192,7 +192,7 @@ public class ExcelWriterBuilder {
if (sheetName != null) { if (sheetName != null) {
excelWriterSheetBuilder.sheetName(sheetName); excelWriterSheetBuilder.sheetName(sheetName);
} }
return new ExcelWriterSheetBuilder(excelWriter); return excelWriterSheetBuilder;
} }
} }

22
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;
}

17
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;
}

49
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<UploadData> {
private static final Logger LOGGER = LoggerFactory.getLogger(UploadDataListener.class);
/**
* 每隔5条存储数据库实际使用中可以3000条然后清理list 方便内存回收
*/
private static final int BATCH_COUNT = 5;
List<UploadData> list = new ArrayList<UploadData>();
@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("存储数据库成功!");
}
}

64
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 {
/**
* 文件下载
* <li>1. 创建excel对应的实体对象 参照{@link DownloadData}
* <li>2. 设置返回的 参数
* <li>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();
}
/**
* 文件上传
* <li>1. 创建excel对应的实体对象 参照{@link UploadData}
* <li>2. 由于默认异步读取excel所以需要创建excel一行一行的回调监听器参照{@link UploadDataListener}
* <li>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<DownloadData> data() {
List<DownloadData> list = new ArrayList<DownloadData>();
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;
}
}

9
src/test/java/com/alibaba/easyexcel/test/demo/write/DemoData.java

@ -7,23 +7,16 @@ import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data; import lombok.Data;
/** /**
* 基础数据类.行高 列宽可以忽略 * 基础数据类
* *
* @author zhuangjiaju * @author zhuangjiaju
**/ **/
@Data @Data
public class DemoData { public class DemoData {
@ExcelProperty("字符串标题") @ExcelProperty("字符串标题")
private String string; private String string;
/**
* 指定写到excel的格式
*/
@ExcelProperty("日期标题") @ExcelProperty("日期标题")
private Date date; private Date date;
/**
* 数字转成百分比默认数字存到excel是数字不存在格式现在要变成百分比就变成文本了所以要指定转换器
*/
@ExcelProperty("数字标题") @ExcelProperty("数字标题")
private Double doubleData; private Double doubleData;
} }

Loading…
Cancel
Save