Search in sources :

Example 11 with FillConfig

use of com.alibaba.excel.write.metadata.fill.FillConfig in project easyexcel by alibaba.

the class FillDataTest method compositeFill.

private void compositeFill(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(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");
        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);
    Assert.assertEquals("张三", map0.get(21));
    Map<String, String> map27 = (Map<String, String>) list.get(27);
    Assert.assertEquals("张三", map27.get(0));
    Map<String, String> map29 = (Map<String, String>) list.get(29);
    Assert.assertEquals("张三", map29.get(3));
}
Also used : HashMap(java.util.HashMap) ExcelWriter(com.alibaba.excel.ExcelWriter) FillWrapper(com.alibaba.excel.write.metadata.fill.FillWrapper) WriteSheet(com.alibaba.excel.write.metadata.WriteSheet) HashMap(java.util.HashMap) Map(java.util.Map) FillConfig(com.alibaba.excel.write.metadata.fill.FillConfig)

Example 12 with FillConfig

use of com.alibaba.excel.write.metadata.fill.FillConfig in project easyexcel by alibaba.

the class FillDataTest method complexFill.

private void complexFill(File file, File template) {
    try (ExcelWriter excelWriter = EasyExcel.write(file).withTemplate(template).build()) {
        WriteSheet writeSheet = EasyExcel.writerSheet().registerWriteHandler(new LoopMergeStrategy(2, 0)).build();
        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("total", 1000);
        excelWriter.fill(map, writeSheet);
    }
    List<Object> list = EasyExcel.read(file).sheet().headRowNumber(3).doReadSync();
    Assert.assertEquals(list.size(), 21L);
    Map<String, String> map19 = (Map<String, String>) list.get(19);
    Assert.assertEquals("张三", map19.get(0));
}
Also used : LoopMergeStrategy(com.alibaba.excel.write.merge.LoopMergeStrategy) HashMap(java.util.HashMap) ExcelWriter(com.alibaba.excel.ExcelWriter) WriteSheet(com.alibaba.excel.write.metadata.WriteSheet) HashMap(java.util.HashMap) Map(java.util.Map) FillConfig(com.alibaba.excel.write.metadata.fill.FillConfig)

Example 13 with FillConfig

use of com.alibaba.excel.write.metadata.fill.FillConfig in project easyexcel by alibaba.

the class FillTest method horizontalFill.

/**
 * 横向的填充
 *
 * @since 2.1.1
 */
@Test
public void horizontalFill() {
    // 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
    // {} 代表普通变量 {.} 代表是list的变量
    String templateFileName = TestFileUtil.getPath() + "demo" + File.separator + "fill" + File.separator + "horizontal.xlsx";
    String fileName = TestFileUtil.getPath() + "horizontalFill" + 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();
        excelWriter.fill(data(), fillConfig, writeSheet);
        excelWriter.fill(data(), fillConfig, writeSheet);
        Map<String, Object> map = new HashMap<>();
        map.put("date", "2019年10月9日13:28:28");
        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();
        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);
    } finally {
        // 千万别忘记close 会帮忙关闭流
        if (excelWriter != null) {
            excelWriter.close();
        }
    }
}
Also used : HashMap(java.util.HashMap) ExcelWriter(com.alibaba.excel.ExcelWriter) WriteSheet(com.alibaba.excel.write.metadata.WriteSheet) FillConfig(com.alibaba.excel.write.metadata.fill.FillConfig) Test(org.junit.Test)

Aggregations

FillConfig (com.alibaba.excel.write.metadata.fill.FillConfig)13 WriteSheet (com.alibaba.excel.write.metadata.WriteSheet)12 ExcelWriter (com.alibaba.excel.ExcelWriter)11 HashMap (java.util.HashMap)10 Test (org.junit.Test)7 FillWrapper (com.alibaba.excel.write.metadata.fill.FillWrapper)5 Map (java.util.Map)4 Date (java.util.Date)2 WriteContext (com.alibaba.excel.context.WriteContext)1 CellDataTypeEnum (com.alibaba.excel.enums.CellDataTypeEnum)1 WriteDirectionEnum (com.alibaba.excel.enums.WriteDirectionEnum)1 WriteTemplateAnalysisCellTypeEnum (com.alibaba.excel.enums.WriteTemplateAnalysisCellTypeEnum)1 ExcelGenerateException (com.alibaba.excel.exception.ExcelGenerateException)1 WriteCellData (com.alibaba.excel.metadata.data.WriteCellData)1 ExcelContentProperty (com.alibaba.excel.metadata.property.ExcelContentProperty)1 BeanMapUtils (com.alibaba.excel.util.BeanMapUtils)1 ClassUtils (com.alibaba.excel.util.ClassUtils)1 FieldUtils (com.alibaba.excel.util.FieldUtils)1 ListUtils (com.alibaba.excel.util.ListUtils)1 MapUtils (com.alibaba.excel.util.MapUtils)1