封装java导出Excel工具类使用(二)
背景
自己封装了一个excel工具类,方便以后使用和部分可能有相同需求的朋友使用,现在做一个简单的记录
快速引入
第一步导包,maven包仅包含poi依赖,导入时注意poi版本冲突
<dependency>
<groupId>top.rdongyue</groupId>
<artifactId>ExcelUtils</artifactId>
<version>1.0.8</version>
</dependency>
基本excel导入成对象和合并excel导入成为对象
请查看上一篇文章 基于POI封装的操作Excel工具类使用
导出基本excel表格
需求:
//定义实体类
@Data
public class StandardExcelModel1 {
@StandardExcelAttr(excelColumn = 0, title = "列表标题1")
@ExcelTitleStyleAnno(fontName = "黑体",bold = true)
String number1;
@StandardExcelAttr(excelColumn = 1, title = "列表标题2")
@ExcelTitleStyleAnno(fontName = "黑体",bold = true)
Integer number2;
@StandardExcelAttr(excelColumn = 2, title = "列表标题3")
@ExcelTitleStyleAnno(fontName = "黑体",bold = true)
String number3;
@StandardExcelAttr(excelColumn = 3, title = "列表标题4")
@ExcelTitleStyleAnno(fontName = "黑体",fontHeight = 16,fontColor = IndexedColors.RED ,bold = true)
String number4;
/**对应1对多的子列表*/
@StandardExcelAttr(excelColumn = 4,colspan = 2, useListTitle = true,forIn = true)
List<TestModelItem> number5;
@StandardExcelAttr(excelColumn = 7, title = "列表标题6")
@ExcelTitleStyleAnno(fontName = "黑体",bold = true)
LocalDateTime number6;
@StandardExcelAttr(excelColumn = 8, title = "列表标题7")
@ExcelTitleStyleAnno(fontName = "黑体",bold = true)
BigDecimal number7;
@StandardExcelAttr(excelColumn = 9, title = "列表标题8")
@ExcelTitleStyleAnno(fontName = "黑体",bold = true)
Long number8;
@StandardExcelAttr(excelColumn = 10, title = "列表标题9")
@ExcelTitleStyleAnno(fontName = "黑体",bold = true)
Character number9;
@StandardExcelAttr(excelColumn = 11, title = "列表标题10")
@ExcelTitleStyleAnno(fontName = "黑体",bold = true)
String number10;
}
@Data
public class TestModelItem {
/**第二级的子标题 依旧可以用ExcelTitleStyleAnno自定义标题样式*/
@StandardExcelAttr(excelColumn = 4, title = "子标题2")
String number2;
@StandardExcelAttr(excelColumn = 5,colspan = 1, useListTitle = true,forIn = true)
List<TestModelItem2> number3;
}
@Data
public class TestModelItem2 {
@StandardExcelAttr(excelColumn = 5, title = "子子标题2")
String number1;
@StandardExcelAttr(excelColumn = 6,title = "子子标题3")
List<String> number2;
}
public static void main(String[] args)
//组装测试数据
List<StandardExcelModel1> standardExcelModels = new ArrayList<>();
for (int i = 0; i <10; i++) {
StandardExcelModel1 testModel = new StandardExcelModel1();
testModel.setNumber1(i+"行");
testModel.setNumber2(i);
testModel.setNumber3(null);
testModel.setNumber4(i+"行");
ArrayList<TestModelItem> objects = new ArrayList<>();
for (int j = 0; j < 2; j++) {
TestModelItem testModelItem = new TestModelItem();
testModelItem.setNumber2("子节点"+j);
ArrayList<TestModelItem2> objects1 = new ArrayList<>();
for (int k = 0; k < 2; k++) {
TestModelItem2 testModelItem2 = new TestModelItem2();
testModelItem2.setNumber1("子子节点"+k);
ArrayList<String> objects2 = new ArrayList<>();
for (int l = 0; l < 2; l++) {
objects2.add("l"+l);
}
testModelItem2.setNumber2(objects2);
objects1.add(testModelItem2);
}
testModelItem.setNumber3(objects1);
objects.add(testModelItem);
}
testModel.setNumber5(objects);
testModel.setNumber6(LocalDateTime.now());
testModel.setNumber7(new BigDecimal(100.00));
testModel.setNumber8(121L);
testModel.setNumber9('K');
testModel.setNumber10(i+"行");
standardExcelModels.add(testModel);
}
//调用导出标准方法
ExportStandardExcelUtil.export(standardExcelModels, StandardExcelModel1.class,new FileOutputStream("d://导出标准表.xlsx"));
注:本次更新的列表1对多样式同样适用于导出异形表中的列表
最后
"如果您发现任何错误或有任何建议,请在评论中告诉我。我会尽力改进工具。"
"感谢您使用我的工具!如果您有任何问题或建议,请随时与我联系。"
"我也会持续改进的我工具。如果您有任何想法,请随时与我分享。"