|
|
@ -37,9 +37,9 @@ easyexcel重写了poi对07版Excel的解析,一个3M的excel用POI sax解析 |
|
|
|
# 帮忙点个⭐Star |
|
|
|
# 帮忙点个⭐Star |
|
|
|
|
|
|
|
|
|
|
|
开源不易,如果觉得EasyExcel对您的工作还是有帮助的话,请帮忙在<a target="_blank" href='https://github.com/alibaba/easyexcel'><img src="https://img.shields.io/github/stars/alibaba/easyexcel.svg?style=flat-square&label=Stars&logo=github" alt="github star"/></a> |
|
|
|
开源不易,如果觉得EasyExcel对您的工作还是有帮助的话,请帮忙在<a target="_blank" href='https://github.com/alibaba/easyexcel'><img src="https://img.shields.io/github/stars/alibaba/easyexcel.svg?style=flat-square&label=Stars&logo=github" alt="github star"/></a> |
|
|
|
的右上角点个⭐Star,您的支持是使EasyExcel变得更好最大的动力 |
|
|
|
的右上角点个⭐Star,您的支持是使EasyExcel变得更好最大的动力。 |
|
|
|
|
|
|
|
|
|
|
|
# 如果获取帮助 |
|
|
|
# 如何获取帮助 |
|
|
|
|
|
|
|
|
|
|
|
## 优先建议自己通过文档来解决问题 |
|
|
|
## 优先建议自己通过文档来解决问题 |
|
|
|
|
|
|
|
|
|
|
@ -87,77 +87,79 @@ easyexcel重写了poi对07版Excel的解析,一个3M的excel用POI sax解析 |
|
|
|
|
|
|
|
|
|
|
|
## 读Excel |
|
|
|
## 读Excel |
|
|
|
|
|
|
|
|
|
|
|
DEMO代码地址:[https://github.com/alibaba/easyexcel/blob/master/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/ReadTest.java](https://github.com/alibaba/easyexcel/blob/master/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/ReadTest.java) |
|
|
|
demo代码地址:[https://github.com/alibaba/easyexcel/blob/master/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/ReadTest.java](https://github.com/alibaba/easyexcel/blob/master/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/ReadTest.java) |
|
|
|
|
|
|
|
详细文档地址:[https://easyexcel.opensource.alibaba.com/docs/current/quickstart/read](https://easyexcel.opensource.alibaba.com/docs/current/quickstart/read) |
|
|
|
|
|
|
|
|
|
|
|
```java |
|
|
|
```java |
|
|
|
/** |
|
|
|
/** |
|
|
|
* 最简单的读 |
|
|
|
* 最简单的读 |
|
|
|
* <p>1. 创建excel对应的实体对象 参照{@link DemoData} |
|
|
|
* <p>1. 创建excel对应的实体对象 参照{@link DemoData} |
|
|
|
* <p>2. 由于默认一行行的读取excel,所以需要创建excel一行一行的回调监听器,参照{@link DemoDataListener} |
|
|
|
* <p>2. 由于默认一行行的读取excel,所以需要创建excel一行一行的回调监听器,参照{@link DemoDataListener} |
|
|
|
* <p>3. 直接读即可 |
|
|
|
* <p>3. 直接读即可 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void simpleRead(){ |
|
|
|
public void simpleRead() { |
|
|
|
String fileName=TestFileUtil.getPath()+"demo"+File.separator+"demo.xlsx"; |
|
|
|
String fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx"; |
|
|
|
// 这里 需要指定读用哪个class去读,然后读取第一个sheet 文件流会自动关闭 |
|
|
|
// 这里 需要指定读用哪个class去读,然后读取第一个sheet 文件流会自动关闭 |
|
|
|
EasyExcel.read(fileName,DemoData.class,new DemoDataListener()).sheet().doRead(); |
|
|
|
EasyExcel.read(fileName, DemoData.class, new DemoDataListener()).sheet().doRead(); |
|
|
|
} |
|
|
|
} |
|
|
|
``` |
|
|
|
``` |
|
|
|
|
|
|
|
|
|
|
|
## 写Excel |
|
|
|
## 写Excel |
|
|
|
|
|
|
|
|
|
|
|
DEMO代码地址:[https://github.com/alibaba/easyexcel/blob/master/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java](https://github.com/alibaba/easyexcel/blob/master/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java) |
|
|
|
demo代码地址:[https://github.com/alibaba/easyexcel/blob/master/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java](https://github.com/alibaba/easyexcel/blob/master/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java) |
|
|
|
|
|
|
|
详细文档地址:[https://easyexcel.opensource.alibaba.com/docs/current/quickstart/write](https://easyexcel.opensource.alibaba.com/docs/current/quickstart/write) |
|
|
|
|
|
|
|
|
|
|
|
```java |
|
|
|
```java |
|
|
|
/** |
|
|
|
/** |
|
|
|
* 最简单的写 |
|
|
|
* 最简单的写 |
|
|
|
* <p>1. 创建excel对应的实体对象 参照{@link com.alibaba.easyexcel.test.demo.write.DemoData} |
|
|
|
* <p>1. 创建excel对应的实体对象 参照{@link com.alibaba.easyexcel.test.demo.write.DemoData} |
|
|
|
* <p>2. 直接写即可 |
|
|
|
* <p>2. 直接写即可 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void simpleWrite(){ |
|
|
|
public void simpleWrite() { |
|
|
|
String fileName=TestFileUtil.getPath()+"write"+System.currentTimeMillis()+".xlsx"; |
|
|
|
String fileName=TestFileUtil.getPath()+"write"+System.currentTimeMillis()+".xlsx"; |
|
|
|
// 这里 需要指定写用哪个class去读,然后写到第一个sheet,名字为模板 然后文件流会自动关闭 |
|
|
|
// 这里 需要指定写用哪个class去读,然后写到第一个sheet,名字为模板 然后文件流会自动关闭 |
|
|
|
// 如果这里想使用03 则 传入excelType参数即可 |
|
|
|
// 如果这里想使用03 则 传入excelType参数即可 |
|
|
|
EasyExcel.write(fileName,DemoData.class).sheet("模板").doWrite(data()); |
|
|
|
EasyExcel.write(fileName,DemoData.class).sheet("模板").doWrite(data()); |
|
|
|
} |
|
|
|
} |
|
|
|
``` |
|
|
|
``` |
|
|
|
|
|
|
|
|
|
|
|
## web上传、下载 |
|
|
|
## web上传、下载 |
|
|
|
|
|
|
|
|
|
|
|
DEMO代码地址:[https://github.com/alibaba/easyexcel/blob/master/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/web/WebTest.java](https://github.com/alibaba/easyexcel/blob/master/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/web/WebTest.java) |
|
|
|
demo代码地址:[https://github.com/alibaba/easyexcel/blob/master/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/web/WebTest.java](https://github.com/alibaba/easyexcel/blob/master/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/web/WebTest.java) |
|
|
|
|
|
|
|
|
|
|
|
```java |
|
|
|
```java |
|
|
|
/** |
|
|
|
/** |
|
|
|
* 文件下载(失败了会返回一个有部分数据的Excel) |
|
|
|
* 文件下载(失败了会返回一个有部分数据的Excel) |
|
|
|
* <p> |
|
|
|
* <p> |
|
|
|
* 1. 创建excel对应的实体对象 参照{@link DownloadData} |
|
|
|
* 1. 创建excel对应的实体对象 参照{@link DownloadData} |
|
|
|
* <p> |
|
|
|
* <p> |
|
|
|
* 2. 设置返回的 参数 |
|
|
|
* 2. 设置返回的 参数 |
|
|
|
* <p> |
|
|
|
* <p> |
|
|
|
* 3. 直接写,这里注意,finish的时候会自动关闭OutputStream,当然你外面再关闭流问题不大 |
|
|
|
* 3. 直接写,这里注意,finish的时候会自动关闭OutputStream,当然你外面再关闭流问题不大 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@GetMapping("download") |
|
|
|
@GetMapping("download") |
|
|
|
public void download(HttpServletResponse response)throws IOException{ |
|
|
|
public void download(HttpServletResponse response) throws IOException { |
|
|
|
// 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman |
|
|
|
// 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman |
|
|
|
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); |
|
|
|
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); |
|
|
|
response.setCharacterEncoding("utf-8"); |
|
|
|
response.setCharacterEncoding("utf-8"); |
|
|
|
// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系 |
|
|
|
// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系 |
|
|
|
String fileName=URLEncoder.encode("测试","UTF-8").replaceAll("\\+","%20"); |
|
|
|
String fileName=URLEncoder.encode("测试","UTF-8").replaceAll("\\+","%20"); |
|
|
|
response.setHeader("Content-disposition","attachment;filename*=utf-8''"+fileName+".xlsx"); |
|
|
|
response.setHeader("Content-disposition","attachment;filename*=utf-8''"+fileName+".xlsx"); |
|
|
|
EasyExcel.write(response.getOutputStream(),DownloadData.class).sheet("模板").doWrite(data()); |
|
|
|
EasyExcel.write(response.getOutputStream(),DownloadData.class).sheet("模板").doWrite(data()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 文件上传 |
|
|
|
* 文件上传 |
|
|
|
* <p>1. 创建excel对应的实体对象 参照{@link UploadData} |
|
|
|
* <p>1. 创建excel对应的实体对象 参照{@link UploadData} |
|
|
|
* <p>2. 由于默认一行行的读取excel,所以需要创建excel一行一行的回调监听器,参照{@link UploadDataListener} |
|
|
|
* <p>2. 由于默认一行行的读取excel,所以需要创建excel一行一行的回调监听器,参照{@link UploadDataListener} |
|
|
|
* <p>3. 直接读即可 |
|
|
|
* <p>3. 直接读即可 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@PostMapping("upload") |
|
|
|
@PostMapping("upload") |
|
|
|
@ResponseBody |
|
|
|
@ResponseBody |
|
|
|
public String upload(MultipartFile file)throws IOException{ |
|
|
|
public String upload(MultipartFile file)throws IOException{ |
|
|
|
EasyExcel.read(file.getInputStream(),UploadData.class,new UploadDataListener(uploadDAO)).sheet().doRead(); |
|
|
|
EasyExcel.read(file.getInputStream(),UploadData.class,new UploadDataListener(uploadDAO)).sheet().doRead(); |
|
|
|
return"success"; |
|
|
|
return"success"; |
|
|
|
} |
|
|
|
} |
|
|
|
``` |
|
|
|
``` |