This is a library that makes Excel easier to use in Java.
To-Excel depends on Apache POI, Model Mapper
<dependency>
<groupId>com.github.ckpoint</groupId>
<artifactId>toexcel</artifactId>
<version>1.0.8</version>
</dependency>
compile group: 'com.github.ckpoint', name: 'toexcel', version: '1.0.8'
public class UserModel {
@ExcelHeader(headerName = "name", headerNames = {"nick-name, nickName, email"})
private String name;
@ExcelHeader(headerName = "age")
private Integer age;
@ExcelHeader(headerName = "gender")
private String gender;
}
If there is no data field in Excel that matches the headerName, can search for the matching field in headerNames.
ToWorkBook toWorkBook = new ToWorkBook(new File("target/excel/map/read_test_1.xlsx"));
ToWorkSheet toWorkSheet = toWorkBook.getSheetAt(0);
List<UserModel> userModels = toWorkSheet.map(UserModel.class);
@Builder
public class UserModel {
@ExcelHeader(headerName = "name", priority = 0)
private String name;
@ExcelHeader(headerName = "age", priority = 1)
private Integer age;
@ExcelHeader(headerName = "gender", priority = 2)
private String gender;
}
List<UserModel> userModelList =
IntStream.range(0, 100).mapToObj(i ->
UserModel.builder().name("tester" + i).age(i).gender("man").build()).collect(Collectors.toList());
ToWorkBook workBook = new ToWorkBook(WorkBookType.XSSF);
ToWorkSheet sheet = workBook.createSheet();
sheet.from(userModelList);
workBook.writeFile("target/excel/map/write_test_1");
ToWorkBook workBook = new ToWorkBook(WorkBookType.XSSF);
ToWorkSheet sheet = workBook.createSheet().updateDirection(SheetDirection.HORIZON);
sheet.createTitleCell(2, "name", "age", "contactNumber");
sheet.merge(2, 1); // 2(width) X 1(height) [][]
sheet.createCellToNewline("sharky", "36", "010-1234-0000", "02-1111-1234");
sheet.createCellToNewline("melpis", "36", "010-1111-1234", "02-4221-1234");
sheet.createCellToNewline("heeseob", "32", "010-0000-1234", "-");
sheet.createCellToNewline("dongjun", "31", "010-4324-1234", "031-4121-1234");
workBook.writeFile("target/excel/manual/merge/merge_horizon_01");
ToWorkBook workBook = new ToWorkBook(WorkBookType.XSSF);
ToWorkSheet sheet = workBook.createSheet().updateDirection(SheetDirection.HORIZON);
sheet.createTitleCell(2, "name");
sheet.merge(1, 2);
sheet.createTitleCell(2, "age");
sheet.merge(1, 2);
sheet.createTitleCell(2, "contactNumber");
sheet.merge(2, 1);
sheet.newLine();
sheet.createTitleCell(2,"phone", "home");
sheet.createCellToNewline("sharky", "36", "010-1234-0000", "02-1111-1234");
sheet.createCellToNewline("melpis", "36", "010-1111-1234", "02-4221-1234");
sheet.createCellToNewline("heeseob", "32", "010-0000-1234", "-");
sheet.createCellToNewline("dongjun", "31", "010-4324-1234", "031-4121-1234");
workBook.writeFile("target/excel/manual/merge/merge_horizon_02");