Excel导入导出工具(简单、好用且轻量级的海量Excel文件导入导出解决方案.)

Excel导入导出工具(简单、好用且轻量级的海量Excel文件导入导出解决方案.)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/sanri1993/article/details/100601578

Excel导入导出工具

项目地址:https://gitee.com/sanri/sanri-excel-poi
优点:简单的配置即可实现导出精美的 Excel 表格,可以不用配置来导入一列数据,导入 map 数据,简单配置可以导入实体数据。
解决了一些常见问题,比如

  1. 导入的时候空行问题
  2. 导入数据去前后空格
  3. 继承类也可以导出数据
  4. 完美的支持日期格式的导入导出
  5. 数字为浮点型的精度处理
  6. 完美解决导出时的中文列宽问题
  7. 可自定义列的顺序
  8. 支持 Excel 公式

发现BUG可以提Issue,可以给我发邮件,可以加我QQ,可以进9420技术群讨论.

作者QQ: 2441719087

作者邮箱: [email protected]

9420 技术交流群: 645576465

作者微信:sanri1993-
在这里插入图片描述

如何使用

以后会上传中央仓库,引用 maven 地址为

<dependency>
	<groupId>com.sanri.excel</groupId> <artifactId>sanri-excel-poi</artifactId> <version>1.0-RELEASE</version> </dependency>
 
  • 1
  • 2
  • 3
  • 4
  • 5

目前需要自己下载代码来构建,或下载已经构建好的 release 包 :

https://gitee.com/sanri/sanri-excel-poi/repository/archive/v1.0-RELEASE?format=zip

  • 还需要添加第三方依赖,如果项目中已经存在依赖,请忽略。(真实项目一般都是有依赖的)
<!-- Excel poi  -->
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.10-FINAL</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.10-FINAL</version> </dependency> <!--apache commons --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.8.1</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.6</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-csv</artifactId> <version>1.2</version> </dependency> <!-- slf4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.21</version> </dependency>
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

定义实体类

@ExcelExport
@ExcelImport(startRow = 1) @Data public class Simple { @ExcelColumn(value = "年龄",order = 2) private int age; @ExcelColumn(value = "级别",order = 1) private Integer level; @ExcelColumn(value = "姓名",order = 0,chineseWidth = true) private String name; @ExcelColumn(value = "生日",order = 3) private Date birthday; @ExcelColumn(value = "序号",order = 4,hidden = true) private long id; @ExcelColumn(value = "是否成功",order = 5) private boolean success; @ExcelColumn(value = "薪水",order = 6,precision = 2) private double money; @ExcelColumn(value = "奖金",order = 7,precision = 2) private float comm; }
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

获取数据并导出

实测在 i5 3 代 cpu ,8 G 内存,导出 10 万数据,用时 5 秒

// 从数据库,redis,消息中间件...... 获取的数据
List<Simple> simpleList= simpleBeanDatas(10); // 创建导出类 ExcelExportWriter excelExportWriter = new ExcelExportWriter(Simple.class); // 开始导出 excelExportWriter.export(simpleList); // 写到输出流 excelExportWriter.writeTo(new FileOutputStream("d:/test/"+System.currentTimeMillis()+".xlsx"));
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

导入一列数据

FileInputStream fileInputStream = FileUtils.openInputStream(new File("D:\\test/1567833685699.xlsx")); // 0 代表导入第 0 列数据,1 表示从第 1 行开始 List<String> strings = ExcelImportUtil.importListData(fileInputStream, String.class, 0, 1);
 
  • 1
  • 2
  • 3

导入键值对数据

FileInputStream fileInputStream = FileUtils.openInputStream(new File("D:\\test/1567833685699.xlsx")); // 0 表示 key 的列为第 0 列;8 表示第 8 列为值列; 1 表示从第 1 行开始 Map<String, String> stringStringMap = ExcelImportUtil.importMapData(fileInputStream, String.class, 0, 8, 1);
 
  • 1
  • 2
  • 3

导入实体对象数据

FileInputStream fileInputStream = FileUtils.openInputStream(new File("D:\\test/1567833427823.xlsx")); List<Simple> simples = ExcelImportUtil.importData(fileInputStream, Simple.class);
 
  • 1
  • 2

配置你的导入导出

注解 @ExcelExport 相关配置

// Excel 导出版本,可选值 ExcelVersion.EXCEL2007,ExcelVersion.EXCEL2003
ExcelVersion version() default ExcelVersion.EXCEL2007; // 当导出有设置标题时,设置标题行的高度 short titleRowHeight() default 40; // 头部行的高度,即列说明行的高度 short headRowHeight() default 30; // 每一行数据的高度 short bodyRowHeight() default 25; // 是否自动宽度,Excel 的自动宽度对中文支持不好,我这里对中文宽度做了适应 boolean autoWidth() default true; // 一个 sheet 页的最大数据量,设置 -1 表示不限制,2003 版本最大 60000 行;超过的行数会另起 sheet 页 int sheetMaxRow() default -1; // 快速导出模式,使用 new SXSSFWorkbook(rowAccessWindowSize) workbook 对象 boolean fastModel() default true; // 快速导出模式的一个设置,一般默认就行 int rowAccessWindowSize() default 1000;
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

注解 @ExcelImport 相关配置

// 数据起始行,一般设置 1 就行,从 0 开始
int startRow(); // 支持导入的版本,默认都支持 ExcelVersion[] support() default {ExcelVersion.EXCEL2003,ExcelVersion.EXCEL2007};
 
  • 1
  • 2
  • 3
  • 4
  • 5

列注解 @ExcelColumn 相关配置

// 导出的中文头部列的值,导入不用管
String value(); // 导入的顺序,默认从 0 开始,导出不用管 int order() default -1; // 宽度,Excel 宽度单元 int width() default -1; // 使用字符数来定义宽度,一个中文算一个字符 int charWidth() default -1; // 使用像素值来定义宽度 int pxWidth() default -1; // 在使用自动宽度的时候,标记当前列是中文列 boolean chineseWidth() default false; // 导出是否隐藏当前列 boolean hidden() default false; // 导入的时候对当前列做字符串前后空格过滤 boolean trim() default true; // 导出的时候的单元格类型,可选值有CELL_TYPE_NUMERIC,CELL_TYPE_STRING,CELL_TYPE_BLANK,CELL_TYPE_BOOLEAN CellType cellType() default CellType.CELL_TYPE_STRING; // 导入、导出日期格式 String pattern() default "yyyy-MM-dd"; // 导入、导出的数字精度设置,会对浮点型做处理 int precision() default -1;
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
文章最后发布于: 2019-09-07 17:34:27

猜你喜欢

转载自www.cnblogs.com/xichji/p/11704537.html
今日推荐