___________ .__ _____ ____.
\_ _____/__ ___ ____ ____ | | / | | | |
| __)_\ \/ // ___\/ __ \| | / | |_ | |
| \> <\ \__\ ___/| |__/ ^ /\__| |
/_______ /__/\_ \\___ >___ >____/\____ |\________|
\/ \/ \/ \/ |__|
(version: 2.1.4-Alpha)
- Excel读取支持部分类型转换了(如转为Integer,Long,Date(部分)等) v2.0.0之前只能全部内容转为String
- Excel支持非注解读取Excel内容了,内容存于
List<List<String>>
对象内 - 现在支持
List<List<String>>
导出Excel了(可以不基于模板) - Excel新增了Map数据样式映射功能(模板可为每个key设置一个样式,定义为:&key, 导出Map数据的样式将与key值映射)
- 新增读取Excel数据转换器接口
com.github.converter.ReadConvertible
- 新增写入Excel数据转换器接口
com.github.converter.WriteConvertible
- 支持多sheet一键导出,多sheet导出封装Wrapper详见
com.github.sheet.wrapper
包内包装类 - 修复已知bug及代码与注释优化
@ExcelField(title = "学号", order = 1)
private Long id;
@ExcelField(title = "姓名", order = 2)
private String name;
// 写入数据转换器 Student2DateConverter
@ExcelField(title = "入学日期", order = 3, writeConverter = Student2DateConverter.class)
private Date date;
@ExcelField(title = "班级", order = 4)
private Integer classes;
// 读取数据转换器 Student2ExpelConverter
@ExcelField(title = "是否开除", order = 5, readConverter = Student2ExpelConverter.class)
private boolean expel;
/**
* excel是否开除 列数据转换器
*/
public class Student2ExpelConverter implements ReadConvertible{
@Override
public Object execRead(String object) {
return object.equals("是");
}
}
@Test
public void excel2Object2() {
String path = "D:\\JProject\\Excel4J\\src\\test\\resources\\students_02.xlsx";
try {
// 1)
// 不基于注解,将Excel内容读至List<List<String>>对象内
List<List<String>> lists = ExcelUtils.getInstance().readExcel2List(path, 1, 2, 0);
System.out.println("读取Excel至String数组:");
for (List<String> list : lists) {
System.out.println(list);
}
// 2)
// 基于注解,将Excel内容读至List<Student2>对象内
// 验证读取转换函数Student2ExpelConverter
// 注解 `@ExcelField(title = "是否开除", order = 5, readConverter = Student2ExpelConverter.class)`
List<Student2> students = ExcelUtils.getInstance().readExcel2Objects(path, Student2.class, 0, 0);
System.out.println("读取Excel至对象数组(支持类型转换):");
for (Student2 st : students) {
System.out.println(st);
}
} catch (Exception e) {
e.printStackTrace();
}
}
读取Excel至String数组:
[10000000000001, 张三, 2016/01/19, 101, 是]
[10000000000002, 李四, 2017-11-17 10:19:10, 201, 否]
读取Excel至对象数组(支持类型转换):
Student2{id=10000000000001, name='张三', date=Tue Jan 19 00:00:00 CST 2016, classes=101, expel='true'}
Student2{id=10000000000002, name='李四', date=Fri Nov 17 10:19:10 CST 2017, classes=201, expel='false'}
Student2{id=10000000000004, name='王二', date=Fri Nov 17 00:00:00 CST 2017, classes=301, expel='false'}
@Test
public void testList2Excel() throws Exception {
List<List<String>> list2 = new ArrayList<>();
List<String> header = new ArrayList<>();
for (int i = 0; i < 10; i++) {
List<String> _list = new ArrayList<>();
for (int j = 0; j < 10; j++) {
_list.add(i + " -- " + j);
}
list2.add(_list);
header.add(i + "---");
}
ExcelUtils.getInstance().exportObjects2Excel(list2, header, "D:/D.xlsx");
}
/**
* 导出excel日期数据转换器
*/
public class Student2DateConverter implements WriteConvertible {
@Override
public Object execWrite(Object object) {
Date date = (Date) object;
return DateUtils.date2Str(date, DateUtils.DATE_FORMAT_MSEC_T_Z);
}
}
// 验证日期转换函数 Student2DateConverter
// 注解 `@ExcelField(title = "入学日期", order = 3, writeConverter = Student2DateConverter.class)`
@Test
public void testWriteConverter() throws Exception {
List<Student2> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
list.add(new Student2(10000L + i, "学生" + i, new Date(), 201, false));
}
ExcelUtils.getInstance().exportObjects2Excel(list, Student2.class, true, "sheet0", true, "D:/D.xlsx");
}
@Test
public void testObject2Excel() throws Exception {
String tempPath = "/normal_template.xlsx";
List<Student1> list = new ArrayList<>();
list.add(new Student1("1010001", "盖伦", "六年级三班"));
list.add(new Student1("1010002", "古尔丹", "一年级三班"));
list.add(new Student1("1010003", "蒙多(被开除了)", "六年级一班"));
list.add(new Student1("1010004", "萝卜特", "三年级二班"));
list.add(new Student1("1010005", "奥拉基", "三年级二班"));
list.add(new Student1("1010006", "得嘞", "四年级二班"));
list.add(new Student1("1010007", "瓜娃子", "五年级一班"));
list.add(new Student1("1010008", "战三", "二年级一班"));
list.add(new Student1("1010009", "李四", "一年级一班"));
Map<String, String> data = new HashMap<>();
data.put("title", "战争学院花名册");
data.put("info", "学校统一花名册");
// 基于模板导出Excel
ExcelUtils.getInstance().exportObjects2Excel(tempPath, 0, list, data, Student1.class, false, "D:/A.xlsx");
// 不基于模板导出Excel
ExcelUtils.getInstance().exportObjects2Excel(list, Student1.class, true, null, true, "D:/B.xlsx");
}
@Test
public void testMap2Excel() throws Exception {
Map<String, List> classes = new HashMap<>();
Map<String, String> data = new HashMap<>();
data.put("title", "战争学院花名册");
data.put("info", "学校统一花名册");
classes.put("class_one", new ArrayList<Student1>() {{
add(new Student1("1010009", "李四", "一年级一班"));
add(new Student1("1010002", "古尔丹", "一年级三班"));
}});
classes.put("class_two", new ArrayList<Student1>() {{
add(new Student1("1010008", "战三", "二年级一班"));
}});
classes.put("class_three", new ArrayList<Student1>() {{
add(new Student1("1010004", "萝卜特", "三年级二班"));
add(new Student1("1010005", "奥拉基", "三年级二班"));
}});
classes.put("class_four", new ArrayList<Student1>() {{
add(new Student1("1010006", "得嘞", "四年级二班"));
}});
classes.put("class_six", new ArrayList<Student1>() {{
add(new Student1("1010001", "盖伦", "六年级三班"));
add(new Student1("1010003", "蒙多", "六年级一班"));
}});
ExcelUtils.getInstance().exportObject2Excel("/map_template.xlsx",
0, classes, data, Student1.class, false, "D:/C.xlsx");
}
定义符 | 描述 | 优先级(大到小) |
---|---|---|
$appoint_line_style | 当前行样式 | 3 |
$single_line_style | 单行样式 | 2 |
$double_line_style | 双行样式 | 2 |
$default_style | 默认样式 | 1 |
$data_index | 数据插入的起始位置 | - |
$serial_number | 插入序号标记 | - |
多sheet数据导出只需将待导出数据封装入com.github.sheet.wrapper
包内的Wrapper类即可实现多sheet一键导出
// 多sheet无模板、无注解导出
@Test
public void testBatchSimple2Excel() throws Exception {
// 生成sheet数据
List<SimpleSheetWrapper> list = new ArrayList<>();
for (int i = 0; i <= 2; i++) {
//表格内容数据
List<String[]> data = new ArrayList<>();
for (int j = 0; j < 1000; j++) {
// 行数据(此处是数组) 也可以是List数据
String[] rows = new String[5];
for (int r = 0; r < 5; r++) {
rows[r] = "sheet_" + i + "row_" + j + "column_" + r;
}
data.add(rows);
}
// 表头数据
List<String> header = new ArrayList<>();
for (int h = 0; h < 5; h++) {
header.add("column_" + h);
}
list.add(new SimpleSheetWrapper(data, header, "sheet_" + i));
}
ExcelUtils.getInstance().simpleSheet2Excel(list, "K.xlsx");
}
// 多sheet无模板、基于注解的导出
@Test
public void testBatchNoTemplate2Excel() throws Exception {
List<NoTemplateSheetWrapper> sheets = new ArrayList<>();
for (int s = 0; s < 3; s++) {
List<Student2> list = new ArrayList<>();
for (int i = 0; i < 1000; i++) {
list.add(new Student2(10000L + i, "学生" + i, new Date(), 201, false));
}
sheets.add(new NoTemplateSheetWrapper(list, Student2.class, true, "sheet_" + s));
}
ExcelUtils.getInstance().noTemplateSheet2Excel(sheets, "EE.xlsx");
}
// 基于模板、注解的多sheet导出
@Test
public void testObject2BatchSheet() throws Exception {
List<NormalSheetWrapper> sheets = new ArrayList<>();
for (int i = 0; i < 2; i++) {
List<Student1> list = new ArrayList<>();
list.add(new Student1("1010001", "盖伦", "六年级三班"));
list.add(new Student1("1010002", "古尔丹", "一年级三班"));
list.add(new Student1("1010003", "蒙多(被开除了)", "六年级一班"));
list.add(new Student1("1010004", "萝卜特", "三年级二班"));
list.add(new Student1("1010005", "奥拉基", "三年级二班"));
list.add(new Student1("1010006", "得嘞", "四年级二班"));
list.add(new Student1("1010007", "瓜娃子", "五年级一班"));
list.add(new Student1("1010008", "战三", "二年级一班"));
list.add(new Student1("1010009", "李四", "一年级一班"));
Map<String, String> data = new HashMap<>();
data.put("title", "战争学院花名册");
data.put("info", "学校统一花名册");
sheets.add(new NormalSheetWrapper(i, list, data, Student1.class, false));
}
String tempPath = "/normal_batch_sheet_template.xlsx";
// 基于模板导出Excel
ExcelUtils.getInstance().normalSheet2Excel(sheets, tempPath, "AA.xlsx");
}
5. 形如Map<String, Collection<Object.toString>>
数据基于模板、注解的多sheet导出com.github.sheet.wrapper.MapSheetWrapper
// Map数据的多sheet导出
@Test
public void testMap2BatchSheet() throws Exception {
List<MapSheetWrapper> sheets = new ArrayList<>();
for (int i = 0; i < 2; i++) {
Map<String, List<?>> classes = new HashMap<>();
Map<String, String> data = new HashMap<>();
data.put("title", "战争学院花名册");
data.put("info", "学校统一花名册");
classes.put("class_one", Arrays.asList(
new Student1("1010009", "李四", "一年级一班"),
new Student1("1010002", "古尔丹", "一年级三班")
));
classes.put("class_two", Collections.singletonList(
new Student1("1010008", "战三", "二年级一班")
));
classes.put("class_three", Arrays.asList(
new Student1("1010004", "萝卜特", "三年级二班"),
new Student1("1010005", "奥拉基", "三年级二班")
));
classes.put("class_four", Collections.singletonList(
new Student1("1010006", "得嘞", "四年级二班")
));
classes.put("class_six", Arrays.asList(
new Student1("1010001", "盖伦", "六年级三班"),
new Student1("1010003", "蒙多", "六年级一班")
));
sheets.add(new MapSheetWrapper(i, classes, data, Student1.class, false));
}
ExcelUtils.getInstance().mapSheet2Excel(sheets, "/map_batch_sheet_template.xlsx", "CC.xlsx");
}
>> git clone https://github.com/Crab2died/Excel4J.git Excel4J
>> package.cmd
<dependency>
<groupId>com.github.crab2died</groupId>
<artifactId>Excel4J</artifactId>
<version>2.1.4-Alpha</version>
</dependency>