Search in sources :

Example 1 with FillWrapper

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

the class FillTempTest 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";
    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);
    // 别忘记关闭流
    excelWriter.finish();
}
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) Date(java.util.Date) FillConfig(com.alibaba.excel.write.metadata.fill.FillConfig) Test(org.junit.Test)

Example 2 with FillWrapper

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

the class ExcelWriteFillExecutor method fill.

public void fill(Object data, FillConfig fillConfig) {
    if (data == null) {
        data = new HashMap<String, Object>(16);
    }
    if (fillConfig == null) {
        fillConfig = FillConfig.builder().build();
    }
    fillConfig.init();
    Object realData;
    // The data prefix that is populated this time
    String currentDataPrefix;
    if (data instanceof FillWrapper) {
        FillWrapper fillWrapper = (FillWrapper) data;
        currentDataPrefix = fillWrapper.getName();
        realData = fillWrapper.getCollectionData();
    } else {
        realData = data;
        currentDataPrefix = null;
    }
    currentUniqueDataFlag = uniqueDataFlag(writeContext.writeSheetHolder(), currentDataPrefix);
    // processing data
    if (realData instanceof Collection) {
        List<AnalysisCell> analysisCellList = readTemplateData(templateCollectionAnalysisCache);
        Collection<?> collectionData = (Collection<?>) realData;
        if (CollectionUtils.isEmpty(collectionData)) {
            return;
        }
        Iterator<?> iterator = collectionData.iterator();
        if (WriteDirectionEnum.VERTICAL.equals(fillConfig.getDirection()) && fillConfig.getForceNewRow()) {
            shiftRows(collectionData.size(), analysisCellList);
        }
        while (iterator.hasNext()) {
            doFill(analysisCellList, iterator.next(), fillConfig, getRelativeRowIndex());
        }
    } else {
        doFill(readTemplateData(templateAnalysisCache), realData, fillConfig, null);
    }
}
Also used : AnalysisCell(com.alibaba.excel.write.metadata.fill.AnalysisCell) FillWrapper(com.alibaba.excel.write.metadata.fill.FillWrapper) Collection(java.util.Collection)

Example 3 with FillWrapper

use of com.alibaba.excel.write.metadata.fill.FillWrapper 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();
        }
    }
}
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) Date(java.util.Date) FillConfig(com.alibaba.excel.write.metadata.fill.FillConfig) Test(org.junit.Test)

Example 4 with FillWrapper

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

the class FillTest method TestFillNullPoint.

@Test
public void TestFillNullPoint() {
    String templateFileName = TestFileUtil.getPath() + "temp/issue1663" + File.separator + "template.xlsx";
    String fileName = TestFileUtil.getPath() + "temp/issue1663" + File.separator + "issue1663.xlsx";
    ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build();
    WriteSheet writeSheet = EasyExcel.writerSheet().build();
    FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.VERTICAL).build();
    excelWriter.fill(new FillWrapper("data1", data()), fillConfig, writeSheet);
    Map<String, Object> map = new HashMap<String, Object>();
    // Variable {date} does not exist in the template.xlsx, which should be ignored instead of reporting an error.
    map.put("date", "2019年10月9日13:28:28");
    excelWriter.fill(map, writeSheet);
    excelWriter.finish();
}
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) FillConfig(com.alibaba.excel.write.metadata.fill.FillConfig) Test(org.junit.Test)

Example 5 with FillWrapper

use of com.alibaba.excel.write.metadata.fill.FillWrapper 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)

Aggregations

FillWrapper (com.alibaba.excel.write.metadata.fill.FillWrapper)5 ExcelWriter (com.alibaba.excel.ExcelWriter)4 WriteSheet (com.alibaba.excel.write.metadata.WriteSheet)4 FillConfig (com.alibaba.excel.write.metadata.fill.FillConfig)4 HashMap (java.util.HashMap)4 Test (org.junit.Test)3 Date (java.util.Date)2 AnalysisCell (com.alibaba.excel.write.metadata.fill.AnalysisCell)1 Collection (java.util.Collection)1 Map (java.util.Map)1