EasyExcel新增三列数据
时间: 2023-11-22 12:47:39 浏览: 83
为了新增三列数据,我们需要在ExcelTest类中进行如下操作:
1. 在ExcelWriterSheetBuilder中使用head方法添加表头信息。
2. 在ExcelWriterSheetBuilder中使用registerWriteHandler方法添加需要写入的数据。
3. 在ExcelWriterSheetBuilder中使用registerWriteHandler方法添加需要写入的数据。
4. 在ExcelWriterSheetBuilder中使用registerWriteHandler方法添加需要写入的数据。
下面是示例代码:
```java
public class ExcelTest {
@Test
public void test() {
// 临时文件路径
String writerFilePath = "D:/temp/device_" + IdUtil.fastUUID() + ".xls";
try (FileInputStream in = new FileInputStream("D:/temp/template.xls")) {
// 生成错误信息的文件
ExcelWriterBuilder writerBuilder = EasyExcel.write(writerFilePath)
.withTemplate(in)
.excelType(ExcelTypeEnum.XLS)
.needHead(true);
ExcelWriterSheetBuilder writerSheetBuilder = EasyExcel.writerSheet("Sheet1");
writerSheetBuilder.head(head());
writerBuilder.registerWriteHandler(new CommentWriteHandler(1, "IMEI已被分配"));
writerBuilder.registerWriteHandler(new ColumnWidthStyleStrategy());
writerBuilder.registerWriteHandler(new FillStyleStrategy());
writerBuilder.registerWriteHandler(new FontStyleStrategy());
ExcelWriter excelWriter = writerBuilder.build();
excelWriter.write(new ArrayList<>(), writerSheetBuilder.build());
excelWriter.finish();
} catch (IOException e) {
e.printStackTrace();
}
}
private List<List<String>> head() {
List<List<String>> head = new ArrayList<>();
List<String> head0 = new ArrayList<>();
head0.add("列1");
List<String> head1 = new ArrayList<>();
head1.add("列2");
List<String> head2 = new ArrayList<>();
head2.add("列3");
List<String> head3 = new ArrayList<>();
head3.add("列4");
head.add(head0); head.add(head1);
head.add(head2);
head.add(head3);
return head;
}
}
```
阅读全文