Excel導入導出工具
項目地址:https://gitee.com/sanri/sanri-excel-poi
優點:簡單的配置即可實現導出精美的 Excel 表格,可以不用配置來導入一列數據,導入 map 數據,簡單配置可以導入實體數據。
解決了一些常見問題,比如
- 導入的時候空行問題
- 導入數據去前后空格
- 繼承類也可以導出數據
- 完美的支持日期格式的導入導出
- 數字為浮點型的精度處理
- 完美解決導出時的中文列寬問題
- 可自定義列的順序
- 支持 Excel 公式
發現BUG可以提Issue,可以給我發郵件,可以加我QQ,可以進9420技術群討論.
作者QQ: 2441719087
作者郵箱: ningxiangsanri@163.com
9420 技術交流群: 645576465
作者微信:sanri1993-
如何使用
以后會上傳中央倉庫,引用 maven 地址為
<dependency>
<groupId>com.sanri.excel</groupId>
<artifactId>sanri-excel-poi</artifactId>
<version>1.0-RELEASE</version>
</dependency>
目前需要自己下載代碼來構建,或下載已經構建好的 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>
定義實體類
@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;
}
獲取數據並導出
實測在 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"));
導入一列數據
FileInputStream fileInputStream = FileUtils.openInputStream(new File("D:\\test/1567833685699.xlsx"));
// 0 代表導入第 0 列數據,1 表示從第 1 行開始
List<String> strings = ExcelImportUtil.importListData(fileInputStream, String.class, 0, 1);
導入鍵值對數據
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);
導入實體對象數據
FileInputStream fileInputStream = FileUtils.openInputStream(new File("D:\\test/1567833427823.xlsx"));
List<Simple> simples = ExcelImportUtil.importData(fileInputStream, Simple.class);
配置你的導入導出
注解 @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;
注解 @ExcelImport 相關配置
// 數據起始行,一般設置 1 就行,從 0 開始
int startRow();
// 支持導入的版本,默認都支持
ExcelVersion[] support() default {ExcelVersion.EXCEL2003,ExcelVersion.EXCEL2007};
列注解 @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;