use of com.alibaba.excel.ExcelWriter in project easyexcel by alibaba.
the class ExcelWriterBuilder method sheet.
public ExcelWriterSheetBuilder sheet(Integer sheetNo, String sheetName) {
ExcelWriter excelWriter = build();
ExcelWriterSheetBuilder excelWriterSheetBuilder = new ExcelWriterSheetBuilder(excelWriter);
if (sheetNo != null) {
excelWriterSheetBuilder.sheetNo(sheetNo);
}
if (sheetName != null) {
excelWriterSheetBuilder.sheetName(sheetName);
}
return excelWriterSheetBuilder;
}
use of com.alibaba.excel.ExcelWriter in project easyexcel by alibaba.
the class FillDataTest method horizontalFill.
private void horizontalFill(File file, File template) {
try (ExcelWriter excelWriter = EasyExcel.write(file).withTemplate(template).build()) {
WriteSheet writeSheet = EasyExcel.writerSheet().build();
FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).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");
excelWriter.fill(map, writeSheet);
excelWriter.finish();
}
List<Object> list = EasyExcel.read(file).sheet().headRowNumber(0).doReadSync();
Assert.assertEquals(list.size(), 5L);
Map<String, String> map0 = (Map<String, String>) list.get(0);
Assert.assertEquals("张三", map0.get(2));
}
use of com.alibaba.excel.ExcelWriter in project easyexcel by alibaba.
the class LargeDataTest method t02Fill.
@Test
public void t02Fill() {
try (ExcelWriter excelWriter = EasyExcel.write(fileFill07).withTemplate(template07).build()) {
WriteSheet writeSheet = EasyExcel.writerSheet().build();
for (int j = 0; j < 5000; j++) {
excelWriter.fill(data(), writeSheet);
LOGGER.info("{} fill success.", j);
}
}
}
use of com.alibaba.excel.ExcelWriter in project easyexcel by alibaba.
the class LargeDataTest method t03ReadAndWriteCsv.
@Test
public void t03ReadAndWriteCsv() {
// write
long start = System.currentTimeMillis();
try (ExcelWriter excelWriter = EasyExcel.write(fileCsv).build()) {
WriteSheet writeSheet = EasyExcel.writerSheet().build();
for (int j = 0; j < 5000; j++) {
excelWriter.write(data(), writeSheet);
LOGGER.info("{} write success.", j);
}
}
LOGGER.info("CSV large data total time spent:{}", System.currentTimeMillis() - start);
// read
start = System.currentTimeMillis();
EasyExcel.read(fileCsv, LargeData.class, new LargeDataListener()).sheet().doRead();
LOGGER.info("CSV large data total time spent:{}", System.currentTimeMillis() - start);
}
use of com.alibaba.excel.ExcelWriter in project easyexcel by alibaba.
the class FillTest method compositeFill.
/**
* 多列表组合填充填充
*
* @since 2.2.0-beta1
*/
@Test
public void compositeFill() {
// 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
// {} 代表普通变量 {.} 代表是list的变量 {前缀.} 前缀可以区分不同的list
String templateFileName = TestFileUtil.getPath() + "demo" + File.separator + "fill" + File.separator + "composite.xlsx";
String fileName = TestFileUtil.getPath() + "compositeFill" + System.currentTimeMillis() + ".xlsx";
// 方案1 : 使用 try-with-resources @since 3.1.0
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包裹
excelWriter.fill(new FillWrapper("data1", data()), fillConfig, writeSheet);
excelWriter.fill(new FillWrapper("data1", data()), fillConfig, writeSheet);
excelWriter.fill(new FillWrapper("data2", data()), writeSheet);
excelWriter.fill(new FillWrapper("data2", data()), writeSheet);
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", new Date());
excelWriter.fill(map, writeSheet);
}
// 方案2 : 不使用 try-with-resources
ExcelWriter excelWriter = null;
try {
excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build();
WriteSheet writeSheet = EasyExcel.writerSheet().build();
FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build();
// 如果有多个list 模板上必须有{前缀.} 这里的前缀就是 data1,然后多个list必须用 FillWrapper包裹
excelWriter.fill(new FillWrapper("data1", data()), fillConfig, writeSheet);
excelWriter.fill(new FillWrapper("data1", data()), fillConfig, writeSheet);
excelWriter.fill(new FillWrapper("data2", data()), writeSheet);
excelWriter.fill(new FillWrapper("data2", data()), writeSheet);
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", new Date());
excelWriter.fill(map, writeSheet);
} finally {
// 千万别忘记close 会帮忙关闭流
if (excelWriter != null) {
excelWriter.close();
}
}
}
Aggregations