refactor: Update the project's fill test demo. (#249)
This commit is contained in:
parent
48a8c2b362
commit
364e027f00
3
fastexcel-support/README_EN.md
Normal file
3
fastexcel-support/README_EN.md
Normal file
@ -0,0 +1,3 @@
|
||||
# easyexcel-support
|
||||
|
||||
External dependencies: Currently, there is only one dependency, cglib. Since cglib does not support higher versions of the JDK, a separate copy has been made.
|
@ -136,7 +136,7 @@ public class FillDataTest {
|
||||
|
||||
private void byNameFill(File file, File template) {
|
||||
FillData fillData = new FillData();
|
||||
fillData.setName("张三");
|
||||
fillData.setName("Zhang San");
|
||||
fillData.setNumber(5.2);
|
||||
EasyExcel.write(file, FillData.class).withTemplate(template).sheet("Sheet2").doFill(fillData);
|
||||
}
|
||||
@ -153,17 +153,17 @@ public class FillDataTest {
|
||||
excelWriter.fill(new FillWrapper("data3", data()), writeSheet);
|
||||
excelWriter.fill(new FillWrapper("data3", data()), writeSheet);
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
map.put("date", "2019年10月9日13:28:28");
|
||||
map.put("date", "2019-10-09 13:28:28");
|
||||
excelWriter.fill(map, writeSheet);
|
||||
}
|
||||
|
||||
List<Object> list = EasyExcel.read(file).ignoreEmptyRow(false).sheet().headRowNumber(0).doReadSync();
|
||||
Map<String, String> map0 = (Map<String, String>)list.get(0);
|
||||
Assertions.assertEquals("张三", map0.get(21));
|
||||
Assertions.assertEquals("Zhang San", map0.get(21));
|
||||
Map<String, String> map27 = (Map<String, String>)list.get(27);
|
||||
Assertions.assertEquals("张三", map27.get(0));
|
||||
Assertions.assertEquals("Zhang San", map27.get(0));
|
||||
Map<String, String> map29 = (Map<String, String>)list.get(29);
|
||||
Assertions.assertEquals("张三", map29.get(3));
|
||||
Assertions.assertEquals("Zhang San", map29.get(3));
|
||||
}
|
||||
|
||||
private void horizontalFill(File file, File template) {
|
||||
@ -173,7 +173,7 @@ public class FillDataTest {
|
||||
excelWriter.fill(data(), fillConfig, writeSheet);
|
||||
excelWriter.fill(data(), fillConfig, writeSheet);
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
map.put("date", "2019年10月9日13:28:28");
|
||||
map.put("date", "2019-10-09 13:28:28");
|
||||
excelWriter.fill(map, writeSheet);
|
||||
excelWriter.finish();
|
||||
}
|
||||
@ -181,7 +181,7 @@ public class FillDataTest {
|
||||
List<Object> list = EasyExcel.read(file).sheet().headRowNumber(0).doReadSync();
|
||||
Assertions.assertEquals(list.size(), 5L);
|
||||
Map<String, String> map0 = (Map<String, String>)list.get(0);
|
||||
Assertions.assertEquals("张三", map0.get(2));
|
||||
Assertions.assertEquals("Zhang San", map0.get(2));
|
||||
}
|
||||
|
||||
private void complexFill(File file, File template) {
|
||||
@ -191,19 +191,19 @@ public class FillDataTest {
|
||||
excelWriter.fill(data(), fillConfig, writeSheet);
|
||||
excelWriter.fill(data(), fillConfig, writeSheet);
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
map.put("date", "2019年10月9日13:28:28");
|
||||
map.put("date", "2019-10-09 13:28:28");
|
||||
map.put("total", 1000);
|
||||
excelWriter.fill(map, writeSheet);
|
||||
}
|
||||
List<Object> list = EasyExcel.read(file).sheet().headRowNumber(3).doReadSync();
|
||||
Assertions.assertEquals(list.size(), 21L);
|
||||
Map<String, String> map19 = (Map<String, String>)list.get(19);
|
||||
Assertions.assertEquals("张三", map19.get(0));
|
||||
Assertions.assertEquals("Zhang San", map19.get(0));
|
||||
}
|
||||
|
||||
private void fill(File file, File template) {
|
||||
FillData fillData = new FillData();
|
||||
fillData.setName("张三");
|
||||
fillData.setName("Zhang San");
|
||||
fillData.setNumber(5.2);
|
||||
EasyExcel.write(file, FillData.class).withTemplate(template).sheet().doFill(fillData);
|
||||
}
|
||||
@ -213,7 +213,7 @@ public class FillDataTest {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
FillData fillData = new FillData();
|
||||
list.add(fillData);
|
||||
fillData.setName("张三");
|
||||
fillData.setName("Zhang San");
|
||||
fillData.setNumber(5.2);
|
||||
if (i == 5) {
|
||||
fillData.setName(null);
|
||||
|
@ -21,19 +21,19 @@ import lombok.Setter;
|
||||
@EqualsAndHashCode
|
||||
@ContentRowHeight(100)
|
||||
public class FillAnnotationData {
|
||||
@ExcelProperty("日期")
|
||||
@DateTimeFormat("yyyy年MM月dd日HH时mm分ss秒")
|
||||
@ExcelProperty("Date")
|
||||
@DateTimeFormat("yyyy-MM-dd HH:mm:ss")
|
||||
private Date date;
|
||||
|
||||
@ExcelProperty(value = "数字")
|
||||
@ExcelProperty(value = "Number")
|
||||
@NumberFormat("#.##%")
|
||||
private Double number;
|
||||
|
||||
@ContentLoopMerge(columnExtend = 2)
|
||||
@ExcelProperty("字符串1")
|
||||
@ExcelProperty("String 1")
|
||||
private String string1;
|
||||
@ExcelProperty("字符串2")
|
||||
@ExcelProperty("String 2")
|
||||
private String string2;
|
||||
@ExcelProperty(value = "图片", converter = StringImageConverter.class)
|
||||
@ExcelProperty(value = "Image", converter = StringImageConverter.class)
|
||||
private String image;
|
||||
}
|
||||
|
@ -70,7 +70,7 @@ public class FillAnnotationDataTest {
|
||||
Date date = cell10.getDateCellValue();
|
||||
Assertions.assertEquals(DateUtils.parseDate("2020-01-01 01:01:01").getTime(), date.getTime());
|
||||
String dataFormatString = cell10.getCellStyle().getDataFormatString();
|
||||
Assertions.assertEquals("yyyy年MM月dd日HH时mm分ss秒", dataFormatString);
|
||||
Assertions.assertEquals("yyyy-MM-dd HH:mm:ss", dataFormatString);
|
||||
Cell cell11 = row1.getCell(1);
|
||||
Assertions.assertEquals(99.99, cell11.getNumericCellValue(), 2);
|
||||
boolean hasMerge = false;
|
||||
|
@ -61,7 +61,7 @@ public class FillStyleAnnotatedTest {
|
||||
|
||||
private void t01Fill07check(XSSFRow row) {
|
||||
XSSFCell cell0 = row.getCell(0);
|
||||
Assertions.assertEquals("张三", cell0.getStringCellValue());
|
||||
Assertions.assertEquals("Zhang San", cell0.getStringCellValue());
|
||||
Assertions.assertEquals(49, cell0.getCellStyle().getDataFormat());
|
||||
Assertions.assertEquals("FFFFFF00", cell0.getCellStyle().getFillForegroundColorColor().getARGBHex());
|
||||
Assertions.assertEquals("FF808000", cell0.getCellStyle().getFont().getXSSFColor().getARGBHex());
|
||||
@ -83,21 +83,21 @@ public class FillStyleAnnotatedTest {
|
||||
Assertions.assertTrue(cell2.getCellStyle().getFont().getBold());
|
||||
|
||||
XSSFCell cell3 = row.getCell(3);
|
||||
Assertions.assertEquals("张三今年5.2岁了", cell3.getStringCellValue());
|
||||
Assertions.assertEquals("Zhang San is 5.2 years old this year", cell3.getStringCellValue());
|
||||
Assertions.assertEquals(0, cell3.getCellStyle().getDataFormat());
|
||||
Assertions.assertEquals("FFFF0000", cell3.getCellStyle().getFillForegroundColorColor().getARGBHex());
|
||||
Assertions.assertEquals("FFEEECE1", cell3.getCellStyle().getFont().getXSSFColor().getARGBHex());
|
||||
Assertions.assertTrue(cell3.getCellStyle().getFont().getBold());
|
||||
|
||||
XSSFCell cell4 = row.getCell(4);
|
||||
Assertions.assertEquals("{.name}忽略,张三", cell4.getStringCellValue());
|
||||
Assertions.assertEquals("{.name} ignored,Zhang San", cell4.getStringCellValue());
|
||||
Assertions.assertEquals(0, cell4.getCellStyle().getDataFormat());
|
||||
Assertions.assertEquals("FFC00000", cell4.getCellStyle().getFillForegroundColorColor().getARGBHex());
|
||||
Assertions.assertEquals("FF000000", cell4.getCellStyle().getFont().getXSSFColor().getARGBHex());
|
||||
Assertions.assertFalse(cell4.getCellStyle().getFont().getBold());
|
||||
|
||||
XSSFCell cell5 = row.getCell(5);
|
||||
Assertions.assertEquals("空", cell5.getStringCellValue());
|
||||
Assertions.assertEquals("Empty", cell5.getStringCellValue());
|
||||
Assertions.assertEquals(0, cell5.getCellStyle().getDataFormat());
|
||||
Assertions.assertEquals("FFF79646", cell5.getCellStyle().getFillForegroundColorColor().getARGBHex());
|
||||
Assertions.assertEquals("FF8064A2", cell5.getCellStyle().getFont().getXSSFColor().getARGBHex());
|
||||
@ -115,7 +115,7 @@ public class FillStyleAnnotatedTest {
|
||||
|
||||
private void t02Fill03check(HSSFWorkbook workbook, HSSFRow row) {
|
||||
HSSFCell cell0 = row.getCell(0);
|
||||
Assertions.assertEquals("张三", cell0.getStringCellValue());
|
||||
Assertions.assertEquals("Zhang San", cell0.getStringCellValue());
|
||||
Assertions.assertEquals(49, cell0.getCellStyle().getDataFormat());
|
||||
Assertions.assertEquals("FFFF:FFFF:0", cell0.getCellStyle().getFillForegroundColorColor().getHexString());
|
||||
Assertions.assertEquals("8080:8080:0", cell0.getCellStyle().getFont(workbook).getHSSFColor(workbook)
|
||||
@ -140,7 +140,7 @@ public class FillStyleAnnotatedTest {
|
||||
Assertions.assertTrue(cell2.getCellStyle().getFont(workbook).getBold());
|
||||
|
||||
HSSFCell cell3 = row.getCell(3);
|
||||
Assertions.assertEquals("张三今年5.2岁了", cell3.getStringCellValue());
|
||||
Assertions.assertEquals("Zhang San is 5.2 years old this year", cell3.getStringCellValue());
|
||||
Assertions.assertEquals(0, cell3.getCellStyle().getDataFormat());
|
||||
Assertions.assertEquals("FFFF:0:0", cell3.getCellStyle().getFillForegroundColorColor().getHexString());
|
||||
Assertions.assertEquals("FFFF:FFFF:9999", cell3.getCellStyle().getFont(workbook).getHSSFColor(workbook)
|
||||
@ -148,7 +148,7 @@ public class FillStyleAnnotatedTest {
|
||||
Assertions.assertTrue(cell3.getCellStyle().getFont(workbook).getBold());
|
||||
|
||||
HSSFCell cell4 = row.getCell(4);
|
||||
Assertions.assertEquals("{.name}忽略,张三", cell4.getStringCellValue());
|
||||
Assertions.assertEquals("{.name} ignored,Zhang San", cell4.getStringCellValue());
|
||||
Assertions.assertEquals(0, cell4.getCellStyle().getDataFormat());
|
||||
Assertions.assertEquals("9999:3333:0", cell4.getCellStyle().getFillForegroundColorColor().getHexString());
|
||||
Assertions.assertEquals("3333:3333:3333", cell4.getCellStyle().getFont(workbook).getHSSFColor(workbook)
|
||||
@ -156,7 +156,7 @@ public class FillStyleAnnotatedTest {
|
||||
Assertions.assertFalse(cell4.getCellStyle().getFont(workbook).getBold());
|
||||
|
||||
HSSFCell cell5 = row.getCell(5);
|
||||
Assertions.assertEquals("空", cell5.getStringCellValue());
|
||||
Assertions.assertEquals("Empty", cell5.getStringCellValue());
|
||||
Assertions.assertEquals(0, cell5.getCellStyle().getDataFormat());
|
||||
Assertions.assertEquals("9999:3333:0", cell5.getCellStyle().getFillForegroundColorColor().getHexString());
|
||||
Assertions.assertEquals("CCCC:9999:FFFF", cell5.getCellStyle().getFont(workbook).getHSSFColor(workbook)
|
||||
@ -170,7 +170,7 @@ public class FillStyleAnnotatedTest {
|
||||
|
||||
private void t11FillStyleHandler07check(XSSFRow row) {
|
||||
XSSFCell cell0 = row.getCell(0);
|
||||
Assertions.assertEquals("张三", cell0.getStringCellValue());
|
||||
Assertions.assertEquals("Zhang San", cell0.getStringCellValue());
|
||||
Assertions.assertEquals(49, cell0.getCellStyle().getDataFormat());
|
||||
Assertions.assertEquals("FFFFFF00", cell0.getCellStyle().getFillForegroundColorColor().getARGBHex());
|
||||
Assertions.assertEquals("FF808000", cell0.getCellStyle().getFont().getXSSFColor().getARGBHex());
|
||||
@ -192,21 +192,21 @@ public class FillStyleAnnotatedTest {
|
||||
Assertions.assertTrue(cell2.getCellStyle().getFont().getBold());
|
||||
|
||||
XSSFCell cell3 = row.getCell(3);
|
||||
Assertions.assertEquals("张三今年5岁了", cell3.getStringCellValue());
|
||||
Assertions.assertEquals("Zhang San is 5 years old this year", cell3.getStringCellValue());
|
||||
Assertions.assertEquals(0, cell3.getCellStyle().getDataFormat());
|
||||
Assertions.assertEquals("FF0000FF", cell3.getCellStyle().getFillForegroundColorColor().getARGBHex());
|
||||
Assertions.assertEquals("FF000080", cell3.getCellStyle().getFont().getXSSFColor().getARGBHex());
|
||||
Assertions.assertTrue(cell3.getCellStyle().getFont().getBold());
|
||||
|
||||
XSSFCell cell4 = row.getCell(4);
|
||||
Assertions.assertEquals("{.name}忽略,张三", cell4.getStringCellValue());
|
||||
Assertions.assertEquals("{.name} ignored,Zhang San", cell4.getStringCellValue());
|
||||
Assertions.assertEquals(0, cell4.getCellStyle().getDataFormat());
|
||||
Assertions.assertEquals("FFFFFF00", cell4.getCellStyle().getFillForegroundColorColor().getARGBHex());
|
||||
Assertions.assertEquals("FF808000", cell4.getCellStyle().getFont().getXSSFColor().getARGBHex());
|
||||
Assertions.assertTrue(cell4.getCellStyle().getFont().getBold());
|
||||
|
||||
XSSFCell cell5 = row.getCell(5);
|
||||
Assertions.assertEquals("空", cell5.getStringCellValue());
|
||||
Assertions.assertEquals("Empty", cell5.getStringCellValue());
|
||||
Assertions.assertEquals(0, cell5.getCellStyle().getDataFormat());
|
||||
Assertions.assertEquals("FF008080", cell5.getCellStyle().getFillForegroundColorColor().getARGBHex());
|
||||
Assertions.assertEquals("FF003366", cell5.getCellStyle().getFont().getXSSFColor().getARGBHex());
|
||||
@ -215,7 +215,7 @@ public class FillStyleAnnotatedTest {
|
||||
|
||||
private void t12FillStyleHandler03check(HSSFWorkbook workbook, HSSFRow row) {
|
||||
HSSFCell cell0 = row.getCell(0);
|
||||
Assertions.assertEquals("张三", cell0.getStringCellValue());
|
||||
Assertions.assertEquals("Zhang San", cell0.getStringCellValue());
|
||||
Assertions.assertEquals(49, cell0.getCellStyle().getDataFormat());
|
||||
Assertions.assertEquals("FFFF:FFFF:0", cell0.getCellStyle().getFillForegroundColorColor().getHexString());
|
||||
Assertions.assertEquals("8080:8080:0", cell0.getCellStyle().getFont(workbook).getHSSFColor(workbook)
|
||||
@ -240,7 +240,7 @@ public class FillStyleAnnotatedTest {
|
||||
Assertions.assertTrue(cell2.getCellStyle().getFont(workbook).getBold());
|
||||
|
||||
HSSFCell cell3 = row.getCell(3);
|
||||
Assertions.assertEquals("张三今年5岁了", cell3.getStringCellValue());
|
||||
Assertions.assertEquals("Zhang San is 5 years old this year", cell3.getStringCellValue());
|
||||
Assertions.assertEquals(0, cell3.getCellStyle().getDataFormat());
|
||||
Assertions.assertEquals("0:0:FFFF", cell3.getCellStyle().getFillForegroundColorColor().getHexString());
|
||||
Assertions.assertEquals("0:0:8080", cell3.getCellStyle().getFont(workbook).getHSSFColor(workbook)
|
||||
@ -248,7 +248,7 @@ public class FillStyleAnnotatedTest {
|
||||
Assertions.assertTrue(cell3.getCellStyle().getFont(workbook).getBold());
|
||||
|
||||
HSSFCell cell4 = row.getCell(4);
|
||||
Assertions.assertEquals("{.name}忽略,张三", cell4.getStringCellValue());
|
||||
Assertions.assertEquals("{.name} ignored,Zhang San", cell4.getStringCellValue());
|
||||
Assertions.assertEquals(0, cell4.getCellStyle().getDataFormat());
|
||||
Assertions.assertEquals("FFFF:FFFF:0", cell4.getCellStyle().getFillForegroundColorColor().getHexString());
|
||||
Assertions.assertEquals("8080:8080:0", cell4.getCellStyle().getFont(workbook).getHSSFColor(workbook)
|
||||
@ -256,7 +256,7 @@ public class FillStyleAnnotatedTest {
|
||||
Assertions.assertTrue(cell4.getCellStyle().getFont(workbook).getBold());
|
||||
|
||||
HSSFCell cell5 = row.getCell(5);
|
||||
Assertions.assertEquals("空", cell5.getStringCellValue());
|
||||
Assertions.assertEquals("Empty", cell5.getStringCellValue());
|
||||
Assertions.assertEquals(0, cell5.getCellStyle().getDataFormat());
|
||||
Assertions.assertEquals("0:8080:8080", cell5.getCellStyle().getFillForegroundColorColor().getHexString());
|
||||
Assertions.assertEquals("0:3333:6666", cell5.getCellStyle().getFont(workbook).getHSSFColor(workbook)
|
||||
@ -316,7 +316,7 @@ public class FillStyleAnnotatedTest {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
FillStyleAnnotatedData fillData = new FillStyleAnnotatedData();
|
||||
list.add(fillData);
|
||||
fillData.setName("张三");
|
||||
fillData.setName("Zhang San");
|
||||
fillData.setNumber(5.2);
|
||||
fillData.setDate(DateUtils.parseDate("2020-01-01 01:01:01"));
|
||||
if (i == 5) {
|
||||
|
@ -64,7 +64,7 @@ public class FillStyleDataTest {
|
||||
|
||||
private void t01Fill07check(XSSFRow row) {
|
||||
XSSFCell cell0 = row.getCell(0);
|
||||
Assertions.assertEquals("张三", cell0.getStringCellValue());
|
||||
Assertions.assertEquals("Zhang San", cell0.getStringCellValue());
|
||||
Assertions.assertEquals(49, cell0.getCellStyle().getDataFormat());
|
||||
Assertions.assertEquals("FF00B050", cell0.getCellStyle().getFillForegroundColorColor().getARGBHex());
|
||||
Assertions.assertEquals("FF7030A0", cell0.getCellStyle().getFont().getXSSFColor().getARGBHex());
|
||||
@ -86,21 +86,21 @@ public class FillStyleDataTest {
|
||||
Assertions.assertTrue(cell2.getCellStyle().getFont().getBold());
|
||||
|
||||
XSSFCell cell3 = row.getCell(3);
|
||||
Assertions.assertEquals("张三今年5.2岁了", cell3.getStringCellValue());
|
||||
Assertions.assertEquals("Zhang San is 5.2 years old this year", cell3.getStringCellValue());
|
||||
Assertions.assertEquals(0, cell3.getCellStyle().getDataFormat());
|
||||
Assertions.assertEquals("FFFF0000", cell3.getCellStyle().getFillForegroundColorColor().getARGBHex());
|
||||
Assertions.assertEquals("FFEEECE1", cell3.getCellStyle().getFont().getXSSFColor().getARGBHex());
|
||||
Assertions.assertTrue(cell3.getCellStyle().getFont().getBold());
|
||||
|
||||
XSSFCell cell4 = row.getCell(4);
|
||||
Assertions.assertEquals("{.name}忽略,张三", cell4.getStringCellValue());
|
||||
Assertions.assertEquals("{.name} ignored,Zhang San", cell4.getStringCellValue());
|
||||
Assertions.assertEquals(0, cell4.getCellStyle().getDataFormat());
|
||||
Assertions.assertEquals("FFC00000", cell4.getCellStyle().getFillForegroundColorColor().getARGBHex());
|
||||
Assertions.assertEquals("FF000000", cell4.getCellStyle().getFont().getXSSFColor().getARGBHex());
|
||||
Assertions.assertFalse(cell4.getCellStyle().getFont().getBold());
|
||||
|
||||
XSSFCell cell5 = row.getCell(5);
|
||||
Assertions.assertEquals("空", cell5.getStringCellValue());
|
||||
Assertions.assertEquals("Empty", cell5.getStringCellValue());
|
||||
Assertions.assertEquals(0, cell5.getCellStyle().getDataFormat());
|
||||
Assertions.assertEquals("FFF79646", cell5.getCellStyle().getFillForegroundColorColor().getARGBHex());
|
||||
Assertions.assertEquals("FF8064A2", cell5.getCellStyle().getFont().getXSSFColor().getARGBHex());
|
||||
@ -118,7 +118,7 @@ public class FillStyleDataTest {
|
||||
|
||||
private void t02Fill03check(HSSFWorkbook workbook, HSSFRow row) {
|
||||
HSSFCell cell0 = row.getCell(0);
|
||||
Assertions.assertEquals("张三", cell0.getStringCellValue());
|
||||
Assertions.assertEquals("Zhang San", cell0.getStringCellValue());
|
||||
Assertions.assertEquals(49, cell0.getCellStyle().getDataFormat());
|
||||
Assertions.assertEquals("0:8080:0", cell0.getCellStyle().getFillForegroundColorColor().getHexString());
|
||||
Assertions.assertEquals("8080:0:8080", cell0.getCellStyle().getFont(workbook).getHSSFColor(workbook)
|
||||
@ -143,7 +143,7 @@ public class FillStyleDataTest {
|
||||
Assertions.assertTrue(cell2.getCellStyle().getFont(workbook).getBold());
|
||||
|
||||
HSSFCell cell3 = row.getCell(3);
|
||||
Assertions.assertEquals("张三今年5.2岁了", cell3.getStringCellValue());
|
||||
Assertions.assertEquals("Zhang San is 5.2 years old this year", cell3.getStringCellValue());
|
||||
Assertions.assertEquals(0, cell3.getCellStyle().getDataFormat());
|
||||
Assertions.assertEquals("FFFF:0:0", cell3.getCellStyle().getFillForegroundColorColor().getHexString());
|
||||
Assertions.assertEquals("FFFF:FFFF:9999", cell3.getCellStyle().getFont(workbook).getHSSFColor(workbook)
|
||||
@ -151,7 +151,7 @@ public class FillStyleDataTest {
|
||||
Assertions.assertTrue(cell3.getCellStyle().getFont(workbook).getBold());
|
||||
|
||||
HSSFCell cell4 = row.getCell(4);
|
||||
Assertions.assertEquals("{.name}忽略,张三", cell4.getStringCellValue());
|
||||
Assertions.assertEquals("{.name} ignored,Zhang San", cell4.getStringCellValue());
|
||||
Assertions.assertEquals(0, cell4.getCellStyle().getDataFormat());
|
||||
Assertions.assertEquals("9999:3333:0", cell4.getCellStyle().getFillForegroundColorColor().getHexString());
|
||||
Assertions.assertEquals("3333:3333:3333", cell4.getCellStyle().getFont(workbook).getHSSFColor(workbook)
|
||||
@ -159,7 +159,7 @@ public class FillStyleDataTest {
|
||||
Assertions.assertFalse(cell4.getCellStyle().getFont(workbook).getBold());
|
||||
|
||||
HSSFCell cell5 = row.getCell(5);
|
||||
Assertions.assertEquals("空", cell5.getStringCellValue());
|
||||
Assertions.assertEquals("Empty", cell5.getStringCellValue());
|
||||
Assertions.assertEquals(0, cell5.getCellStyle().getDataFormat());
|
||||
Assertions.assertEquals("9999:3333:0", cell5.getCellStyle().getFillForegroundColorColor().getHexString());
|
||||
Assertions.assertEquals("CCCC:9999:FFFF", cell5.getCellStyle().getFont(workbook).getHSSFColor(workbook)
|
||||
@ -182,7 +182,7 @@ public class FillStyleDataTest {
|
||||
|
||||
private void t11FillStyleHandler07check(XSSFRow row) {
|
||||
XSSFCell cell0 = row.getCell(0);
|
||||
Assertions.assertEquals("张三", cell0.getStringCellValue());
|
||||
Assertions.assertEquals("Zhang San", cell0.getStringCellValue());
|
||||
Assertions.assertEquals(49, cell0.getCellStyle().getDataFormat());
|
||||
Assertions.assertEquals("FFFFFF00", cell0.getCellStyle().getFillForegroundColorColor().getARGBHex());
|
||||
Assertions.assertEquals("FF808000", cell0.getCellStyle().getFont().getXSSFColor().getARGBHex());
|
||||
@ -204,21 +204,21 @@ public class FillStyleDataTest {
|
||||
Assertions.assertTrue(cell2.getCellStyle().getFont().getBold());
|
||||
|
||||
XSSFCell cell3 = row.getCell(3);
|
||||
Assertions.assertEquals("张三今年5.2岁了", cell3.getStringCellValue());
|
||||
Assertions.assertEquals("Zhang San is 5.2 years old this year", cell3.getStringCellValue());
|
||||
Assertions.assertEquals(0, cell3.getCellStyle().getDataFormat());
|
||||
Assertions.assertEquals("FFFF0000", cell3.getCellStyle().getFillForegroundColorColor().getARGBHex());
|
||||
Assertions.assertEquals("FFEEECE1", cell3.getCellStyle().getFont().getXSSFColor().getARGBHex());
|
||||
Assertions.assertTrue(cell3.getCellStyle().getFont().getBold());
|
||||
|
||||
XSSFCell cell4 = row.getCell(4);
|
||||
Assertions.assertEquals("{.name}忽略,张三", cell4.getStringCellValue());
|
||||
Assertions.assertEquals("{.name} ignored,Zhang San", cell4.getStringCellValue());
|
||||
Assertions.assertEquals(0, cell4.getCellStyle().getDataFormat());
|
||||
Assertions.assertEquals("FFC00000", cell4.getCellStyle().getFillForegroundColorColor().getARGBHex());
|
||||
Assertions.assertEquals("FF000000", cell4.getCellStyle().getFont().getXSSFColor().getARGBHex());
|
||||
Assertions.assertFalse(cell4.getCellStyle().getFont().getBold());
|
||||
|
||||
XSSFCell cell5 = row.getCell(5);
|
||||
Assertions.assertEquals("空", cell5.getStringCellValue());
|
||||
Assertions.assertEquals("Empty", cell5.getStringCellValue());
|
||||
Assertions.assertEquals(0, cell5.getCellStyle().getDataFormat());
|
||||
Assertions.assertEquals("FFF79646", cell5.getCellStyle().getFillForegroundColorColor().getARGBHex());
|
||||
Assertions.assertEquals("FF8064A2", cell5.getCellStyle().getFont().getXSSFColor().getARGBHex());
|
||||
@ -236,7 +236,7 @@ public class FillStyleDataTest {
|
||||
|
||||
private void t12FillStyleHandler03check(HSSFWorkbook workbook, HSSFRow row) {
|
||||
HSSFCell cell0 = row.getCell(0);
|
||||
Assertions.assertEquals("张三", cell0.getStringCellValue());
|
||||
Assertions.assertEquals("Zhang San", cell0.getStringCellValue());
|
||||
Assertions.assertEquals(49, cell0.getCellStyle().getDataFormat());
|
||||
Assertions.assertEquals("FFFF:FFFF:0", cell0.getCellStyle().getFillForegroundColorColor().getHexString());
|
||||
Assertions.assertEquals("8080:8080:0", cell0.getCellStyle().getFont(workbook).getHSSFColor(workbook)
|
||||
@ -261,7 +261,7 @@ public class FillStyleDataTest {
|
||||
Assertions.assertTrue(cell2.getCellStyle().getFont(workbook).getBold());
|
||||
|
||||
HSSFCell cell3 = row.getCell(3);
|
||||
Assertions.assertEquals("张三今年5.2岁了", cell3.getStringCellValue());
|
||||
Assertions.assertEquals("Zhang San is 5.2 years old this year", cell3.getStringCellValue());
|
||||
Assertions.assertEquals(0, cell3.getCellStyle().getDataFormat());
|
||||
Assertions.assertEquals("FFFF:0:0", cell3.getCellStyle().getFillForegroundColorColor().getHexString());
|
||||
Assertions.assertEquals("FFFF:FFFF:9999", cell3.getCellStyle().getFont(workbook).getHSSFColor(workbook)
|
||||
@ -269,7 +269,7 @@ public class FillStyleDataTest {
|
||||
Assertions.assertTrue(cell3.getCellStyle().getFont(workbook).getBold());
|
||||
|
||||
HSSFCell cell4 = row.getCell(4);
|
||||
Assertions.assertEquals("{.name}忽略,张三", cell4.getStringCellValue());
|
||||
Assertions.assertEquals("{.name} ignored,Zhang San", cell4.getStringCellValue());
|
||||
Assertions.assertEquals(0, cell4.getCellStyle().getDataFormat());
|
||||
Assertions.assertEquals("9999:3333:0", cell4.getCellStyle().getFillForegroundColorColor().getHexString());
|
||||
Assertions.assertEquals("3333:3333:3333", cell4.getCellStyle().getFont(workbook).getHSSFColor(workbook)
|
||||
@ -277,7 +277,7 @@ public class FillStyleDataTest {
|
||||
Assertions.assertFalse(cell4.getCellStyle().getFont(workbook).getBold());
|
||||
|
||||
HSSFCell cell5 = row.getCell(5);
|
||||
Assertions.assertEquals("空", cell5.getStringCellValue());
|
||||
Assertions.assertEquals("Empty", cell5.getStringCellValue());
|
||||
Assertions.assertEquals(0, cell5.getCellStyle().getDataFormat());
|
||||
Assertions.assertEquals("9999:3333:0", cell5.getCellStyle().getFillForegroundColorColor().getHexString());
|
||||
Assertions.assertEquals("CCCC:9999:FFFF", cell5.getCellStyle().getFont(workbook).getHSSFColor(workbook)
|
||||
@ -337,7 +337,7 @@ public class FillStyleDataTest {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
FillStyleData fillData = new FillStyleData();
|
||||
list.add(fillData);
|
||||
fillData.setName("张三");
|
||||
fillData.setName("Zhang San");
|
||||
fillData.setNumber(5.2);
|
||||
fillData.setDate(DateUtils.parseDate("2020-01-01 01:01:01"));
|
||||
if (i == 5) {
|
||||
|
@ -19,7 +19,7 @@ import cn.idev.excel.write.metadata.fill.FillWrapper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* 写的填充写法
|
||||
* Example of writing and filling data into Excel
|
||||
*
|
||||
* @author Jiaju Zhuang
|
||||
* @since 2.1.1
|
||||
@ -27,52 +27,52 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
public class FillTest {
|
||||
/**
|
||||
* 最简单的填充
|
||||
* Simplest example of filling data
|
||||
*
|
||||
* @since 2.1.1
|
||||
*/
|
||||
@Test
|
||||
public void simpleFill() {
|
||||
// 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
|
||||
// Template note: Use {} to indicate variables. If there are existing "{", "}" characters, use "\{", "\}" instead.
|
||||
String templateFileName =
|
||||
TestFileUtil.getPath() + "demo" + File.separator + "fill" + File.separator + "simple.xlsx";
|
||||
|
||||
// 方案1 根据对象填充
|
||||
// Option 1: Fill based on an object
|
||||
String fileName = TestFileUtil.getPath() + "simpleFill" + System.currentTimeMillis() + ".xlsx";
|
||||
// 这里 会填充到第一个sheet, 然后文件流会自动关闭
|
||||
// This will fill the first sheet, and the file stream will be automatically closed.
|
||||
FillData fillData = new FillData();
|
||||
fillData.setName("张三");
|
||||
fillData.setName("Zhang San");
|
||||
fillData.setNumber(5.2);
|
||||
EasyExcel.write(fileName).withTemplate(templateFileName).sheet().doFill(fillData);
|
||||
|
||||
// 方案2 根据Map填充
|
||||
// Option 2: Fill based on a Map
|
||||
fileName = TestFileUtil.getPath() + "simpleFill" + System.currentTimeMillis() + ".xlsx";
|
||||
// 这里 会填充到第一个sheet, 然后文件流会自动关闭
|
||||
// This will fill the first sheet, and the file stream will be automatically closed.
|
||||
Map<String, Object> map = MapUtils.newHashMap();
|
||||
map.put("name", "张三");
|
||||
map.put("name", "Zhang San");
|
||||
map.put("number", 5.2);
|
||||
EasyExcel.write(fileName).withTemplate(templateFileName).sheet().doFill(map);
|
||||
}
|
||||
|
||||
/**
|
||||
* 填充列表
|
||||
* Example of filling a list
|
||||
*
|
||||
* @since 2.1.1
|
||||
*/
|
||||
@Test
|
||||
public void listFill() {
|
||||
// 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
|
||||
// 填充list 的时候还要注意 模板中{.} 多了个点 表示list
|
||||
// 如果填充list的对象是map,必须包涵所有list的key,哪怕数据为null,必须使用map.put(key,null)
|
||||
// Template note: Use {} to indicate variables. If there are existing "{", "}" characters, use "\{", "\}" instead.
|
||||
// When filling a list, note that {.} in the template indicates a list.
|
||||
// If the object filling the list is a Map, it must contain all keys of the list, even if the data is null. Use map.put(key, null).
|
||||
String templateFileName =
|
||||
TestFileUtil.getPath() + "demo" + File.separator + "fill" + File.separator + "list.xlsx";
|
||||
|
||||
// 方案1 一下子全部放到内存里面 并填充
|
||||
// Option 1: Load all data into memory at once and fill
|
||||
String fileName = TestFileUtil.getPath() + "listFill" + System.currentTimeMillis() + ".xlsx";
|
||||
// 这里 会填充到第一个sheet, 然后文件流会自动关闭
|
||||
// This will fill the first sheet, and the file stream will be automatically closed.
|
||||
EasyExcel.write(fileName).withTemplate(templateFileName).sheet().doFill(data());
|
||||
|
||||
// 方案2 分多次 填充 会使用文件缓存(省内存)
|
||||
// Option 2: Fill in multiple passes, using file caching (saves memory)
|
||||
fileName = TestFileUtil.getPath() + "listFill" + System.currentTimeMillis() + ".xlsx";
|
||||
try (ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build()) {
|
||||
WriteSheet writeSheet = EasyExcel.writerSheet().build();
|
||||
@ -82,95 +82,95 @@ public class FillTest {
|
||||
}
|
||||
|
||||
/**
|
||||
* 复杂的填充
|
||||
* Example of complex filling
|
||||
*
|
||||
* @since 2.1.1
|
||||
*/
|
||||
@Test
|
||||
public void complexFill() {
|
||||
// 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
|
||||
// {} 代表普通变量 {.} 代表是list的变量
|
||||
// Template note: Use {} to indicate variables. If there are existing "{", "}" characters, use "\{", "\}" instead.
|
||||
// {} represents a normal variable, {.} represents a list variable.
|
||||
String templateFileName =
|
||||
TestFileUtil.getPath() + "demo" + File.separator + "fill" + File.separator + "complex.xlsx";
|
||||
|
||||
String fileName = TestFileUtil.getPath() + "complexFill" + System.currentTimeMillis() + ".xlsx";
|
||||
// 方案1
|
||||
// Option 1
|
||||
try (ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build()) {
|
||||
WriteSheet writeSheet = EasyExcel.writerSheet().build();
|
||||
// 这里注意 入参用了forceNewRow 代表在写入list的时候不管list下面有没有空行 都会创建一行,然后下面的数据往后移动。默认 是false,会直接使用下一行,如果没有则创建。
|
||||
// forceNewRow 如果设置了true,有个缺点 就是他会把所有的数据都放到内存了,所以慎用
|
||||
// 简单的说 如果你的模板有list,且list不是最后一行,下面还有数据需要填充 就必须设置 forceNewRow=true 但是这个就会把所有数据放到内存 会很耗内存
|
||||
// 如果数据量大 list不是最后一行 参照下一个
|
||||
// Note: The forceNewRow parameter is used here. When writing a list, it will always create a new row, and the data below will be shifted down. Default is false, which will use the next row if available, otherwise create a new one.
|
||||
// forceNewRow: If set to true, it will load all data into memory, so use it with caution.
|
||||
// In short, if your template has a list and the list is not the last row, and there is data below that needs to be filled, you must set forceNewRow=true. However, this will consume a lot of memory.
|
||||
// For large datasets where the list is not the last row, refer to the next example.
|
||||
FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
|
||||
excelWriter.fill(data(), fillConfig, writeSheet);
|
||||
excelWriter.fill(data(), fillConfig, writeSheet);
|
||||
Map<String, Object> map = MapUtils.newHashMap();
|
||||
map.put("date", "2019年10月9日13:28:28");
|
||||
map.put("date", "2019-10-09 13:28:28");
|
||||
map.put("total", 1000);
|
||||
excelWriter.fill(map, writeSheet);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据量大的复杂填充
|
||||
* Example of complex filling with large datasets
|
||||
* <p>
|
||||
* 这里的解决方案是 确保模板list为最后一行,然后再拼接table.还有03版没救,只能刚正面加内存。
|
||||
* The solution here is to ensure that the list in the template is the last row, and then append a table. For Excel 2003, there is no solution other than increasing memory.
|
||||
*
|
||||
* @since 2.1.1
|
||||
*/
|
||||
@Test
|
||||
public void complexFillWithTable() {
|
||||
// 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
|
||||
// {} 代表普通变量 {.} 代表是list的变量
|
||||
// 这里模板 删除了list以后的数据,也就是统计的这一行
|
||||
// Template note: Use {} to indicate variables. If there are existing "{", "}" characters, use "\{", "\}" instead.
|
||||
// {} represents a normal variable, {.} represents a list variable.
|
||||
// Here, the template deletes the data after the list, i.e., the summary row.
|
||||
String templateFileName =
|
||||
TestFileUtil.getPath() + "demo" + File.separator + "fill" + File.separator + "complexFillWithTable.xlsx";
|
||||
|
||||
String fileName = TestFileUtil.getPath() + "complexFillWithTable" + System.currentTimeMillis() + ".xlsx";
|
||||
|
||||
// 方案1
|
||||
// Option 1
|
||||
try (ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build()) {
|
||||
WriteSheet writeSheet = EasyExcel.writerSheet().build();
|
||||
// 直接写入数据
|
||||
// Directly write data
|
||||
excelWriter.fill(data(), writeSheet);
|
||||
excelWriter.fill(data(), writeSheet);
|
||||
|
||||
// 写入list之前的数据
|
||||
// Write data before the list
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
map.put("date", "2019年10月9日13:28:28");
|
||||
map.put("date", "2019-10-09 13:28:28");
|
||||
excelWriter.fill(map, writeSheet);
|
||||
|
||||
// list 后面还有个统计 想办法手动写入
|
||||
// 这里偷懒直接用list 也可以用对象
|
||||
// There is a summary after the list, which needs to be written manually.
|
||||
// Here, we use a list for simplicity. You can also use an object.
|
||||
List<List<String>> totalListList = ListUtils.newArrayList();
|
||||
List<String> totalList = ListUtils.newArrayList();
|
||||
totalListList.add(totalList);
|
||||
totalList.add(null);
|
||||
totalList.add(null);
|
||||
totalList.add(null);
|
||||
// 第四列
|
||||
totalList.add("统计:1000");
|
||||
// 这里是write 别和fill 搞错了
|
||||
// Fourth column
|
||||
totalList.add("Total:1000");
|
||||
// Note: Use write here, not fill.
|
||||
excelWriter.write(totalListList, writeSheet);
|
||||
// 总体上写法比较复杂 但是也没有想到好的版本 异步的去写入excel 不支持行的删除和移动,也不支持备注这种的写入,所以也排除了可以
|
||||
// 新建一个 然后一点点复制过来的方案,最后导致list需要新增行的时候,后面的列的数据没法后移,后续会继续想想解决方案
|
||||
// Overall, the writing is complex, but there is no better solution. Asynchronous writing to Excel does not support row deletion or movement, nor does it support writing comments, so this approach is used.
|
||||
// The idea is to create a new sheet and copy data bit by bit. However, when adding rows to the list, the data in the columns below cannot be shifted. A better solution will be explored in the future.
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 横向的填充
|
||||
* Example of horizontal filling
|
||||
*
|
||||
* @since 2.1.1
|
||||
*/
|
||||
@Test
|
||||
public void horizontalFill() {
|
||||
// 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
|
||||
// {} 代表普通变量 {.} 代表是list的变量
|
||||
// Template note: Use {} to indicate variables. If there are existing "{", "}" characters, use "\{", "\}" instead.
|
||||
// {} represents a normal variable, {.} represents a list variable.
|
||||
String templateFileName =
|
||||
TestFileUtil.getPath() + "demo" + File.separator + "fill" + File.separator + "horizontal.xlsx";
|
||||
|
||||
String fileName = TestFileUtil.getPath() + "horizontalFill" + System.currentTimeMillis() + ".xlsx";
|
||||
// 方案1
|
||||
// Option 1
|
||||
try (ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build()) {
|
||||
WriteSheet writeSheet = EasyExcel.writerSheet().build();
|
||||
FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build();
|
||||
@ -178,30 +178,30 @@ public class FillTest {
|
||||
excelWriter.fill(data(), fillConfig, writeSheet);
|
||||
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("date", "2019年10月9日13:28:28");
|
||||
map.put("date", "2019-10-09 13:28:28");
|
||||
excelWriter.fill(map, writeSheet);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 多列表组合填充填充
|
||||
* Example of composite filling with multiple lists
|
||||
*
|
||||
* @since 2.2.0-beta1
|
||||
*/
|
||||
@Test
|
||||
public void compositeFill() {
|
||||
// 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
|
||||
// {} 代表普通变量 {.} 代表是list的变量 {前缀.} 前缀可以区分不同的list
|
||||
// Template note: Use {} to indicate variables. If there are existing "{", "}" characters, use "\{", "\}" instead.
|
||||
// {} represents a normal variable, {.} represents a list variable, {prefix.} prefix can distinguish different lists.
|
||||
String templateFileName =
|
||||
TestFileUtil.getPath() + "demo" + File.separator + "fill" + File.separator + "composite.xlsx";
|
||||
|
||||
String fileName = TestFileUtil.getPath() + "compositeFill" + System.currentTimeMillis() + ".xlsx";
|
||||
|
||||
// 方案1
|
||||
// Option 1
|
||||
try (ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build()) {
|
||||
WriteSheet writeSheet = EasyExcel.writerSheet().build();
|
||||
FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build();
|
||||
// 如果有多个list 模板上必须有{前缀.} 这里的前缀就是 data1,然后多个list必须用 FillWrapper包裹
|
||||
// If there are multiple lists, the template must have {prefix.}. Here, the prefix is data1, and multiple lists must be wrapped with FillWrapper.
|
||||
excelWriter.fill(new FillWrapper("data1", data()), fillConfig, writeSheet);
|
||||
excelWriter.fill(new FillWrapper("data1", data()), fillConfig, writeSheet);
|
||||
excelWriter.fill(new FillWrapper("data2", data()), writeSheet);
|
||||
@ -210,7 +210,6 @@ public class FillTest {
|
||||
excelWriter.fill(new FillWrapper("data3", data()), writeSheet);
|
||||
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
//map.put("date", "2019年10月9日13:28:28");
|
||||
map.put("date", new Date());
|
||||
|
||||
excelWriter.fill(map, writeSheet);
|
||||
@ -222,7 +221,7 @@ public class FillTest {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
FillData fillData = new FillData();
|
||||
list.add(fillData);
|
||||
fillData.setName("张三");
|
||||
fillData.setName("Zhang San");
|
||||
fillData.setNumber(5.2);
|
||||
fillData.setDate(new Date());
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ import cn.idev.excel.write.metadata.fill.FillWrapper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* 写的填充写法
|
||||
* Example of filling data into Excel templates.
|
||||
*
|
||||
* @author Jiaju Zhuang
|
||||
* @since 2.1.1
|
||||
@ -27,141 +27,146 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
public class FillTempTest {
|
||||
/**
|
||||
* 最简单的填充
|
||||
* Simplest example of filling data.
|
||||
*
|
||||
* @since 2.1.1
|
||||
*/
|
||||
@Test
|
||||
public void simpleFill() {
|
||||
// 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
|
||||
// Template note: Use {} to represent variables. If the template contains "{", "}" as special characters, use "\{", "\}" instead.
|
||||
String templateFileName = "/Users/zhuangjiaju/Downloads/simple.xlsx";
|
||||
|
||||
// 方案1 根据对象填充
|
||||
// Option 1: Fill using an object
|
||||
String fileName = TestFileUtil.getPath() + "simpleFill" + System.currentTimeMillis() + ".xlsx";
|
||||
// 这里 会填充到第一个sheet, 然后文件流会自动关闭
|
||||
// This will fill the first sheet, and the file stream will be closed automatically.
|
||||
FillData fillData = new FillData();
|
||||
fillData.setName("张三");
|
||||
fillData.setName("Zhang San");
|
||||
fillData.setNumber(5.2);
|
||||
EasyExcel.write(fileName).withTemplate(templateFileName).sheet().doFill(fillData);
|
||||
|
||||
//// 方案2 根据Map填充
|
||||
//fileName = TestFileUtil.getPath() + "simpleFill" + System.currentTimeMillis() + ".xlsx";
|
||||
//// 这里 会填充到第一个sheet, 然后文件流会自动关闭
|
||||
//Map<String, Object> map = new HashMap<String, Object>();
|
||||
//map.put("name", "张三");
|
||||
//map.put("number", 5.2);
|
||||
//EasyExcel.write(fileName).withTemplate(templateFileName).sheet().doFill(map);
|
||||
/*
|
||||
// Option 2: Fill using a Map
|
||||
fileName = TestFileUtil.getPath() + "simpleFill" + System.currentTimeMillis() + ".xlsx";
|
||||
// This will fill the first sheet, and the file stream will be closed automatically.
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
map.put("name", "Zhang San");
|
||||
map.put("number", 5.2);
|
||||
EasyExcel.write(fileName).withTemplate(templateFileName).sheet().doFill(map);
|
||||
*/
|
||||
}
|
||||
|
||||
/**
|
||||
* 填充列表
|
||||
* Example of filling a list of data.
|
||||
*
|
||||
* @since 2.1.1
|
||||
*/
|
||||
@Test
|
||||
public void listFill() {
|
||||
// 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
|
||||
// 填充list 的时候还要注意 模板中{.} 多了个点 表示list
|
||||
// Template note: Use {} to represent variables. If the template contains "{", "}" as special characters, use "\{", "\}" instead.
|
||||
// When filling a list, note that {.} in the template indicates a list.
|
||||
String templateFileName =
|
||||
TestFileUtil.getPath() + "demo" + File.separator + "fill" + File.separator + "list.xlsx";
|
||||
|
||||
// 方案1 一下子全部放到内存里面 并填充
|
||||
// Option 1: Load all data into memory at once and fill
|
||||
String fileName = TestFileUtil.getPath() + "listFill" + System.currentTimeMillis() + ".xlsx";
|
||||
// 这里 会填充到第一个sheet, 然后文件流会自动关闭
|
||||
// This will fill the first sheet, and the file stream will be closed automatically.
|
||||
EasyExcel.write(fileName).withTemplate(templateFileName).sheet().doFill(data());
|
||||
|
||||
// 方案2 分多次 填充 会使用文件缓存(省内存)
|
||||
// Option 2: Fill in multiple passes using file caching (saves memory)
|
||||
fileName = TestFileUtil.getPath() + "listFill" + System.currentTimeMillis() + ".xlsx";
|
||||
ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build();
|
||||
WriteSheet writeSheet = EasyExcel.writerSheet().build();
|
||||
excelWriter.fill(data(), writeSheet);
|
||||
excelWriter.fill(data(), writeSheet);
|
||||
// 千万别忘记关闭流
|
||||
// Do not forget to close the stream
|
||||
excelWriter.finish();
|
||||
}
|
||||
|
||||
/**
|
||||
* 复杂的填充
|
||||
* Example of complex data filling.
|
||||
*
|
||||
* @since 2.1.1
|
||||
*/
|
||||
@Test
|
||||
public void complexFill() {
|
||||
// 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
|
||||
// {} 代表普通变量 {.} 代表是list的变量
|
||||
// Template note: Use {} to represent variables. If the template contains "{", "}" as special characters, use "\{", "\}" instead.
|
||||
// {} represents a regular variable, {.} represents a list variable.
|
||||
String templateFileName =
|
||||
TestFileUtil.getPath() + "demo" + File.separator + "fill" + File.separator + "complex.xlsx";
|
||||
|
||||
String fileName = TestFileUtil.getPath() + "complexFill" + System.currentTimeMillis() + ".xlsx";
|
||||
ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build();
|
||||
WriteSheet writeSheet = EasyExcel.writerSheet().build();
|
||||
// 这里注意 入参用了forceNewRow 代表在写入list的时候不管list下面有没有空行 都会创建一行,然后下面的数据往后移动。默认 是false,会直接使用下一行,如果没有则创建。
|
||||
// forceNewRow 如果设置了true,有个缺点 就是他会把所有的数据都放到内存了,所以慎用
|
||||
// 简单的说 如果你的模板有list,且list不是最后一行,下面还有数据需要填充 就必须设置 forceNewRow=true 但是这个就会把所有数据放到内存 会很耗内存
|
||||
// 如果数据量大 list不是最后一行 参照下一个
|
||||
// Note: The `forceNewRow` parameter ensures that a new row is created when writing a list, even if there are no empty rows below it.
|
||||
// By default, it is false, meaning it will use the next row if available, or create one if not.
|
||||
// Setting `forceNewRow=true` has the drawback of loading all data into memory, so use it cautiously.
|
||||
// If your template has a list and data below it, you must set `forceNewRow=true`, but this will consume more memory.
|
||||
// For large datasets where the list is not the last row, refer to the next example.
|
||||
FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
|
||||
excelWriter.fill(data(), fillConfig, writeSheet);
|
||||
excelWriter.fill(data(), fillConfig, writeSheet);
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
map.put("date", "2019年10月9日13:28:28");
|
||||
map.put("date", "2019-10-09 13:28:28");
|
||||
map.put("total", 1000);
|
||||
excelWriter.fill(map, writeSheet);
|
||||
excelWriter.finish();
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据量大的复杂填充
|
||||
* Example of complex data filling with large datasets.
|
||||
* <p>
|
||||
* 这里的解决方案是 确保模板list为最后一行,然后再拼接table.还有03版没救,只能刚正面加内存。
|
||||
* The solution here is to ensure the list in the template is the last row, then append a table.
|
||||
* Note: Excel 2003 format is not supported and requires more memory.
|
||||
*
|
||||
* @since 2.1.1
|
||||
*/
|
||||
@Test
|
||||
public void complexFillWithTable() {
|
||||
// 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
|
||||
// {} 代表普通变量 {.} 代表是list的变量
|
||||
// 这里模板 删除了list以后的数据,也就是统计的这一行
|
||||
// Template note: Use {} to represent variables. If the template contains "{", "}" as special characters, use "\{", "\}" instead.
|
||||
// {} represents a regular variable, {.} represents a list variable.
|
||||
// Here, the template removes data after the list, such as summary rows.
|
||||
String templateFileName =
|
||||
TestFileUtil.getPath() + "demo" + File.separator + "fill" + File.separator + "complexFillWithTable.xlsx";
|
||||
|
||||
String fileName = TestFileUtil.getPath() + "complexFillWithTable" + System.currentTimeMillis() + ".xlsx";
|
||||
ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build();
|
||||
WriteSheet writeSheet = EasyExcel.writerSheet().build();
|
||||
// 直接写入数据
|
||||
// Directly write data
|
||||
excelWriter.fill(data(), writeSheet);
|
||||
excelWriter.fill(data(), writeSheet);
|
||||
|
||||
// 写入list之前的数据
|
||||
// Write data before the list
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
map.put("date", "2019年10月9日13:28:28");
|
||||
map.put("date", "2019-10-09 13:28:28");
|
||||
excelWriter.fill(map, writeSheet);
|
||||
|
||||
// list 后面还有个统计 想办法手动写入
|
||||
// 这里偷懒直接用list 也可以用对象
|
||||
// Manually write summary data after the list
|
||||
// Here, we use a list for simplicity, but an object could also be used.
|
||||
List<List<String>> totalListList = new ArrayList<List<String>>();
|
||||
List<String> totalList = new ArrayList<String>();
|
||||
totalListList.add(totalList);
|
||||
totalList.add(null);
|
||||
totalList.add(null);
|
||||
totalList.add(null);
|
||||
// 第四列
|
||||
totalList.add("统计:1000");
|
||||
// 这里是write 别和fill 搞错了
|
||||
// Fourth column
|
||||
totalList.add("Total: 1000");
|
||||
// Use `write` instead of `fill` here
|
||||
excelWriter.write(totalListList, writeSheet);
|
||||
excelWriter.finish();
|
||||
// 总体上写法比较复杂 但是也没有想到好的版本 异步的去写入excel 不支持行的删除和移动,也不支持备注这种的写入,所以也排除了可以
|
||||
// 新建一个 然后一点点复制过来的方案,最后导致list需要新增行的时候,后面的列的数据没法后移,后续会继续想想解决方案
|
||||
// Overall, this approach is complex, but no better solution is available.
|
||||
// Asynchronous writing to Excel does not support row deletion or movement, nor does it support comments.
|
||||
// Therefore, this workaround is necessary.
|
||||
}
|
||||
|
||||
/**
|
||||
* 横向的填充
|
||||
* Example of horizontal data filling.
|
||||
*
|
||||
* @since 2.1.1
|
||||
*/
|
||||
@Test
|
||||
public void horizontalFill() {
|
||||
// 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
|
||||
// {} 代表普通变量 {.} 代表是list的变量
|
||||
// Template note: Use {} to represent variables. If the template contains "{", "}" as special characters, use "\{", "\}" instead.
|
||||
// {} represents a regular variable, {.} represents a list variable.
|
||||
String templateFileName =
|
||||
TestFileUtil.getPath() + "demo" + File.separator + "fill" + File.separator + "horizontal.xlsx";
|
||||
|
||||
@ -173,22 +178,22 @@ public class FillTempTest {
|
||||
excelWriter.fill(data(), fillConfig, writeSheet);
|
||||
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
map.put("date", "2019年10月9日13:28:28");
|
||||
map.put("date", "2019-10-09 13:28:28");
|
||||
excelWriter.fill(map, writeSheet);
|
||||
|
||||
// 别忘记关闭流
|
||||
// Do not forget to close the stream
|
||||
excelWriter.finish();
|
||||
}
|
||||
|
||||
/**
|
||||
* 多列表组合填充填充
|
||||
* Example of composite data filling with multiple lists.
|
||||
*
|
||||
* @since 2.2.0-beta1
|
||||
*/
|
||||
@Test
|
||||
public void compositeFill() {
|
||||
// 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
|
||||
// {} 代表普通变量 {.} 代表是list的变量 {前缀.} 前缀可以区分不同的list
|
||||
// Template note: Use {} to represent variables. If the template contains "{", "}" as special characters, use "\{", "\}" instead.
|
||||
// {} represents a regular variable, {.} represents a list variable, and {prefix.} distinguishes different lists.
|
||||
String templateFileName =
|
||||
TestFileUtil.getPath() + "demo" + File.separator + "fill" + File.separator + "composite.xlsx";
|
||||
|
||||
@ -196,7 +201,8 @@ public class FillTempTest {
|
||||
ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build();
|
||||
WriteSheet writeSheet = EasyExcel.writerSheet().build();
|
||||
FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build();
|
||||
// 如果有多个list 模板上必须有{前缀.} 这里的前缀就是 data1,然后多个list必须用 FillWrapper包裹
|
||||
// If there are multiple lists, the template must use {prefix.}, where "data1" is the prefix.
|
||||
// Multiple lists must be wrapped in FillWrapper.
|
||||
excelWriter.fill(new FillWrapper("data1", data()), fillConfig, writeSheet);
|
||||
excelWriter.fill(new FillWrapper("data1", data()), fillConfig, writeSheet);
|
||||
excelWriter.fill(new FillWrapper("data2", data()), writeSheet);
|
||||
@ -205,21 +211,26 @@ public class FillTempTest {
|
||||
excelWriter.fill(new FillWrapper("data3", data()), writeSheet);
|
||||
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
//map.put("date", "2019年10月9日13:28:28");
|
||||
//map.put("date", "2019-10-09 13:28:28");
|
||||
map.put("date", new Date());
|
||||
|
||||
excelWriter.fill(map, writeSheet);
|
||||
|
||||
// 别忘记关闭流
|
||||
// Do not forget to close the stream
|
||||
excelWriter.finish();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates sample data for filling.
|
||||
*
|
||||
* @return A list of FillData objects.
|
||||
*/
|
||||
private List<FillData> data() {
|
||||
List<FillData> list = new ArrayList<FillData>();
|
||||
for (int i = 0; i < 10; i++) {
|
||||
FillData fillData = new FillData();
|
||||
list.add(fillData);
|
||||
fillData.setName("张三");
|
||||
fillData.setName("Zhang San");
|
||||
fillData.setNumber(5.2);
|
||||
}
|
||||
return list;
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -1,2 +1,2 @@
|
||||
姓名,数字,复杂,忽略,空
|
||||
{name},{number},{name}今年{number}岁了,\{name\}忽略,{name},空{.empty}
|
||||
Name,Number,Complex,Ignored,Empty
|
||||
{name},{number},{name} is {number} years old this year,\{name\} ignored, {name},Empty{.empty}
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user