Browse Source

Merge branch 'master' into poi-new-version

pull/2888/head
Ling Hengqian 2 years ago committed by GitHub
parent
commit
77232af45b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      .gitee/ISSUE_TEMPLATE.zh-CN.md
  2. 8
      .github/ISSUE_TEMPLATE/bug.md
  3. 10
      .github/ISSUE_TEMPLATE/question.md
  4. 4
      .github/ISSUE_TEMPLATE/suggest.md
  5. 26
      .github/workflows/sync2gitee.yml
  6. 184
      README.md
  7. 123
      easyexcel-core/src/main/java/com/alibaba/excel/EasyExcelFactory.java
  8. 5
      easyexcel-core/src/main/java/com/alibaba/excel/analysis/v03/handlers/FormulaRecordHandler.java
  9. 3
      easyexcel-core/src/main/java/com/alibaba/excel/analysis/v03/handlers/NumberRecordHandler.java
  10. 5
      easyexcel-core/src/main/java/com/alibaba/excel/analysis/v07/handlers/CellTagHandler.java
  11. 6
      easyexcel-core/src/main/java/com/alibaba/excel/analysis/v07/handlers/sax/SharedStringsTableHandler.java
  12. 10
      easyexcel-core/src/main/java/com/alibaba/excel/analysis/v07/handlers/sax/XlsxRowHandler.java
  13. 19
      easyexcel-core/src/main/java/com/alibaba/excel/constant/EasyExcelConstants.java
  14. 17
      easyexcel-core/src/main/java/com/alibaba/excel/constant/ExcelXmlConstants.java
  15. 9
      easyexcel-core/src/main/java/com/alibaba/excel/converters/date/DateNumberConverter.java
  16. 5
      easyexcel-core/src/main/java/com/alibaba/excel/converters/localdatetime/LocalDateNumberConverter.java
  17. 5
      easyexcel-core/src/main/java/com/alibaba/excel/converters/string/StringNumberConverter.java
  18. 40
      easyexcel-core/src/main/java/com/alibaba/excel/enums/ReadDefaultReturnEnum.java
  19. 3
      easyexcel-core/src/main/java/com/alibaba/excel/metadata/data/CellData.java
  20. 37
      easyexcel-core/src/main/java/com/alibaba/excel/metadata/data/ReadCellData.java
  21. 8
      easyexcel-core/src/main/java/com/alibaba/excel/metadata/format/DataFormatter.java
  22. 12
      easyexcel-core/src/main/java/com/alibaba/excel/read/builder/ExcelReaderBuilder.java
  23. 69
      easyexcel-core/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java
  24. 12
      easyexcel-core/src/main/java/com/alibaba/excel/read/metadata/ReadWorkbook.java
  25. 17
      easyexcel-core/src/main/java/com/alibaba/excel/read/metadata/holder/ReadWorkbookHolder.java
  26. 46
      easyexcel-core/src/main/java/com/alibaba/excel/util/ConverterUtils.java
  27. 71
      easyexcel-core/src/main/java/com/alibaba/excel/util/DateUtils.java
  28. 8
      easyexcel-core/src/main/java/com/alibaba/excel/write/metadata/RowData.java
  29. 68
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/compatibility/CompatibilityTest.java
  30. 52
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/nomodel/NoModelDataTest.java
  31. 143
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/rare/WriteTest.java
  32. 1
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/write/DemoData.java
  33. 35
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/DemoData2.java
  34. 24
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/DemoData3.java
  35. 316
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java
  36. BIN
      easyexcel-test/src/test/resources/compatibility/t04.xlsx
  37. BIN
      easyexcel-test/src/test/resources/compatibility/t05.xlsx
  38. BIN
      easyexcel-test/src/test/resources/compatibility/t06.xlsx
  39. BIN
      easyexcel-test/src/test/resources/compatibility/t07.xlsx
  40. 2
      pom.xml
  41. 10
      update.md

9
.gitee/ISSUE_TEMPLATE.zh-CN.md

@ -0,0 +1,9 @@
# 建议先去看文档
[快速开始](https://easyexcel.opensource.alibaba.com/docs/current/) 、[常见问题](https://easyexcel.opensource.alibaba.com/qa/)
# 异常代码
```java
这里写你的代码
```
# 异常提示
大家尽量把问题一次性描述清楚,然后贴上全部异常,这样方便把问题一次性解决掉。
# 其他描述

8
.github/ISSUE_TEMPLATE/bug.md

@ -7,12 +7,12 @@ assignees: zhuangjiaju
---
**建议先去看文档**
# 建议先去看文档
[快速开始](https://easyexcel.opensource.alibaba.com/docs/current/) 、[常见问题](https://easyexcel.opensource.alibaba.com/qa/)
**触发场景描述**
# 触发场景描述
**触发Bug的代码**
# 触发Bug的代码
```java
这里写代码
```
**提示的异常或者没有达到的效果**
# 提示的异常或者没有达到的效果

10
.github/ISSUE_TEMPLATE/question.md

@ -7,12 +7,12 @@ assignees: ''
---
**建议先去看文档**
# 建议先去看文档
[快速开始](https://easyexcel.opensource.alibaba.com/docs/current/) 、[常见问题](https://easyexcel.opensource.alibaba.com/qa/)
**异常代码**
# 异常代码
```java
这里写你的代码
```
**异常提示**
请提供完整的异常提示,记住是全部异常!
**建议描述**
# 异常提示
大家尽量把问题一次性描述清楚,然后贴上全部异常,这样方便把问题一次性解决掉。
# 问题描述

4
.github/ISSUE_TEMPLATE/suggest.md

@ -7,6 +7,6 @@ assignees: ''
---
**建议先去看文档**
# 建议先去看文档
[快速开始](https://easyexcel.opensource.alibaba.com/docs/current/) 、[常见问题](https://easyexcel.opensource.alibaba.com/qa/)
**建议描述**
# 建议描述

26
.github/workflows/sync2gitee.yml

@ -0,0 +1,26 @@
# 通过 Github action, 在仓库的每一次 commit 后自动同步到 Gitee 上
name: Mirror the Github organization repos to Gitee
on: [push, pull_request]
jobs:
repo-sync:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@main
with:
persist-credentials: false
- name: Mirror the Github organization repos to Gitee.
uses: Yikun/hub-mirror-action@master
with:
# 必选,需要同步的 Github 这里记住选择的是仓库 或者账号 而不是具体的项目
src: github/alibaba
# 必选,需要同步到的 Gitee 这里记住选择的是仓库 或者账号 而不是具体的项目
dst: gitee/easyexcel
# 必选,Gitee公钥对应的私钥,https://gitee.com/profile/sshkeys
dst_key: ${{ secrets.GITEE_PRIVATE_KEY }}
# 必选,Gitee对应的用于创建仓库的token,https://gitee.com/profile/personal_access_tokens
dst_token: ${{ secrets.GITEE_TOKEN }}
# 如果是组织,指定组织即可,默认为用户 user
account_type: org
# 需要同步的仓库里面的项目
static_list: "easyexcel"

184
README.md

@ -4,11 +4,94 @@ EasyExcel
[![Maven central](https://maven-badges.herokuapp.com/maven-central/com.alibaba/easyexcel/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.alibaba/easyexcel)
[![License](http://img.shields.io/:license-apache-brightgreen.svg)](http://www.apache.org/licenses/LICENSE-2.0.html)
# JAVA解析Excel工具
Java解析、生成Excel比较有名的框架有Apache poi、jxl。但他们都存在一个严重的问题就是非常的耗内存,poi有一套SAX模式的API可以一定程度的解决一些内存溢出的问题,但POI还是有一些缺陷,比如07版Excel解压缩以及解压后存储都是在内存中完成的,内存消耗依然很大。
easyexcel重写了poi对07版Excel的解析,一个3M的excel用POI sax解析依然需要100M左右内存,改用easyexcel可以降低到几M,并且再大的excel也不会出现内存溢出;03版依赖POI的sax模式,在上层做了模型转换的封装,让使用者更加简单方便
# 网站
* 官方网站:[https://easyexcel.opensource.alibaba.com/](https://easyexcel.opensource.alibaba.com/)
* github地址:[https://github.com/alibaba/easyexcel](https://github.com/alibaba/easyexcel)
* gitee地址:[https://gitee.com/easyexcel/easyexcel](https://gitee.com/easyexcel/easyexcel)
# 64M内存20秒读取75M(46W行25列)的Excel(3.0.2+版本)
当然还有[极速模式](https://easyexcel.opensource.alibaba.com/qa/read#%E5%BC%80%E5%90%AF%E6%80%A5%E9%80%9F%E6%A8%A1%E5%BC%8F)
能更快,但是内存占用会在100M多一点
![img](img/readme/large.png)
# 最新版本
```xml
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.2.0</version>
</dependency>
```
# 帮忙点个⭐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>
的右上角点个⭐Star,您的支持是使EasyExcel变得更好最大的动力。
# 人员招聘
由于工作较忙,需要招聘一些同学加入`EasyExcel`开源,一起跟着`EasyExcel`成长。
## 你将获得
* 认识一帮热爱开源的小伙伴
* 你写的代码可以被无数人看到,提升自我编码能力
* 可能会有一定的物质奖励(在和公司申请,不一定能过)
## 你的工作
* 群&`issue`答疑
* 做一些代码的`PR`合并去修复bug
* 讨论`EasyExcel`规划
## 招聘要求
* 3年以上Java编程经验,基础扎实
* 对技术比较热爱,并且喜欢阅读源码
* 自驱力强,能主动的研究一些问题
* 需要持之以恒,开源需要长期维护
## 联系方式
直接加钉钉群,联系是仪即可
# 如何获取帮助
## 优先建议自己通过文档来解决问题
* [快速开始](https://easyexcel.opensource.alibaba.com/docs/current/)
* [常见问题](https://easyexcel.opensource.alibaba.com/docs/qa/)
* [API](https://easyexcel.opensource.alibaba.com/docs/current/api/)
## 其次建议通过`issues`来解决解决问题
可以尝试在以下2个链接搜索问题,如果不存在可以尝试创建`issue`。
* 去 [github](https://github.com/alibaba/easyexcel/issues) 搜索`issues`
* 去 [gitee](https://gitee.com/easyexcel/easyexcel/issues) 搜索`issues`
通过 `issues` 解决问题,可以给后面遇到相同问题的同学查看,所以比较推荐这种方式。
不管`github`、`gitee`都会定期有人回答您的问题,比较紧急可以在提完`issue`以后在钉钉群艾特群主并发送`issue`地址帮忙解决。
`QQ` 公司不让用,有时候也会去看,但是核心肯定还是在钉钉。
## 也可以加入钉钉&QQ群来解决问题
加入钉钉或QQ群,看完公告可以获得帮助 。
比较推荐钉钉群,`QQ` 公司不让用,当然QQ群也会有热心网友帮忙解决。
[QQ1群(已满): 662022184](https://jq.qq.com/?_wv=1027&k=1T21jJxh)
[QQ2群(已满): 1097936804](https://jq.qq.com/?_wv=1027&k=j5zEy6Xl)
[QQ3群(已满): 453928496](https://qm.qq.com/cgi-bin/qm/qr?k=e2ULsA5A0GldhV2CXJ8sIbAyu9I6qqs7&jump_from=webapi)
[QQ4群(已满): 496594404](https://qm.qq.com/cgi-bin/qm/qr?k=e_aVG1Q7gi0PJUBkbrUGAgbeO3kUEInK&jump_from=webapi)
[QQ5群: 451925680](https://jq.qq.com/?_wv=1027&k=BbLBIo9P)
[QQ5群(已满): 451925680](https://jq.qq.com/?_wv=1027&k=6VHhvxyf)
[QQ6群: 784741035](https://jq.qq.com/?_wv=1027&k=BbLBIo9P)
[钉钉1群(已满): 21960511](https://qr.dingtalk.com/action/joingroup?code=v1,k1,cchz6k12ci9B08NNqhNRFGXocNVHrZtW0kaOtTKg/Rk=&_dt_no_comment=1&origin=11)
[钉钉2群(已满): 32796397](https://qr.dingtalk.com/action/joingroup?code=v1,k1,jyU9GtEuNU5S0QTyklqYcYJ8qDZtUuTPMM7uPZTS8Hs=&_dt_no_comment=1&origin=11)
[钉钉3群(已满): 33797247](https://qr.dingtalk.com/action/joingroup?code=v1,k1,3UGlEScTGQaHpW2cIRo+gkxJ9EVZ5fz26M6nW3uFP30=&_dt_no_comment=1&origin=11)
@ -17,47 +100,27 @@ EasyExcel
[钉钉6群(已满): 34707941](https://h5.dingtalk.com/circle/healthCheckin.html?dtaction=os&corpId=dingcf68008a1d443ac012d5427bdb061b7a&6ae36c3d-0c80-4=22398493-6c2a-4&cbdbhh=qwertyuiop)
[钉钉7群(已满): 35235427](https://h5.dingtalk.com/circle/healthCheckin.html?dtaction=os&corpId=ding532b9018c06c7fc8660273c4b78e6440&167fb=ed003&cbdbhh=qwertyuiop)
[钉钉8群(已满): 44752220](https://h5.dingtalk.com/circle/healthCheckin.html?dtaction=os&corpId=dingea96808beee421693fd4ba7542d6e5da&0380092a-fa46=a6a40905-7951&cbdbhh=qwertyuiop)
[钉钉9群: 11045002277](https://h5.dingtalk.com/circle/healthCheckin.html?dtaction=os&corpId=dinge338d2215891c0459c13cd6b2cb108a6&6972d=b92f9&cbdbhh=qwertyuiop)
[钉钉9群(已满): 11045002277](https://h5.dingtalk.com/circle/healthCheckin.html?dtaction=os&corpId=dinge338d2215891c0459c13cd6b2cb108a6&6972d=b92f9&cbdbhh=qwertyuiop)
[钉钉10群: 27360019755](https://qr.dingtalk.com/action/joingroup?code=v1,k1,v25LHn2liWmrWUKlkhIzOTcK7s7onp/sZP8mO5oIYSs=&_dt_no_comment=1&origin=11)
[官方网站: https://easyexcel.opensource.alibaba.com/](https://easyexcel.opensource.alibaba.com/)
# 维护者
[常见问题](https://easyexcel.opensource.alibaba.com/qa/)
#### 因为公司不方便用QQ,所以建议加钉钉群
# JAVA解析Excel工具EasyExcel
Java解析、生成Excel比较有名的框架有Apache poi、jxl。但他们都存在一个严重的问题就是非常的耗内存,poi有一套SAX模式的API可以一定程度的解决一些内存溢出的问题,但POI还是有一些缺陷,比如07版Excel解压缩以及解压后存储都是在内存中完成的,内存消耗依然很大。easyexcel重写了poi对07版Excel的解析,一个3M的excel用POI sax解析依然需要100M左右内存,改用easyexcel可以降低到几M,并且再大的excel也不会出现内存溢出;03版依赖POI的sax模式,在上层做了模型转换的封装,让使用者更加简单方便
姬朋飞(玉霄)、庄家钜、怀宇
## 64M内存20秒读取75M(46W行25列)的Excel(3.0.2+版本)
当然还有[极速模式](https://easyexcel.opensource.alibaba.com/qa/read#%E5%BC%80%E5%90%AF%E6%80%A5%E9%80%9F%E6%A8%A1%E5%BC%8F)能更快,但是内存占用会在100M多一点
![img](img/readme/large.png)
# 快速开始
### 最新版本
```xml
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.1.4</version>
</dependency>
```
## 读Excel
## 相关文档
* [快速开始](https://easyexcel.opensource.alibaba.com/docs/current/)
* [更新记事](/update.md)
* [贡献代码](https://easyexcel.opensource.alibaba.com/community/contribute)
## 维护者
姬朋飞(玉霄)、庄家钜、怀宇
## 快速开始
### 读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
/**
* 最简单的读
* <p>1. 创建excel对应的实体对象 参照{@link DemoData}
* <p>2. 由于默认一行行的读取excel,所以需要创建excel一行一行的回调监听器,参照{@link DemoDataListener}
* <p>3. 直接读即可
*/
* 最简单的读
* <p>1. 创建excel对应的实体对象 参照{@link DemoData}
* <p>2. 由于默认一行行的读取excel,所以需要创建excel一行一行的回调监听器,参照{@link DemoDataListener}
* <p>3. 直接读即可
*/
@Test
public void simpleRead() {
String fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx";
@ -66,8 +129,11 @@ DEMO代码地址:[https://github.com/alibaba/easyexcel/blob/master/easyexcel-t
}
```
### 写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)
## 写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)
详细文档地址:[https://easyexcel.opensource.alibaba.com/docs/current/quickstart/write](https://easyexcel.opensource.alibaba.com/docs/current/quickstart/write)
```java
/**
* 最简单的写
@ -76,36 +142,38 @@ DEMO代码地址:[https://github.com/alibaba/easyexcel/blob/master/easyexcel-t
*/
@Test
public void simpleWrite() {
String fileName = TestFileUtil.getPath() + "write" + System.currentTimeMillis() + ".xlsx";
String fileName=TestFileUtil.getPath()+"write"+System.currentTimeMillis()+".xlsx";
// 这里 需要指定写用哪个class去读,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
// 如果这里想使用03 则 传入excelType参数即可
EasyExcel.write(fileName, DemoData.class).sheet("模板").doWrite(data());
EasyExcel.write(fileName,DemoData.class).sheet("模板").doWrite(data());
}
```
### 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)
## 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)
```java
/**
* 文件下载(失败了会返回一个有部分数据的Excel)
* <p>
* 1. 创建excel对应的实体对象 参照{@link DownloadData}
* <p>
* 2. 设置返回的 参数
* <p>
* 3. 直接写,这里注意,finish的时候会自动关闭OutputStream,当然你外面再关闭流问题不大
*/
/**
* 文件下载(失败了会返回一个有部分数据的Excel)
* <p>
* 1. 创建excel对应的实体对象 参照{@link DownloadData}
* <p>
* 2. 设置返回的 参数
* <p>
* 3. 直接写,这里注意,finish的时候会自动关闭OutputStream,当然你外面再关闭流问题不大
*/
@GetMapping("download")
public void download(HttpServletResponse response) throws IOException {
// 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
EasyExcel.write(response.getOutputStream(), DownloadData.class).sheet("模板").doWrite(data());
String fileName=URLEncoder.encode("测试","UTF-8").replaceAll("\\+","%20");
response.setHeader("Content-disposition","attachment;filename*=utf-8''"+fileName+".xlsx");
EasyExcel.write(response.getOutputStream(),DownloadData.class).sheet("模板").doWrite(data());
}
/**
* 文件上传
* <p>1. 创建excel对应的实体对象 参照{@link UploadData}
@ -114,10 +182,8 @@ DEMO代码地址:[https://github.com/alibaba/easyexcel/blob/master/easyexcel-t
*/
@PostMapping("upload")
@ResponseBody
public String upload(MultipartFile file) throws IOException {
EasyExcel.read(file.getInputStream(), UploadData.class, new UploadDataListener(uploadDAO)).sheet().doRead();
return "success";
public String upload(MultipartFile file)throws IOException{
EasyExcel.read(file.getInputStream(),UploadData.class,new UploadDataListener(uploadDAO)).sheet().doRead();
return"success";
}
```
### 联系我们
有问题阿里同事可以通过钉钉找到我,阿里外同学可以通过git留言。其他技术非技术相关的也欢迎一起探讨。
```

123
easyexcel-core/src/main/java/com/alibaba/excel/EasyExcelFactory.java

@ -14,25 +14,10 @@ import com.alibaba.excel.write.builder.ExcelWriterTableBuilder;
/**
* Reader and writer factory class
*
* <h1>Quick start</h1>
* <h2>Read</h2>
* <h3>Sample1</h3>
*
* <h3>Sample2</h3>
*
* <h2>Write</h2>
*
* <h3>Sample1</h3>
*
* <h3>Sample2</h3>
*
*
*
* @author jipengfei
*/
public class EasyExcelFactory {
/**
* Build excel the write
*
@ -45,8 +30,7 @@ public class EasyExcelFactory {
/**
* Build excel the write
*
* @param file
* File to write
* @param file File to write
* @return Excel writer builder
*/
public static ExcelWriterBuilder write(File file) {
@ -56,10 +40,8 @@ public class EasyExcelFactory {
/**
* Build excel the write
*
* @param file
* File to write
* @param head
* Annotate the class for configuration information
* @param file File to write
* @param head Annotate the class for configuration information
* @return Excel writer builder
*/
public static ExcelWriterBuilder write(File file, Class head) {
@ -74,8 +56,7 @@ public class EasyExcelFactory {
/**
* Build excel the write
*
* @param pathName
* File path to write
* @param pathName File path to write
* @return Excel writer builder
*/
public static ExcelWriterBuilder write(String pathName) {
@ -85,10 +66,8 @@ public class EasyExcelFactory {
/**
* Build excel the write
*
* @param pathName
* File path to write
* @param head
* Annotate the class for configuration information
* @param pathName File path to write
* @param head Annotate the class for configuration information
* @return Excel writer builder
*/
public static ExcelWriterBuilder write(String pathName, Class head) {
@ -103,8 +82,7 @@ public class EasyExcelFactory {
/**
* Build excel the write
*
* @param outputStream
* Output stream to write
* @param outputStream Output stream to write
* @return Excel writer builder
*/
public static ExcelWriterBuilder write(OutputStream outputStream) {
@ -114,10 +92,8 @@ public class EasyExcelFactory {
/**
* Build excel the write
*
* @param outputStream
* Output stream to write
* @param head
* Annotate the class for configuration information.
* @param outputStream Output stream to write
* @param head Annotate the class for configuration information.
* @return Excel writer builder
*/
public static ExcelWriterBuilder write(OutputStream outputStream, Class head) {
@ -141,8 +117,7 @@ public class EasyExcelFactory {
/**
* Build excel the <code>writerSheet</code>
*
* @param sheetNo
* Index of sheet,0 base.
* @param sheetNo Index of sheet,0 base.
* @return Excel sheet writer builder.
*/
public static ExcelWriterSheetBuilder writerSheet(Integer sheetNo) {
@ -152,8 +127,7 @@ public class EasyExcelFactory {
/**
* Build excel the 'writerSheet'
*
* @param sheetName
* The name of sheet.
* @param sheetName The name of sheet.
* @return Excel sheet writer builder.
*/
public static ExcelWriterSheetBuilder writerSheet(String sheetName) {
@ -163,10 +137,8 @@ public class EasyExcelFactory {
/**
* Build excel the 'writerSheet'
*
* @param sheetNo
* Index of sheet,0 base.
* @param sheetName
* The name of sheet.
* @param sheetNo Index of sheet,0 base.
* @param sheetName The name of sheet.
* @return Excel sheet writer builder.
*/
public static ExcelWriterSheetBuilder writerSheet(Integer sheetNo, String sheetName) {
@ -192,8 +164,7 @@ public class EasyExcelFactory {
/**
* Build excel the 'writerTable'
*
* @param tableNo
* Index of table,0 base.
* @param tableNo Index of table,0 base.
* @return Excel table writer builder.
*/
public static ExcelWriterTableBuilder writerTable(Integer tableNo) {
@ -216,8 +187,7 @@ public class EasyExcelFactory {
/**
* Build excel the read
*
* @param file
* File to read.
* @param file File to read.
* @return Excel reader builder.
*/
public static ExcelReaderBuilder read(File file) {
@ -227,10 +197,8 @@ public class EasyExcelFactory {
/**
* Build excel the read
*
* @param file
* File to read.
* @param readListener
* Read listener.
* @param file File to read.
* @param readListener Read listener.
* @return Excel reader builder.
*/
public static ExcelReaderBuilder read(File file, ReadListener readListener) {
@ -240,12 +208,9 @@ public class EasyExcelFactory {
/**
* Build excel the read
*
* @param file
* File to read.
* @param head
* Annotate the class for configuration information.
* @param readListener
* Read listener.
* @param file File to read.
* @param head Annotate the class for configuration information.
* @param readListener Read listener.
* @return Excel reader builder.
*/
public static ExcelReaderBuilder read(File file, Class head, ReadListener readListener) {
@ -263,8 +228,7 @@ public class EasyExcelFactory {
/**
* Build excel the read
*
* @param pathName
* File path to read.
* @param pathName File path to read.
* @return Excel reader builder.
*/
public static ExcelReaderBuilder read(String pathName) {
@ -274,10 +238,8 @@ public class EasyExcelFactory {
/**
* Build excel the read
*
* @param pathName
* File path to read.
* @param readListener
* Read listener.
* @param pathName File path to read.
* @param readListener Read listener.
* @return Excel reader builder.
*/
public static ExcelReaderBuilder read(String pathName, ReadListener readListener) {
@ -287,12 +249,9 @@ public class EasyExcelFactory {
/**
* Build excel the read
*
* @param pathName
* File path to read.
* @param head
* Annotate the class for configuration information.
* @param readListener
* Read listener.
* @param pathName File path to read.
* @param head Annotate the class for configuration information.
* @param readListener Read listener.
* @return Excel reader builder.
*/
public static ExcelReaderBuilder read(String pathName, Class head, ReadListener readListener) {
@ -310,8 +269,7 @@ public class EasyExcelFactory {
/**
* Build excel the read
*
* @param inputStream
* Input stream to read.
* @param inputStream Input stream to read.
* @return Excel reader builder.
*/
public static ExcelReaderBuilder read(InputStream inputStream) {
@ -321,10 +279,8 @@ public class EasyExcelFactory {
/**
* Build excel the read
*
* @param inputStream
* Input stream to read.
* @param readListener
* Read listener.
* @param inputStream Input stream to read.
* @param readListener Read listener.
* @return Excel reader builder.
*/
public static ExcelReaderBuilder read(InputStream inputStream, ReadListener readListener) {
@ -334,12 +290,9 @@ public class EasyExcelFactory {
/**
* Build excel the read
*
* @param inputStream
* Input stream to read.
* @param head
* Annotate the class for configuration information.
* @param readListener
* Read listener.
* @param inputStream Input stream to read.
* @param head Annotate the class for configuration information.
* @param readListener Read listener.
* @return Excel reader builder.
*/
public static ExcelReaderBuilder read(InputStream inputStream, Class head, ReadListener readListener) {
@ -366,8 +319,7 @@ public class EasyExcelFactory {
/**
* Build excel the 'readSheet'
*
* @param sheetNo
* Index of sheet,0 base.
* @param sheetNo Index of sheet,0 base.
* @return Excel sheet reader builder.
*/
public static ExcelReaderSheetBuilder readSheet(Integer sheetNo) {
@ -377,8 +329,7 @@ public class EasyExcelFactory {
/**
* Build excel the 'readSheet'
*
* @param sheetName
* The name of sheet.
* @param sheetName The name of sheet.
* @return Excel sheet reader builder.
*/
public static ExcelReaderSheetBuilder readSheet(String sheetName) {
@ -388,10 +339,8 @@ public class EasyExcelFactory {
/**
* Build excel the 'readSheet'
*
* @param sheetNo
* Index of sheet,0 base.
* @param sheetName
* The name of sheet.
* @param sheetNo Index of sheet,0 base.
* @param sheetName The name of sheet.
* @return Excel sheet reader builder.
*/
public static ExcelReaderSheetBuilder readSheet(Integer sheetNo, String sheetName) {

5
easyexcel-core/src/main/java/com/alibaba/excel/analysis/v03/handlers/FormulaRecordHandler.java

@ -5,6 +5,7 @@ import java.util.Map;
import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler;
import com.alibaba.excel.constant.BuiltinFormats;
import com.alibaba.excel.constant.EasyExcelConstants;
import com.alibaba.excel.context.xls.XlsReadContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.enums.RowTypeEnum;
@ -56,7 +57,9 @@ public class FormulaRecordHandler extends AbstractXlsRecordHandler implements Ig
break;
case NUMERIC:
tempCellData.setType(CellDataTypeEnum.NUMBER);
tempCellData.setNumberValue(BigDecimal.valueOf(frec.getValue()));
tempCellData.setOriginalNumberValue(BigDecimal.valueOf(frec.getValue()));
tempCellData.setNumberValue(
tempCellData.getOriginalNumberValue().round(EasyExcelConstants.EXCEL_MATH_CONTEXT));
int dataFormat =
xlsReadContext.xlsReadWorkbookHolder().getFormatTrackingHSSFListener().getFormatIndex(frec);
DataFormatData dataFormatData = new DataFormatData();

3
easyexcel-core/src/main/java/com/alibaba/excel/analysis/v03/handlers/NumberRecordHandler.java

@ -4,6 +4,7 @@ import java.math.BigDecimal;
import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler;
import com.alibaba.excel.constant.BuiltinFormats;
import com.alibaba.excel.constant.EasyExcelConstants;
import com.alibaba.excel.context.xls.XlsReadContext;
import com.alibaba.excel.enums.RowTypeEnum;
import com.alibaba.excel.metadata.data.DataFormatData;
@ -22,7 +23,7 @@ public class NumberRecordHandler extends AbstractXlsRecordHandler implements Ign
@Override
public void processRecord(XlsReadContext xlsReadContext, Record record) {
NumberRecord nr = (NumberRecord)record;
ReadCellData<?> cellData = ReadCellData.newInstance(BigDecimal.valueOf(nr.getValue()), nr.getRow(),
ReadCellData<?> cellData = ReadCellData.newInstanceOriginal(BigDecimal.valueOf(nr.getValue()), nr.getRow(),
(int)nr.getColumn());
short dataFormat = (short)xlsReadContext.xlsReadWorkbookHolder().getFormatTrackingHSSFListener().getFormatIndex(
nr);

5
easyexcel-core/src/main/java/com/alibaba/excel/analysis/v07/handlers/CellTagHandler.java

@ -2,6 +2,7 @@ package com.alibaba.excel.analysis.v07.handlers;
import java.math.BigDecimal;
import com.alibaba.excel.constant.EasyExcelConstants;
import com.alibaba.excel.constant.ExcelXmlConstants;
import com.alibaba.excel.context.xlsx.XlsxReadContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
@ -88,7 +89,9 @@ public class CellTagHandler extends AbstractXlsxTagHandler {
break;
}
tempCellData.setType(CellDataTypeEnum.NUMBER);
tempCellData.setNumberValue(BigDecimal.valueOf(Double.parseDouble(tempDataString)));
tempCellData.setOriginalNumberValue(new BigDecimal(tempDataString));
tempCellData.setNumberValue(
tempCellData.getOriginalNumberValue().round(EasyExcelConstants.EXCEL_MATH_CONTEXT));
break;
default:
throw new IllegalStateException("Cannot set values now");

6
easyexcel-core/src/main/java/com/alibaba/excel/analysis/v07/handlers/sax/SharedStringsTableHandler.java

@ -44,15 +44,18 @@ public class SharedStringsTableHandler extends DefaultHandler {
switch (name) {
case ExcelXmlConstants.SHAREDSTRINGS_T_TAG:
case ExcelXmlConstants.SHAREDSTRINGS_X_T_TAG:
case ExcelXmlConstants.SHAREDSTRINGS_NS2_T_TAG:
currentElementData = null;
isTagt = true;
break;
case ExcelXmlConstants.SHAREDSTRINGS_SI_TAG:
case ExcelXmlConstants.SHAREDSTRINGS_X_SI_TAG:
case ExcelXmlConstants.SHAREDSTRINGS_NS2_SI_TAG:
currentData = null;
break;
case ExcelXmlConstants.SHAREDSTRINGS_RPH_TAG:
case ExcelXmlConstants.SHAREDSTRINGS_X_RPH_TAG:
case ExcelXmlConstants.SHAREDSTRINGS_NS2_RPH_TAG:
ignoreTagt = true;
break;
default:
@ -68,6 +71,7 @@ public class SharedStringsTableHandler extends DefaultHandler {
switch (name) {
case ExcelXmlConstants.SHAREDSTRINGS_T_TAG:
case ExcelXmlConstants.SHAREDSTRINGS_X_T_TAG:
case ExcelXmlConstants.SHAREDSTRINGS_NS2_T_TAG:
if (currentElementData != null) {
if (currentData == null) {
currentData = new StringBuilder();
@ -78,6 +82,7 @@ public class SharedStringsTableHandler extends DefaultHandler {
break;
case ExcelXmlConstants.SHAREDSTRINGS_SI_TAG:
case ExcelXmlConstants.SHAREDSTRINGS_X_SI_TAG:
case ExcelXmlConstants.SHAREDSTRINGS_NS2_SI_TAG:
if (currentData == null) {
readCache.put(null);
} else {
@ -86,6 +91,7 @@ public class SharedStringsTableHandler extends DefaultHandler {
break;
case ExcelXmlConstants.SHAREDSTRINGS_RPH_TAG:
case ExcelXmlConstants.SHAREDSTRINGS_X_RPH_TAG:
case ExcelXmlConstants.SHAREDSTRINGS_NS2_RPH_TAG:
ignoreTagt = false;
break;
default:

10
easyexcel-core/src/main/java/com/alibaba/excel/analysis/v07/handlers/sax/XlsxRowHandler.java

@ -26,33 +26,41 @@ import org.xml.sax.helpers.DefaultHandler;
@Slf4j
public class XlsxRowHandler extends DefaultHandler {
private final XlsxReadContext xlsxReadContext;
private static final Map<String, XlsxTagHandler> XLSX_CELL_HANDLER_MAP = new HashMap<String, XlsxTagHandler>(32);
private static final Map<String, XlsxTagHandler> XLSX_CELL_HANDLER_MAP = new HashMap<>(64);
static {
CellFormulaTagHandler cellFormulaTagHandler = new CellFormulaTagHandler();
XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.CELL_FORMULA_TAG, cellFormulaTagHandler);
XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.X_CELL_FORMULA_TAG, cellFormulaTagHandler);
XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.NS2_CELL_FORMULA_TAG, cellFormulaTagHandler);
CellInlineStringValueTagHandler cellInlineStringValueTagHandler = new CellInlineStringValueTagHandler();
XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.CELL_INLINE_STRING_VALUE_TAG, cellInlineStringValueTagHandler);
XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.X_CELL_INLINE_STRING_VALUE_TAG, cellInlineStringValueTagHandler);
XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.NS2_CELL_INLINE_STRING_VALUE_TAG, cellInlineStringValueTagHandler);
CellTagHandler cellTagHandler = new CellTagHandler();
XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.CELL_TAG, cellTagHandler);
XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.X_CELL_TAG, cellTagHandler);
XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.NS2_CELL_TAG, cellTagHandler);
CellValueTagHandler cellValueTagHandler = new CellValueTagHandler();
XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.CELL_VALUE_TAG, cellValueTagHandler);
XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.X_CELL_VALUE_TAG, cellValueTagHandler);
XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.NS2_CELL_VALUE_TAG, cellValueTagHandler);
CountTagHandler countTagHandler = new CountTagHandler();
XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.DIMENSION_TAG, countTagHandler);
XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.X_DIMENSION_TAG, countTagHandler);
XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.NS2_DIMENSION_TAG, countTagHandler);
HyperlinkTagHandler hyperlinkTagHandler = new HyperlinkTagHandler();
XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.HYPERLINK_TAG, hyperlinkTagHandler);
XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.X_HYPERLINK_TAG, hyperlinkTagHandler);
XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.NS2_HYPERLINK_TAG, hyperlinkTagHandler);
MergeCellTagHandler mergeCellTagHandler = new MergeCellTagHandler();
XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.MERGE_CELL_TAG, mergeCellTagHandler);
XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.X_MERGE_CELL_TAG, mergeCellTagHandler);
XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.NS2_MERGE_CELL_TAG, mergeCellTagHandler);
RowTagHandler rowTagHandler = new RowTagHandler();
XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.ROW_TAG, rowTagHandler);
XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.X_ROW_TAG, rowTagHandler);
XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.NS2_ROW_TAG, rowTagHandler);
}
public XlsxRowHandler(XlsxReadContext xlsxReadContext) {

19
easyexcel-core/src/main/java/com/alibaba/excel/constant/EasyExcelConstants.java

@ -0,0 +1,19 @@
package com.alibaba.excel.constant;
import java.math.MathContext;
import java.math.RoundingMode;
/**
* Used to store constant
*
* @author Jiaju Zhuang
*/
public class EasyExcelConstants {
/**
* Excel by default with 15 to store Numbers, and the double in Java can use to store number 17, led to the accuracy
* will be a problem. So you need to set up 15 to deal with precision
*/
public static final MathContext EXCEL_MATH_CONTEXT = new MathContext(15, RoundingMode.HALF_UP);
}

17
easyexcel-core/src/main/java/com/alibaba/excel/constant/ExcelXmlConstants.java

@ -17,16 +17,28 @@ public class ExcelXmlConstants {
public static final String HYPERLINK_TAG = "hyperlink";
public static final String X_DIMENSION_TAG = "x:dimension";
public static final String NS2_DIMENSION_TAG = "ns2:dimension";
public static final String X_ROW_TAG = "x:row";
public static final String NS2_ROW_TAG = "ns2:row";
public static final String X_CELL_FORMULA_TAG = "x:f";
public static final String NS2_CELL_FORMULA_TAG = "ns2:f";
public static final String X_CELL_VALUE_TAG = "x:v";
public static final String NS2_CELL_VALUE_TAG = "ns2:v";
/**
* When the data is "inlineStr" his tag is "t"
*/
public static final String X_CELL_INLINE_STRING_VALUE_TAG = "x:t";
public static final String NS2_CELL_INLINE_STRING_VALUE_TAG = "ns2:t";
public static final String X_CELL_TAG = "x:c";
public static final String NS2_CELL_TAG = "ns2:c";
public static final String X_MERGE_CELL_TAG = "x:mergeCell";
public static final String NS2_MERGE_CELL_TAG = "ns2:mergeCell";
public static final String X_HYPERLINK_TAG = "x:hyperlink";
public static final String NS2_HYPERLINK_TAG = "ns2:hyperlink";
/**
* s attribute
@ -66,17 +78,22 @@ public class ExcelXmlConstants {
*/
public static final String SHAREDSTRINGS_T_TAG = "t";
public static final String SHAREDSTRINGS_X_T_TAG = "x:t";
public static final String SHAREDSTRINGS_NS2_T_TAG = "ns2:t";
/**
* SharedStringItem
*/
public static final String SHAREDSTRINGS_SI_TAG = "si";
public static final String SHAREDSTRINGS_X_SI_TAG = "x:si";
public static final String SHAREDSTRINGS_NS2_SI_TAG = "ns2:si";
/**
* Mac 2016 2017 will have this extra field to ignore
*/
public static final String SHAREDSTRINGS_RPH_TAG = "rPh";
public static final String SHAREDSTRINGS_X_RPH_TAG = "x:rPh";
public static final String SHAREDSTRINGS_NS2_RPH_TAG = "ns2:rPh";
}

9
easyexcel-core/src/main/java/com/alibaba/excel/converters/date/DateNumberConverter.java

@ -9,6 +9,7 @@ import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.DateUtils;
import org.apache.poi.ss.usermodel.DateUtil;
@ -33,11 +34,11 @@ public class DateNumberConverter implements Converter<Date> {
public Date convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
return DateUtil.getJavaDate(cellData.getNumberValue().doubleValue(),
globalConfiguration.getUse1904windowing(), null);
return DateUtils.getJavaDate(cellData.getNumberValue().doubleValue(),
globalConfiguration.getUse1904windowing());
} else {
return DateUtil.getJavaDate(cellData.getNumberValue().doubleValue(),
contentProperty.getDateTimeFormatProperty().getUse1904windowing(), null);
return DateUtils.getJavaDate(cellData.getNumberValue().doubleValue(),
contentProperty.getDateTimeFormatProperty().getUse1904windowing());
}
}

5
easyexcel-core/src/main/java/com/alibaba/excel/converters/localdatetime/LocalDateNumberConverter.java

@ -9,6 +9,7 @@ import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.DateUtils;
import org.apache.poi.ss.usermodel.DateUtil;
@ -33,10 +34,10 @@ public class LocalDateNumberConverter implements Converter<LocalDateTime> {
public LocalDateTime convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
return DateUtil.getLocalDateTime(cellData.getNumberValue().doubleValue(),
return DateUtils.getLocalDateTime(cellData.getNumberValue().doubleValue(),
globalConfiguration.getUse1904windowing());
} else {
return DateUtil.getLocalDateTime(cellData.getNumberValue().doubleValue(),
return DateUtils.getLocalDateTime(cellData.getNumberValue().doubleValue(),
contentProperty.getDateTimeFormatProperty().getUse1904windowing());
}
}

5
easyexcel-core/src/main/java/com/alibaba/excel/converters/string/StringNumberConverter.java

@ -37,9 +37,8 @@ public class StringNumberConverter implements Converter<String> {
GlobalConfiguration globalConfiguration) {
// If there are "DateTimeFormat", read as date
if (contentProperty != null && contentProperty.getDateTimeFormatProperty() != null) {
return DateUtils.format(
DateUtil.getJavaDate(cellData.getNumberValue().doubleValue(),
contentProperty.getDateTimeFormatProperty().getUse1904windowing(), null),
return DateUtils.format(cellData.getNumberValue(),
contentProperty.getDateTimeFormatProperty().getUse1904windowing(),
contentProperty.getDateTimeFormatProperty().getFormat());
}
// If there are "NumberFormat", read as number

40
easyexcel-core/src/main/java/com/alibaba/excel/enums/ReadDefaultReturnEnum.java

@ -0,0 +1,40 @@
package com.alibaba.excel.enums;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import com.alibaba.excel.metadata.data.CellData;
import com.alibaba.excel.util.StringUtils;
/**
* Read not to {@code com.alibaba.excel.metadata.BasicParameter#clazz} value, the default will return type.
*
* @author Jiaju Zhuang
*/
public enum ReadDefaultReturnEnum {
/**
* default.The content of cells into string, is the same as you see in the excel.
*/
STRING,
/**
* Returns the actual type.
* Will be automatically selected according to the cell contents what return type, will return the following class:
* <ol>
* <li>{@link BigDecimal}</li>
* <li>{@link Boolean}</li>
* <li>{@link String}</li>
* <li>{@link LocalDateTime}</li>
* </ol>
*/
ACTUAL_DATA,
/**
* Return to {@link com.alibaba.excel.metadata.data.ReadCellData}, can decide which field you need.
*/
READ_CELL_DATA,
;
}

3
easyexcel-core/src/main/java/com/alibaba/excel/metadata/data/CellData.java

@ -1,6 +1,7 @@
package com.alibaba.excel.metadata.data;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.AbstractCell;
@ -57,6 +58,7 @@ public class CellData<T> extends AbstractCell {
}
switch (type) {
case STRING:
case DIRECT_STRING:
case ERROR:
if (StringUtils.isEmpty(stringValue)) {
type = CellDataTypeEnum.EMPTY;
@ -76,5 +78,4 @@ public class CellData<T> extends AbstractCell {
}
}
}

37
easyexcel-core/src/main/java/com/alibaba/excel/metadata/data/ReadCellData.java

@ -1,7 +1,9 @@
package com.alibaba.excel.metadata.data;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import com.alibaba.excel.constant.EasyExcelConstants;
import com.alibaba.excel.enums.CellDataTypeEnum;
import lombok.EqualsAndHashCode;
@ -11,6 +13,7 @@ import lombok.Setter;
/**
* read cell data
* <p>
*
* @author Jiaju Zhuang
*/
@ -19,6 +22,29 @@ import lombok.Setter;
@EqualsAndHashCode
@NoArgsConstructor
public class ReadCellData<T> extends CellData<T> {
/**
* originalNumberValue vs numberValue
* <ol>
* <li>
* NUMBER
* originalNumberValue: Original data and the accuracy of his is 17, but in fact the excel only 15 precision to
* process the data
* numberValue: After correction of the data and the accuracy of his is 15
* for example, originalNumberValue = `2087.0249999999996` , numberValue = `2087.03`
* </li>
* <li>
* DATE
* originalNumberValue: Storage is a data type double, accurate to milliseconds
* dateValue: Based on double converted to a date format, he will revised date difference, accurate to seconds
* for example, originalNumberValue = `44729.99998836806` ,time is:`2022-06-17 23:59:58.995`,
* But in excel is displayed:` 2022-06-17 23:59:59`, dateValue = `2022-06-17 23:59:59`
* </li>
* </ol>
* {@link CellDataTypeEnum#NUMBER} {@link CellDataTypeEnum#DATE}
*/
private BigDecimal originalNumberValue;
/**
* data format.
*/
@ -107,11 +133,21 @@ public class ReadCellData<T> extends CellData<T> {
return cellData;
}
public static ReadCellData<?> newInstanceOriginal(BigDecimal numberValue, Integer rowIndex, Integer columnIndex) {
ReadCellData<?> cellData = new ReadCellData<>(numberValue);
cellData.setRowIndex(rowIndex);
cellData.setColumnIndex(columnIndex);
cellData.setOriginalNumberValue(numberValue);
cellData.setNumberValue(numberValue.round(EasyExcelConstants.EXCEL_MATH_CONTEXT));
return cellData;
}
@Override
public ReadCellData<Object> clone() {
ReadCellData<Object> readCellData = new ReadCellData<>();
readCellData.setType(getType());
readCellData.setNumberValue(getNumberValue());
readCellData.setOriginalNumberValue(getOriginalNumberValue());
readCellData.setStringValue(getStringValue());
readCellData.setBooleanValue(getBooleanValue());
readCellData.setData(getData());
@ -123,5 +159,4 @@ public class ReadCellData<T> extends CellData<T> {
}
return readCellData;
}
}

8
easyexcel-core/src/main/java/com/alibaba/excel/metadata/format/DataFormatter.java

@ -212,10 +212,10 @@ public class DataFormatter {
CellFormat cfmt = CellFormat.getInstance(locale, formatStr);
// CellFormat requires callers to identify date vs not, so do so
Object cellValueO = data;
if (DateUtil.isADateFormat(dataFormat, formatStr) &&
if (DateUtils.isADateFormat(dataFormat, formatStr) &&
// don't try to handle Date value 0, let a 3 or 4-part format take care of it
data.doubleValue() != 0.0) {
cellValueO = DateUtil.getJavaDate(data, use1904windowing);
cellValueO = DateUtils.getJavaDate(data, use1904windowing);
}
// Wrap and return (non-cachable - CellFormat does that)
return new CellFormatResultWrapper(cfmt.apply(cellValueO));
@ -243,6 +243,8 @@ public class DataFormatter {
return format;
}
private Format createFormat(Short dataFormat, String dataFormatString) {
String formatStr = dataFormatString;
@ -628,7 +630,7 @@ public class DataFormatter {
// Hint about the raw excel value
((ExcelStyleDateFormatter)dateFormat).setDateToBeFormatted(data);
}
return performDateFormatting(DateUtil.getJavaDate(data, use1904windowing), dateFormat);
return performDateFormatting(DateUtils.getJavaDate(data, use1904windowing), dateFormat);
}
/**

12
easyexcel-core/src/main/java/com/alibaba/excel/read/builder/ExcelReaderBuilder.java

@ -13,6 +13,7 @@ import com.alibaba.excel.cache.ReadCache;
import com.alibaba.excel.cache.selector.ReadCacheSelector;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.enums.CellExtraTypeEnum;
import com.alibaba.excel.enums.ReadDefaultReturnEnum;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.event.SyncReadListener;
import com.alibaba.excel.read.listener.ModelBuildEventListener;
@ -198,6 +199,17 @@ public class ExcelReaderBuilder extends AbstractExcelReaderParameterBuilder<Exce
return this;
}
/**
* Read not to {@code com.alibaba.excel.metadata.BasicParameter#clazz} value, the default will return type.
* Is only effective when set `useDefaultListener=true` or `useDefaultListener=null`.
*
* @see ReadDefaultReturnEnum
*/
public ExcelReaderBuilder readDefaultReturn(ReadDefaultReturnEnum readDefaultReturn) {
readWorkbook.setReadDefaultReturn(readDefaultReturn);
return this;
}
public ExcelReader build() {
return new ExcelReader(readWorkbook);
}

69
easyexcel-core/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java

@ -1,21 +1,30 @@
package com.alibaba.excel.read.listener;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Map;
import javax.print.DocFlavor.STRING;
import com.alibaba.excel.constant.EasyExcelConstants;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.enums.HeadKindEnum;
import com.alibaba.excel.enums.ReadDefaultReturnEnum;
import com.alibaba.excel.exception.ExcelDataConvertException;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.data.DataFormatData;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.read.metadata.holder.ReadSheetHolder;
import com.alibaba.excel.read.metadata.property.ExcelReadHeadProperty;
import com.alibaba.excel.util.BeanMapUtils;
import com.alibaba.excel.util.BooleanUtils;
import com.alibaba.excel.util.ClassUtils;
import com.alibaba.excel.util.ConverterUtils;
import com.alibaba.excel.util.DateUtils;
import com.alibaba.excel.util.MapUtils;
import com.alibaba.excel.util.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.cglib.beans.BeanMap;
/**
@ -33,13 +42,13 @@ public class ModelBuildEventListener implements IgnoreExceptionReadListener<Map<
.setCurrentRowAnalysisResult(buildUserModel(cellDataMap, readSheetHolder, context));
return;
}
context.readRowHolder().setCurrentRowAnalysisResult(buildStringList(cellDataMap, readSheetHolder, context));
context.readRowHolder().setCurrentRowAnalysisResult(buildNoModel(cellDataMap, readSheetHolder, context));
}
private Object buildStringList(Map<Integer, ReadCellData<?>> cellDataMap, ReadSheetHolder readSheetHolder,
private Object buildNoModel(Map<Integer, ReadCellData<?>> cellDataMap, ReadSheetHolder readSheetHolder,
AnalysisContext context) {
int index = 0;
Map<Integer, String> map = MapUtils.newLinkedHashMapWithExpectedSize(cellDataMap.size());
Map<Integer, Object> map = MapUtils.newLinkedHashMapWithExpectedSize(cellDataMap.size());
for (Map.Entry<Integer, ReadCellData<?>> entry : cellDataMap.entrySet()) {
Integer key = entry.getKey();
ReadCellData<?> cellData = entry.getValue();
@ -48,9 +57,23 @@ public class ModelBuildEventListener implements IgnoreExceptionReadListener<Map<
index++;
}
index++;
map.put(key,
(String)ConverterUtils.convertToJavaObject(cellData, null, null, readSheetHolder.converterMap(),
context, context.readRowHolder().getRowIndex(), key));
ReadDefaultReturnEnum readDefaultReturn = context.readWorkbookHolder().getReadDefaultReturn();
if (readDefaultReturn == ReadDefaultReturnEnum.STRING) {
// string
map.put(key,
(String)ConverterUtils.convertToJavaObject(cellData, null, null, readSheetHolder.converterMap(),
context, context.readRowHolder().getRowIndex(), key));
} else {
// retrun ReadCellData
ReadCellData<?> convertedReadCellData = convertReadCellData(cellData,
context.readWorkbookHolder().getReadDefaultReturn(), readSheetHolder, context, key);
if (readDefaultReturn == ReadDefaultReturnEnum.READ_CELL_DATA) {
map.put(key, convertedReadCellData);
} else {
map.put(key, convertedReadCellData.getData());
}
}
}
// fix https://github.com/alibaba/easyexcel/issues/2014
int headSize = calculateHeadSize(readSheetHolder);
@ -61,6 +84,38 @@ public class ModelBuildEventListener implements IgnoreExceptionReadListener<Map<
return map;
}
private ReadCellData convertReadCellData(ReadCellData<?> cellData, ReadDefaultReturnEnum readDefaultReturn,
ReadSheetHolder readSheetHolder, AnalysisContext context, Integer columnIndex) {
Class<?> classGeneric;
switch (cellData.getType()) {
case STRING:
case DIRECT_STRING:
case ERROR:
case EMPTY:
classGeneric = String.class;
break;
case BOOLEAN:
classGeneric = Boolean.class;
break;
case NUMBER:
DataFormatData dataFormatData = cellData.getDataFormatData();
if (dataFormatData != null && DateUtils.isADateFormat(dataFormatData.getIndex(),
dataFormatData.getFormat())) {
classGeneric = LocalDateTime.class;
} else {
classGeneric = BigDecimal.class;
}
break;
default:
classGeneric = ConverterUtils.defaultClassGeneric;
break;
}
return (ReadCellData)ConverterUtils.convertToJavaObject(cellData, null, ReadCellData.class,
classGeneric, null, readSheetHolder.converterMap(), context, context.readRowHolder().getRowIndex(),
columnIndex);
}
private int calculateHeadSize(ReadSheetHolder readSheetHolder) {
if (readSheetHolder.excelReadHeadProperty().getHeadMap().size() > 0) {
return readSheetHolder.excelReadHeadProperty().getHeadMap().size();

12
easyexcel-core/src/main/java/com/alibaba/excel/read/metadata/ReadWorkbook.java

@ -11,6 +11,7 @@ import com.alibaba.excel.cache.ReadCache;
import com.alibaba.excel.cache.selector.ReadCacheSelector;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.enums.CellExtraTypeEnum;
import com.alibaba.excel.enums.ReadDefaultReturnEnum;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.read.listener.ModelBuildEventListener;
import com.alibaba.excel.support.ExcelTypeEnum;
@ -62,7 +63,6 @@ public class ReadWorkbook extends ReadBasicParameter {
/**
* This object can be read in the Listener {@link AnalysisEventListener#invoke(Object, AnalysisContext)}
* {@link AnalysisContext#getCustom()}
*
*/
private Object customObject;
/**
@ -96,8 +96,18 @@ public class ReadWorkbook extends ReadBasicParameter {
* Whether to use the default listener, which is used by default.
* <p>
* The {@link ModelBuildEventListener} is loaded by default to convert the object.
* defualt is true.
*/
private Boolean useDefaultListener;
/**
* Read not to {@code com.alibaba.excel.metadata.BasicParameter#clazz} value, the default will return type.
* Is only effective when set `useDefaultListener=true` or `useDefaultListener=null`.
*
* @see ReadDefaultReturnEnum
*/
private ReadDefaultReturnEnum readDefaultReturn;
/**
* Read some additional fields. None are read by default.
*

17
easyexcel-core/src/main/java/com/alibaba/excel/read/metadata/holder/ReadWorkbookHolder.java

@ -14,6 +14,7 @@ import com.alibaba.excel.cache.selector.SimpleReadCacheSelector;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.enums.CellExtraTypeEnum;
import com.alibaba.excel.enums.HolderEnum;
import com.alibaba.excel.enums.ReadDefaultReturnEnum;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.exception.ExcelAnalysisException;
import com.alibaba.excel.read.metadata.ReadSheet;
@ -64,10 +65,20 @@ public class ReadWorkbookHolder extends AbstractReadHolder {
* if false, Will transfer 'inputStream' to temporary files to improve efficiency
*/
private Boolean mandatoryUseInputStream;
/**
* Default true
*/
private Boolean autoCloseStream;
/**
* Read not to {@code com.alibaba.excel.metadata.BasicParameter#clazz} value, the default will return type.
* Is only effective when set `useDefaultListener=true` or `useDefaultListener=null`.
*
* @see ReadDefaultReturnEnum
*/
private ReadDefaultReturnEnum readDefaultReturn;
/**
* Excel type
*/
@ -146,6 +157,12 @@ public class ReadWorkbookHolder extends AbstractReadHolder {
this.autoCloseStream = readWorkbook.getAutoCloseStream();
}
if (readWorkbook.getReadDefaultReturn() == null) {
this.readDefaultReturn = ReadDefaultReturnEnum.STRING;
} else {
this.readDefaultReturn = readWorkbook.getReadDefaultReturn();
}
this.customObject = readWorkbook.getCustomObject();
if (readWorkbook.getIgnoreEmptyRow() == null) {
this.ignoreEmptyRow = Boolean.TRUE;

46
easyexcel-core/src/main/java/com/alibaba/excel/util/ConverterUtils.java

@ -84,24 +84,52 @@ public class ConverterUtils {
public static Object convertToJavaObject(ReadCellData<?> cellData, Field field,
ExcelContentProperty contentProperty, Map<ConverterKey, Converter<?>> converterMap, AnalysisContext context,
Integer rowIndex, Integer columnIndex) {
Class<?> clazz;
if (field == null) {
clazz = String.class;
} else {
clazz = field.getType();
return convertToJavaObject(cellData, field, null, null, contentProperty, converterMap, context, rowIndex,
columnIndex);
}
/**
* Convert it into a Java object
*
* @param cellData
* @param field
* @param clazz
* @param contentProperty
* @param converterMap
* @param context
* @param rowIndex
* @param columnIndex
* @return
*/
public static Object convertToJavaObject(ReadCellData<?> cellData, Field field, Class<?> clazz,
Class<?> classGeneric, ExcelContentProperty contentProperty, Map<ConverterKey, Converter<?>> converterMap,
AnalysisContext context, Integer rowIndex, Integer columnIndex) {
if (clazz == null) {
if (field == null) {
clazz = String.class;
} else {
clazz = field.getType();
}
}
if (clazz == CellData.class || clazz == ReadCellData.class) {
Class<?> classGeneric = getClassGeneric(field.getGenericType());
ReadCellData<Object> cellDataReturn = cellData.clone();
cellDataReturn.setData(doConvertToJavaObject(cellData, classGeneric, contentProperty, converterMap,
context, rowIndex, columnIndex));
cellDataReturn.setData(
doConvertToJavaObject(cellData, getClassGeneric(field, classGeneric), contentProperty,
converterMap, context, rowIndex, columnIndex));
return cellDataReturn;
}
return doConvertToJavaObject(cellData, clazz, contentProperty, converterMap, context, rowIndex,
columnIndex);
}
private static Class<?> getClassGeneric(Type type) {
private static Class<?> getClassGeneric(Field field, Class<?> classGeneric) {
if (classGeneric != null) {
return classGeneric;
}
if (field == null) {
return defaultClassGeneric;
}
Type type = field.getGenericType();
if (!(type instanceof ParameterizedType)) {
return defaultClassGeneric;
}

71
easyexcel-core/src/main/java/com/alibaba/excel/util/DateUtils.java

@ -1,9 +1,11 @@
package com.alibaba.excel.util;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
@ -11,6 +13,8 @@ import java.util.Locale;
import java.util.Map;
import java.util.regex.Pattern;
import org.apache.poi.ss.usermodel.DateUtil;
/**
* Date utils
*
@ -184,6 +188,33 @@ public class DateUtils {
}
}
/**
* Format date
*
* @param date
* @param dateFormat
* @return
*/
public static String format(LocalDateTime date, String dateFormat) {
return format(date, dateFormat, null);
}
/**
* Format date
*
* @param date
* @param dateFormat
* @return
*/
public static String format(BigDecimal date, Boolean use1904windowing, String dateFormat) {
if (date == null) {
return null;
}
LocalDateTime localDateTime = DateUtil.getLocalDateTime(date.doubleValue(),
BooleanUtils.isTrue(use1904windowing), true);
return format(localDateTime, dateFormat);
}
private static DateFormat getCacheDateFormat(String dateFormat) {
Map<String, SimpleDateFormat> dateFormatMap = DATE_FORMAT_THREAD_LOCAL.get();
if (dateFormatMap == null) {
@ -200,6 +231,46 @@ public class DateUtils {
return simpleDateFormat;
}
/**
* Given an Excel date with either 1900 or 1904 date windowing,
* converts it to a java.util.Date.
*
* Excel Dates and Times are stored without any timezone
* information. If you know (through other means) that your file
* uses a different TimeZone to the system default, you can use
* this version of the getJavaDate() method to handle it.
*
* @param date The Excel date.
* @param use1904windowing true if date uses 1904 windowing,
* or false if using 1900 date windowing.
* @return Java representation of the date, or null if date is not a valid Excel date
*/
public static Date getJavaDate(double date, boolean use1904windowing) {
//To calculate the Date, in the use of `org.apache.poi.ss.usermodel.DateUtil.getJavaDate(double, boolean,
// java.util.TimeZone, boolean), Date when similar `2023-01-01 00:00:00.500`, returns the`2023-01-01
// 00:00:01`, but excel in fact shows the `2023-01-01 00:00:00`.
// `org.apache.poi.ss.usermodel.DateUtil.getLocalDateTime(double, boolean, boolean)` There is no problem.
return Date.from(getLocalDateTime(date, use1904windowing).atZone(ZoneId.systemDefault()).toInstant());
}
/**
* Given an Excel date with either 1900 or 1904 date windowing,
* converts it to a java.time.LocalDateTime.
*
* Excel Dates and Times are stored without any timezone
* information. If you know (through other means) that your file
* uses a different TimeZone to the system default, you can use
* this version of the getJavaDate() method to handle it.
*
* @param date The Excel date.
* @param use1904windowing true if date uses 1904 windowing,
* or false if using 1900 date windowing.
* @return Java representation of the date, or null if date is not a valid Excel date
*/
public static LocalDateTime getLocalDateTime(double date, boolean use1904windowing) {
return DateUtil.getLocalDateTime(date, use1904windowing, true);
}
/**
* Determine if it is a date format.
*

8
easyexcel-core/src/main/java/com/alibaba/excel/write/metadata/RowData.java

@ -18,17 +18,17 @@ public interface RowData {
/**
* Returns the number of elements in this collection. If this collection
* contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
* <tt>Integer.MAX_VALUE</tt>.
* contains more than <code>Integer.MAX_VALUE</code> elements, returns
* <code>Integer.MAX_VALUE</code>.
*
* @return the number of elements in this collection
*/
int size();
/**
* Returns <tt>true</tt> if this collection contains no elements.
* Returns <code>true</code> if this collection contains no elements.
*
* @return <tt>true</tt> if this collection contains no elements
* @return <code>true</code> if this collection contains no elements
*/
boolean isEmpty();

68
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/compatibility/CompatibilityTest.java

@ -1,10 +1,13 @@
package com.alibaba.easyexcel.test.core.compatibility;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.constant.EasyExcelConstants;
import com.alibaba.excel.enums.ReadDefaultReturnEnum;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
@ -45,11 +48,72 @@ public class CompatibilityTest {
@Test
public void t03() {
// Exist in `sharedStrings.xml` `x:t` start tag, need to be compatible
List<Map<Integer, Object>> list = EasyExcel.read(TestFileUtil.getPath() + "compatibility/t03.xlsx").sheet().doReadSync();
// In the presence of the first line of a lot of null columns, ignore null columns
List<Map<Integer, Object>> list = EasyExcel.read(TestFileUtil.getPath() + "compatibility/t03.xlsx").sheet()
.doReadSync();
log.info("data:{}", JSON.toJSONString(list));
Assert.assertEquals(1, list.size());
Map<Integer, Object> row0 = list.get(0);
Assert.assertEquals(12, row0.size());
}
@Test
public void t04() {
// Exist in `sheet1.xml` `ns2:t` start tag, need to be compatible
List<Map<Integer, Object>> list = EasyExcel.read(TestFileUtil.getPath() + "compatibility/t04.xlsx").sheet()
.doReadSync();
log.info("data:{}", JSON.toJSONString(list));
Assert.assertEquals(56, list.size());
Map<Integer, Object> row0 = list.get(0);
Assert.assertEquals("QQSJK28F152A012242S0081", row0.get(5));
}
@Test
public void t05() {
// https://github.com/alibaba/easyexcel/issues/1956
// Excel read date needs to be rounded
List<Map<Integer, String>> list = EasyExcel
.read(TestFileUtil.getPath() + "compatibility/t05.xlsx")
.sheet()
.doReadSync();
log.info("data:{}", JSON.toJSONString(list));
Assert.assertEquals("2023-01-01 00:00:00", list.get(0).get(0));
Assert.assertEquals("2023-01-01 00:00:00", list.get(1).get(0));
Assert.assertEquals("2023-01-01 00:00:00", list.get(2).get(0));
Assert.assertEquals("2023-01-01 00:00:01", list.get(3).get(0));
Assert.assertEquals("2023-01-01 00:00:01", list.get(4).get(0));
}
@Test
public void t06() {
// Keep error precision digital format
List<Map<Integer, String>> list = EasyExcel
.read(TestFileUtil.getPath() + "compatibility/t06.xlsx")
.headRowNumber(0)
.sheet()
.doReadSync();
log.info("data:{}", JSON.toJSONString(list));
Assert.assertEquals("2087.03", list.get(0).get(2));
}
@Test
public void t07() {
// https://github.com/alibaba/easyexcel/issues/2805
// Excel read date needs to be rounded
List<Map<Integer, Object>> list = EasyExcel
.read(TestFileUtil.getPath() + "compatibility/t07.xlsx")
.readDefaultReturn(ReadDefaultReturnEnum.ACTUAL_DATA)
.sheet()
.doReadSync();
log.info("data:{}", JSON.toJSONString(list));
Assert.assertEquals(0, new BigDecimal("24.1998124").compareTo((BigDecimal)list.get(0).get(11)));
list = EasyExcel
.read(TestFileUtil.getPath() + "compatibility/t07.xlsx")
.sheet()
.doReadSync();
log.info("data:{}", JSON.toJSONString(list));
Assert.assertEquals("24.20", list.get(0).get(11));
}
}

52
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/nomodel/NoModelDataTest.java

@ -1,14 +1,20 @@
package com.alibaba.easyexcel.test.core.nomodel;
import java.io.File;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.enums.ReadDefaultReturnEnum;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.util.DateUtils;
import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
@ -19,6 +25,7 @@ import org.junit.runners.MethodSorters;
* @author Jiaju Zhuang
*/
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@Slf4j
public class NoModelDataTest {
private static File file07;
@ -28,7 +35,6 @@ public class NoModelDataTest {
private static File fileRepeat03;
private static File fileRepeatCsv;
@BeforeClass
public static void init() {
file07 = TestFileUtil.createNewFile("noModel07.xlsx");
@ -41,20 +47,20 @@ public class NoModelDataTest {
@Test
public void t01ReadAndWrite07() throws Exception {
readAndWrite(file07, fileRepeat07);
readAndWrite(file07, fileRepeat07, false);
}
@Test
public void t02ReadAndWrite03() throws Exception {
readAndWrite(file03, fileRepeat03);
readAndWrite(file03, fileRepeat03, false);
}
@Test
public void t03ReadAndWriteCsv() throws Exception {
readAndWrite(fileCsv, fileRepeatCsv);
readAndWrite(fileCsv, fileRepeatCsv, true);
}
private void readAndWrite(File file, File fileRepeat) throws Exception {
private void readAndWrite(File file, File fileRepeat, boolean isCsv) throws Exception {
EasyExcel.write(file).sheet().doWrite(data());
List<Map<Integer, String>> result = EasyExcel.read(file).headRowNumber(0).sheet().doReadSync();
Assert.assertEquals(10, result.size());
@ -63,6 +69,42 @@ public class NoModelDataTest {
Assert.assertEquals("109", data10.get(1));
Assert.assertEquals("2020-01-01 01:01:01", data10.get(2));
List<Map<Integer, Object>> actualDataList = EasyExcel.read(file)
.headRowNumber(0)
.readDefaultReturn(ReadDefaultReturnEnum.ACTUAL_DATA)
.sheet()
.doReadSync();
log.info("actualDataList:{}", JSON.toJSONString(actualDataList));
Assert.assertEquals(10, actualDataList.size());
Map<Integer, Object> actualData10 = actualDataList.get(9);
Assert.assertEquals("string19", actualData10.get(0));
if (isCsv) {
// CSV only string type
Assert.assertEquals("109", actualData10.get(1));
Assert.assertEquals("2020-01-01 01:01:01", actualData10.get(2));
} else {
Assert.assertEquals(0, new BigDecimal("109").compareTo((BigDecimal)actualData10.get(1)));
Assert.assertEquals(LocalDateTime.of(2020, 1, 1, 1, 1, 1), actualData10.get(2));
}
List<Map<Integer, ReadCellData<?>>> readCellDataList = EasyExcel.read(file)
.headRowNumber(0)
.readDefaultReturn(ReadDefaultReturnEnum.READ_CELL_DATA)
.sheet()
.doReadSync();
log.info("readCellDataList:{}", JSON.toJSONString(readCellDataList));
Assert.assertEquals(10, readCellDataList.size());
Map<Integer, ReadCellData<?>> readCellData10 = readCellDataList.get(9);
Assert.assertEquals("string19", readCellData10.get(0).getData());
if (isCsv) {
// CSV only string type
Assert.assertEquals("109", readCellData10.get(1).getData());
Assert.assertEquals("2020-01-01 01:01:01", readCellData10.get(2).getData());
} else {
Assert.assertEquals(0, new BigDecimal("109").compareTo((BigDecimal)readCellData10.get(1).getData()));
Assert.assertEquals(LocalDateTime.of(2020, 1, 1, 1, 1, 1), readCellData10.get(2).getData());
}
EasyExcel.write(fileRepeat).sheet().doWrite(result);
result = EasyExcel.read(fileRepeat).headRowNumber(0).sheet().doReadSync();
Assert.assertEquals(10, result.size());

143
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/rare/WriteTest.java

@ -0,0 +1,143 @@
package com.alibaba.easyexcel.test.demo.rare;
import java.io.File;
import java.util.Date;
import java.util.List;
import com.alibaba.easyexcel.test.demo.write.DemoData;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.util.FileUtils;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.excel.write.handler.RowWriteHandler;
import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.handler.WorkbookWriteHandler;
import com.alibaba.excel.write.handler.context.RowWriteHandlerContext;
import com.alibaba.excel.write.handler.context.SheetWriteHandlerContext;
import com.alibaba.excel.write.handler.context.WorkbookWriteHandlerContext;
import com.alibaba.excel.write.metadata.WriteSheet;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.junit.Ignore;
import org.junit.Test;
/**
* 记录一些不太常见的案例
*
* @author Jiaju Zhuang
*/
@Ignore
@Slf4j
public class WriteTest {
/**
* 压缩临时文件
* 在导出Excel且格式为xlsx的时候会生成一个临时的xml文件会比较大再磁盘不太够的情况下可以压缩
* 当然压缩式耗费性能的
*/
@Test
public void compressedTemporaryFile() {
log.info("临时的xml存储在:{}", FileUtils.getPoiFilesPath());
File file = TestFileUtil.createNewFile("rare/compressedTemporaryFile" + System.currentTimeMillis()
+ ".xlsx");
// 这里 需要指定写用哪个class去写
try (ExcelWriter excelWriter = EasyExcel.write(file, DemoData.class).registerWriteHandler(
new WorkbookWriteHandler() {
/**
* 拦截Workbook创建完成事件
* @param context
*/
@Override
public void afterWorkbookCreate(WorkbookWriteHandlerContext context) {
// 获取到Workbook对象
Workbook workbook = context.getWriteWorkbookHolder().getWorkbook();
// 只有SXSSFWorkbook模式才会生成临时文件
if (workbook instanceof SXSSFWorkbook) {
SXSSFWorkbook sxssfWorkbook = (SXSSFWorkbook)workbook;
// 设置临时文件压缩,当然这个会浪费cpu性能 但是临时文件会变小
sxssfWorkbook.setCompressTempFiles(true);
}
}
}).build()) {
// 这里注意 如果同一个sheet只要创建一次
WriteSheet writeSheet = EasyExcel.writerSheet("模板").build();
// 10万数据 确保有足够的空间
for (int i = 0; i < 10000; i++) {
// 分页去数据库查询数据 这里可以去数据库查询每一页的数据
List<DemoData> data = data();
excelWriter.write(data, writeSheet);
}
log.info("写入完毕,开始准备迁移压缩文件。");
}
}
/**
* 在指定单元格写入数据
*/
@Test
public void specifiedCellWrite() {
File file = TestFileUtil.createNewFile("rare/specifiedCellWrite" + System.currentTimeMillis()
+ ".xlsx");
// 需要区分是在 最后一行之前 还是之后
// 区分的原因是:excel只能一直向前,而且内存里面只存储100条,而afterRowDispose是在每一行写入完成的时候调用,所以修改一行需要拦截这个事件
// 如果是在最后一行之后,由于后面不会再有数据了,所以只要拦截afterWorkbookDispose,在整个excel快写完的时候调用,继续写入数据即可
EasyExcel.write(file, DemoData.class)
// 写入的值在最后一行之前
.registerWriteHandler(new RowWriteHandler() {
@Override
public void afterRowDispose(RowWriteHandlerContext context) {
if (context.getRow().getRowNum() == 2) {
Cell cell = context.getRow().getCell(2);
if (cell == null) {
cell = context.getRow().createCell(2);
}
cell.setCellValue("测试的第二行数据呀");
}
}
})
// 写入的值 在最后一一行之后
.registerWriteHandler(new WorkbookWriteHandler() {
@Override
public void afterWorkbookDispose(WorkbookWriteHandlerContext context) {
Workbook workbook = context.getWriteWorkbookHolder().getWorkbook();
Sheet sheet = workbook.getSheetAt(0);
Row row = sheet.getRow(99);
if (row == null) {
row = sheet.createRow(99);
}
Cell cell = row.getCell(2);
if (cell == null) {
cell = row.createCell(2);
}
cell.setCellValue("测试地99行数据呀");
}
})
.sheet("模板")
.doWrite(data());
log.info("写入到文件完成:{}", file);
}
private List<DemoData> data() {
List<DemoData> list = ListUtils.newArrayList();
for (int i = 0; i < 10; i++) {
DemoData data = new DemoData();
data.setString("字符串" + i);
data.setDate(new Date());
data.setDoubleData(0.56);
list.add(data);
}
return list;
}
}

1
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/write/DemoData.java

@ -24,6 +24,7 @@ public class DemoData {
private Date date;
@ExcelProperty("数字标题")
private Double doubleData;
/**
* 忽略这个字段
*/

35
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/DemoData2.java

@ -0,0 +1,35 @@
package com.alibaba.easyexcel.test.temp;
import java.math.BigDecimal;
import java.util.Date;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
/**
* 基础数据类
*
* @author Jiaju Zhuang
**/
@Getter
@Setter
@EqualsAndHashCode
public class DemoData2 {
@ExcelProperty("字符串标题")
private String string;
@ExcelProperty("日期标题")
private Date date;
@ExcelProperty("数字标题")
private Double doubleData;
@ExcelProperty("数字标题2")
private BigDecimal bigDecimal;
/**
* 忽略这个字段
*/
@ExcelIgnore
private String ignore;
}

24
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/DemoData3.java

@ -0,0 +1,24 @@
package com.alibaba.easyexcel.test.temp;
import java.time.LocalDateTime;
import java.util.Date;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
/**
* 基础数据类
*
* @author Jiaju Zhuang
**/
@Getter
@Setter
@EqualsAndHashCode
public class DemoData3 {
@ExcelProperty("日期时间标题")
private LocalDateTime localDateTime;
}

316
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java

@ -1,32 +1,49 @@
package com.alibaba.easyexcel.test.temp;
import java.io.File;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.alibaba.easyexcel.test.demo.write.DemoData;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.util.NumberDataFormatterUtils;
import com.alibaba.excel.util.NumberUtils;
import com.alibaba.excel.util.PositionUtils;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.util.CellReference;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
/**
* 临时测试
*
* @author Jiaju Zhuang
**/
@Slf4j
public class Lock2Test {
private static final Logger LOGGER = LoggerFactory.getLogger(Lock2Test.class);
@ -37,7 +54,11 @@ public class Lock2Test {
// File file = TestFileUtil.readUserHomeFile("test/test6.xls");
File file = new File("/Users/zhuangjiaju/IdeaProjects/easyexcel/src/test/resources/converter/converter07.xlsx");
List<Object> list = EasyExcel.read("/Users/zhuangjiaju/Downloads/测试格式.xlsx").sheet(0).headRowNumber(0).doReadSync();
List<Object> list = EasyExcel.read(
"/Users/zhuangjiaju/IdeaProjects/easyexcel/easyexcel-test/target/test-classes/simpleWrite1674051907397.xlsx")
//.useDefaultListener(false)
.sheet(0)
.headRowNumber(0).doReadSync();
LOGGER.info("数据:{}", list.size());
for (Object data : list) {
LOGGER.info("返回数据:{}", CollectionUtils.size(data));
@ -109,18 +130,6 @@ public class Lock2Test {
return list;
}
private List<DemoData> data() {
List<DemoData> list = new ArrayList<DemoData>();
for (int i = 0; i < 10; i++) {
DemoData data = new DemoData();
data.setString("字符串" + i);
data.setDate(new Date());
data.setDoubleData(0.56);
list.add(data);
}
return list;
}
@Test
public void testc() throws Exception {
LOGGER.info("reslut:{}", JSON.toJSONString(new CellReference("B3")));
@ -151,10 +160,9 @@ public class Lock2Test {
@Test
public void test335() throws Exception {
LOGGER.info("reslut:{}", PositionUtils.getCol("A10",null));
LOGGER.info("reslut:{}", PositionUtils.getCol("A10", null));
LOGGER.info("reslut:{}", PositionUtils.getRow("A10"));
LOGGER.info("reslut:{}", PositionUtils.getCol("AB10",null));
LOGGER.info("reslut:{}", PositionUtils.getCol("AB10", null));
LOGGER.info("reslut:{}", PositionUtils.getRow("AB10"));
//LOGGER.info("reslut:{}", PositionUtils2.getCol("A10",null));
@ -163,5 +171,279 @@ public class Lock2Test {
//LOGGER.info("reslut:{}", PositionUtils2.getRow("AB10"));
}
@Test
public void numberforamt() throws Exception {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
//LOGGER.info("date:{}",
// NumberDataFormatterUtils.format(BigDecimal.valueOf(44727.99998842592), (short)200, "yyyy-MM-dd HH:mm:ss",
// null,
// null, null));
//
//LOGGER.info("date:{}",
// NumberDataFormatterUtils.format(BigDecimal.valueOf(44728.99998842592), (short)200, "yyyy-MM-dd HH:mm:ss",
// null,
// null, null));
//
//LOGGER.info("date:{}",
// NumberDataFormatterUtils.format(BigDecimal.valueOf(44729.99998836806), (short)200, "yyyy-MM-dd HH:mm:ss",
// null,
// null, null));
//
//LOGGER.info("date:{}",
// NumberDataFormatterUtils.format(BigDecimal.valueOf(44727.99998842592).setScale(10, RoundingMode
// .HALF_UP), (short)200, "yyyy-MM-dd HH:mm:ss",
// null,
// null, null));
//
//LOGGER.info("date:{}",
// NumberDataFormatterUtils.format(BigDecimal.valueOf(44728.99998842592).setScale(10, RoundingMode
// .HALF_UP), (short)200, "yyyy-MM-dd HH:mm:ss",
// null,
// null, null));
//44729.9999883681
//44729.999988368058
//LOGGER.info("date:{}",
// NumberDataFormatterUtils.format(BigDecimal.valueOf(44729.999988368058).setScale(10, RoundingMode
// .HALF_UP), (short)200, "yyyy-MM-dd HH:mm:ss",
// null,
// null, null));
//LOGGER.info("date:{}",BigDecimal.valueOf(44729.999988368058).setScale(10, RoundingMode.HALF_UP).doubleValue
// ());
// 2022/6/17 23:59:59
// 期望 44729.99998842592
//LOGGER.info("data:{}", DateUtil.getJavaDate(44729.9999883681, true));
LOGGER.info("data4:{}", DateUtil.getJavaDate(BigDecimal.valueOf(44729.999988368058)
.setScale(4, RoundingMode.HALF_UP).doubleValue(), false));
LOGGER.info("data5:{}", DateUtil.getJavaDate(BigDecimal.valueOf(44729.999988368058)
.setScale(5, RoundingMode.HALF_UP).doubleValue(), false));
LOGGER.info("data6:{}", DateUtil.getJavaDate(BigDecimal.valueOf(44729.999988368058)
.setScale(6, RoundingMode.HALF_UP).doubleValue(), false));
LOGGER.info("data7:{}", DateUtil.getJavaDate(BigDecimal.valueOf(44729.999988368058)
.setScale(7, RoundingMode.HALF_UP).doubleValue(), false));
LOGGER.info("data8:{}", DateUtil.getJavaDate(BigDecimal.valueOf(44729.999988368058)
.setScale(8, RoundingMode.HALF_UP).doubleValue(), false));
LOGGER.info("data:{}", format.format(DateUtil.getJavaDate(44729.999988368058, false)));
LOGGER.info("data:{}", format.format(DateUtil.getJavaDate(44729.9999883681, false)));
LOGGER.info("data:{}", DateUtil.getJavaDate(Double.parseDouble("44729.999988368058"), false));
LOGGER.info("data:{}", DateUtil.getJavaDate(Double.parseDouble("44729.9999883681"), false));
// 44729.999976851854
// 44729.999988368058
LOGGER.info("data:{}", DateUtil.getExcelDate(format.parse("2022-06-17 23:59:58")));
// 44729.99998842592
LOGGER.info("data:{}", DateUtil.getExcelDate(format.parse("2022-06-17 23:59:59")));
LOGGER.info("data:{}", DateUtil.getJavaDate(BigDecimal.valueOf(44729.999976851854)
.setScale(10, RoundingMode.HALF_UP).doubleValue(), false));
LOGGER.info("data:{}", DateUtil.getJavaDate(BigDecimal.valueOf(44729.99998842592)
.setScale(10, RoundingMode.HALF_UP).doubleValue(), false));
LOGGER.info("data:{}", DateUtil.getJavaDate(BigDecimal.valueOf(44729.999976851854)
.setScale(5, RoundingMode.HALF_UP).doubleValue(), false));
LOGGER.info("data:{}", DateUtil.getJavaDate(BigDecimal.valueOf(44729.99998842592)
.setScale(5, RoundingMode.HALF_UP).doubleValue(), false));
}
@Test
public void testDate() throws Exception {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
log.info("TT:{}", format.format(new Date(100L)));
log.info("TT:{}", new Date().getTime());
}
@Test
public void testDateAll() throws Exception {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
long dateTime = 0L;
while (true) {
Date date = new Date(dateTime);
double excelDate = DateUtil.getExcelDate(date);
Assert.assertEquals("测试基本转换错误" + dateTime, format.format(date),
format.format(DateUtil.getJavaDate(excelDate, false)));
Assert.assertEquals("测试精度5转换错误" + dateTime, format.format(date),
format.format(DateUtil.getJavaDate(BigDecimal.valueOf(excelDate)
.setScale(10, RoundingMode.HALF_UP).doubleValue(), false)));
LOGGER.info("date:{}", format2.format(DateUtil.getJavaDate(BigDecimal.valueOf(excelDate)
.setScale(10, RoundingMode.HALF_UP).doubleValue())));
dateTime += 1000L;
// 30天输出
if (dateTime % (24 * 60 * 60 * 1000) == 0) {
log.info("{}成功", format.format(date));
}
if (dateTime > 1673957544750L) {
log.info("结束啦");
break;
}
}
log.info("结束啦");
}
@Test
public void numberforamt3() throws Exception {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
List<Map<Integer, ReadCellData>> list = EasyExcel.read("/Users/zhuangjiaju/Downloads/date3.xlsx")
.useDefaultListener(false)
.sheet(0)
.headRowNumber(0).doReadSync();
LOGGER.info("数据:{}", list.size());
for (Map<Integer, ReadCellData> readCellDataMap : list) {
ReadCellData data = readCellDataMap.get(0);
LOGGER.info("data:{}", format.format(
DateUtil.getJavaDate(data.getNumberValue().setScale(10, RoundingMode.HALF_UP).doubleValue(), false)));
}
//
//LOGGER.info("data:{}", format.format(DateUtil.getJavaDate(44727.999988425923, false)));
//LOGGER.info("data:{}", format.format(DateUtil.getJavaDate(44729.999988368058, false)));
}
@Test
public void numberforamt4() throws Exception {
String fileName = TestFileUtil.getPath() + "simpleWrite" + System.currentTimeMillis() + ".xlsx";
// 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
// 如果这里想使用03 则 传入excelType参数即可
EasyExcel.write(fileName, DemoData.class)
.sheet("模板")
.doWrite(() -> {
// 分页查询数据
return data2();
});
}
@Test
public void numberforamt77() throws Exception {
String fileName = TestFileUtil.getPath() + "simpleWrite" + System.currentTimeMillis() + ".xlsx";
// 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
// 如果这里想使用03 则 传入excelType参数即可
EasyExcel.write(fileName, DemoData3.class)
.sheet("模板")
.doWrite(() -> {
List<DemoData3> list = new ArrayList<>();
DemoData3 demoData3 = new DemoData3();
demoData3.setLocalDateTime(LocalDateTime.of(2023, 1, 1, 0, 0, 0, 400000000));
list.add(demoData3);
demoData3 = new DemoData3();
demoData3.setLocalDateTime(LocalDateTime.of(2023, 1, 1, 0, 0, 0, 499000000));
list.add(demoData3);
demoData3 = new DemoData3();
demoData3.setLocalDateTime(LocalDateTime.of(2023, 1, 1, 0, 0, 0, 500000000));
list.add(demoData3);
demoData3 = new DemoData3();
demoData3.setLocalDateTime(LocalDateTime.of(2023, 1, 1, 0, 0, 0, 501000000));
list.add(demoData3);
demoData3 = new DemoData3();
demoData3.setLocalDateTime(LocalDateTime.of(2023, 1, 1, 0, 0, 0, 995000000));
list.add(demoData3);
return list;
});
}
@Test
public void numberforamt99() throws Exception {
LocalDateTime localDateTime=LocalDateTime.of(2023, 1, 1, 0, 0, 0, 995000000);
log.info("date:{}",localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")));
}
@Test
public void numberforamt5() throws Exception {
String fileName = TestFileUtil.getPath() + "simpleWrite" + System.currentTimeMillis() + ".xlsx";
// 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
// 如果这里想使用03 则 传入excelType参数即可
EasyExcel.write(fileName, DemoData.class)
.sheet("模板")
.doWrite(() -> {
// 分页查询数据
return data3();
});
}
@Test
public void numberforamt6() throws Exception {
DecimalFormat decimalFormat = new DecimalFormat("#.#");
BigDecimal bigDecimal = new BigDecimal(3101011021236149800L);
log.info("b:{}", bigDecimal);
log.info("b:{}", bigDecimal.setScale(-4, RoundingMode.HALF_UP));
log.info("b:{}", decimalFormat.format(bigDecimal.setScale(-4, RoundingMode.HALF_UP)));
}
@Test
public void numberforamt7() throws Exception {
DecimalFormat decimalFormat = new DecimalFormat("#.#");
BigDecimal bigDecimal = new BigDecimal(3.1010110212361498E+18).round(new MathContext(15, RoundingMode.HALF_UP));
//bigDecimal.
// bigDecimal
log.info("b:{}", bigDecimal);
log.info("b:{}", bigDecimal.setScale(-4, RoundingMode.HALF_UP));
log.info("b:{}", decimalFormat.format(bigDecimal.setScale(-4, RoundingMode.HALF_UP)));
log.info("b:{}", decimalFormat.format(bigDecimal));
}
private List<DemoData2> data3() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
List<DemoData2> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
DemoData2 data = new DemoData2();
data.setString("字符串" + i);
data.setDoubleData(0.56);
data.setBigDecimal(BigDecimal.valueOf(3101011021236149800L));
list.add(data);
}
return list;
}
private List<DemoData> data() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
List<DemoData> list = new ArrayList<DemoData>();
for (int i = 0; i < 10; i++) {
DemoData data = new DemoData();
data.setString("字符串" + i);
try {
data.setDate(format.parse("2032-01-18 09:00:01.995"));
} catch (ParseException e) {
throw new RuntimeException(e);
}
data.setDoubleData(0.56);
list.add(data);
}
return list;
}
private List<DemoData> data2() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
List<DemoData> list = new ArrayList<DemoData>();
for (int i = 0; i < 10; i++) {
DemoData data = new DemoData();
data.setString("字符串" + i);
try {
data.setDate(format.parse("2032-01-18 09:00:00."));
} catch (ParseException e) {
throw new RuntimeException(e);
}
data.setDoubleData(0.56);
list.add(data);
}
return list;
}
}

BIN
easyexcel-test/src/test/resources/compatibility/t04.xlsx

Binary file not shown.

BIN
easyexcel-test/src/test/resources/compatibility/t05.xlsx

Binary file not shown.

BIN
easyexcel-test/src/test/resources/compatibility/t06.xlsx

Binary file not shown.

BIN
easyexcel-test/src/test/resources/compatibility/t07.xlsx

Binary file not shown.

2
pom.xml

@ -20,7 +20,7 @@
<properties>
<revision>3.1.4</revision>
<revision>3.2.0</revision>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jdk.version>1.8</jdk.version>
<gpg.skip>true</gpg.skip>

10
update.md

@ -1,3 +1,13 @@
# 3.2.0
* 修复部分xlsx读取日期可能相差1秒的bug [Issue #1956](https://github.com/alibaba/easyexcel/issues/1956)
* 修复部分数据精度和excel不匹配的bug [Issue #2805](https://github.com/alibaba/easyexcel/issues/2805)
* 不创建对象的读支持读取原始的数据类型
# 3.1.5
* 提高xlsx读取兼容性:兼用ns2开头的标签
# 3.1.4
* 提高xlsx读取兼容性:在存在第一行很多空列的情况下,忽略空列

Loading…
Cancel
Save