add template support

This commit is contained in:
Looly
2024-08-24 22:17:05 +08:00
parent 01e81de806
commit e8ceffa98d
9 changed files with 440 additions and 39 deletions

View File

@@ -143,7 +143,7 @@ public class ExcelWriteTest {
writer.writeRow(row2);
// 生成文件或导出Excel
writer.flush(FileUtil.file("d:/test/writeWithSheetTest.xlsx"));
writer.flush(FileUtil.file("d:/test/writeWithSheetTest.xlsx"), true);
writer.close();
}
@@ -868,7 +868,7 @@ public class ExcelWriteTest {
writer.writeImg(file, 0, 0, 5, 10);
writer.flush(new File("C:\\Users\\zsz\\Desktop\\2.xlsx"));
writer.flush(new File("C:\\Users\\zsz\\Desktop\\2.xlsx"), true);
writer.close();
}

View File

@@ -22,6 +22,6 @@ public class TemplateContextTest {
final ExcelWriter writer = ExcelUtil.getWriter("template.xlsx");
final TemplateContext templateContext = new TemplateContext(writer.getSheet());
Assertions.assertNotNull(templateContext.getCell("date"));
Assertions.assertNotNull(templateContext.getCell(".month"));
Assertions.assertNotNull(templateContext.getCell("month"));
}
}

View File

@@ -0,0 +1,34 @@
package org.dromara.hutool.poi.excel.writer;
import org.dromara.hutool.core.io.file.FileUtil;
import org.dromara.hutool.core.map.MapUtil;
import org.dromara.hutool.poi.excel.ExcelUtil;
import org.junit.jupiter.api.Test;
public class TemplateWriterTest {
@Test
void writeRowTest() {
final ExcelWriter writer = ExcelUtil.getWriter("d:/test/template.xlsx");
// 单个替换的变量
writer.fillRow(MapUtil
.builder("date", (Object)"2024-01-01")
.build());
// 列表替换
for (int i = 0; i < 10; i++) {
writer.fillRow(MapUtil
.builder("user.name", (Object)"张三")
.put("user.age", 18)
.put("year", 2024)
.put("month", 8)
.put("day", 24)
.put("day", 24)
.put("user.area123", "某某市")
.put("invalid", "不替换")
.build());
}
writer.flush(FileUtil.file("d:/test/templateResult.xlsx"), true);
}
}