Search in sources :

Example 1 with FillConfig

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

the class FillTempTest method complexFill.

/**
 * 复杂的填充
 *
 * @since 2.1.1
 */
@Test
public void complexFill() {
    // 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
    // {} 代表普通变量 {.} 代表是list的变量
    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不是最后一行 参照下一个
    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);
    excelWriter.finish();
}
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)

Example 2 with FillConfig

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

the class FillTempTest 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";
    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<String, Object>();
    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) WriteSheet(com.alibaba.excel.write.metadata.WriteSheet) FillConfig(com.alibaba.excel.write.metadata.fill.FillConfig) Test(org.junit.Test)

Example 3 with FillConfig

use of com.alibaba.excel.write.metadata.fill.FillConfig 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 4 with FillConfig

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

the class ExcelWriteFillExecutor method doFill.

private void doFill(List<AnalysisCell> analysisCellList, Object oneRowData, FillConfig fillConfig, Integer relativeRowIndex) {
    if (CollectionUtils.isEmpty(analysisCellList) || oneRowData == null) {
        return;
    }
    Map dataMap;
    if (oneRowData instanceof Map) {
        dataMap = (Map) oneRowData;
    } else {
        dataMap = BeanMapUtils.create(oneRowData);
    }
    Set<String> dataKeySet = new HashSet<>(dataMap.keySet());
    RowWriteHandlerContext rowWriteHandlerContext = WriteHandlerUtils.createRowWriteHandlerContext(writeContext, null, relativeRowIndex, Boolean.FALSE);
    for (AnalysisCell analysisCell : analysisCellList) {
        CellWriteHandlerContext cellWriteHandlerContext = WriteHandlerUtils.createCellWriteHandlerContext(writeContext, null, analysisCell.getRowIndex(), null, analysisCell.getColumnIndex(), relativeRowIndex, Boolean.FALSE, ExcelContentProperty.EMPTY);
        if (analysisCell.getOnlyOneVariable()) {
            String variable = analysisCell.getVariableList().get(0);
            if (!dataKeySet.contains(variable)) {
                continue;
            }
            Object value = dataMap.get(variable);
            ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(dataMap, writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadClazz(), variable);
            cellWriteHandlerContext.setExcelContentProperty(excelContentProperty);
            createCell(analysisCell, fillConfig, cellWriteHandlerContext, rowWriteHandlerContext);
            cellWriteHandlerContext.setOriginalValue(value);
            cellWriteHandlerContext.setOriginalFieldClass(FieldUtils.getFieldClass(dataMap, variable, value));
            converterAndSet(cellWriteHandlerContext);
            WriteCellData<?> cellData = cellWriteHandlerContext.getFirstCellData();
            // Restyle
            if (fillConfig.getAutoStyle()) {
                Optional.ofNullable(collectionFieldStyleCache.get(currentUniqueDataFlag)).map(collectionFieldStyleMap -> collectionFieldStyleMap.get(analysisCell)).ifPresent(cellData::setOriginCellStyle);
            }
        } else {
            StringBuilder cellValueBuild = new StringBuilder();
            int index = 0;
            List<WriteCellData<?>> cellDataList = new ArrayList<>();
            cellWriteHandlerContext.setExcelContentProperty(ExcelContentProperty.EMPTY);
            cellWriteHandlerContext.setIgnoreFillStyle(Boolean.TRUE);
            createCell(analysisCell, fillConfig, cellWriteHandlerContext, rowWriteHandlerContext);
            Cell cell = cellWriteHandlerContext.getCell();
            for (String variable : analysisCell.getVariableList()) {
                cellValueBuild.append(analysisCell.getPrepareDataList().get(index++));
                if (!dataKeySet.contains(variable)) {
                    continue;
                }
                Object value = dataMap.get(variable);
                ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(dataMap, writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadClazz(), variable);
                cellWriteHandlerContext.setOriginalValue(value);
                cellWriteHandlerContext.setOriginalFieldClass(FieldUtils.getFieldClass(dataMap, variable, value));
                cellWriteHandlerContext.setExcelContentProperty(excelContentProperty);
                cellWriteHandlerContext.setTargetCellDataType(CellDataTypeEnum.STRING);
                WriteCellData<?> cellData = convert(cellWriteHandlerContext);
                cellDataList.add(cellData);
                CellDataTypeEnum type = cellData.getType();
                if (type != null) {
                    switch(type) {
                        case STRING:
                            cellValueBuild.append(cellData.getStringValue());
                            break;
                        case BOOLEAN:
                            cellValueBuild.append(cellData.getBooleanValue());
                            break;
                        case NUMBER:
                            cellValueBuild.append(cellData.getNumberValue());
                            break;
                        default:
                            break;
                    }
                }
            }
            cellValueBuild.append(analysisCell.getPrepareDataList().get(index));
            cell.setCellValue(cellValueBuild.toString());
            cellWriteHandlerContext.setCellDataList(cellDataList);
            if (CollectionUtils.isNotEmpty(cellDataList)) {
                cellWriteHandlerContext.setFirstCellData(cellDataList.get(0));
            }
            // Restyle
            if (fillConfig.getAutoStyle()) {
                Optional.ofNullable(collectionFieldStyleCache.get(currentUniqueDataFlag)).map(collectionFieldStyleMap -> collectionFieldStyleMap.get(analysisCell)).ifPresent(cell::setCellStyle);
            }
        }
        WriteHandlerUtils.afterCellDispose(cellWriteHandlerContext);
    }
    // In the case of the fill line may be called many times
    if (rowWriteHandlerContext.getRow() != null) {
        WriteHandlerUtils.afterRowDispose(rowWriteHandlerContext);
    }
}
Also used : CellType(org.apache.poi.ss.usermodel.CellType) Setter(lombok.Setter) CellDataTypeEnum(com.alibaba.excel.enums.CellDataTypeEnum) Getter(lombok.Getter) BeanMapUtils(com.alibaba.excel.util.BeanMapUtils) ClassUtils(com.alibaba.excel.util.ClassUtils) HashMap(java.util.HashMap) WriteDirectionEnum(com.alibaba.excel.enums.WriteDirectionEnum) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) ExcelContentProperty(com.alibaba.excel.metadata.property.ExcelContentProperty) HashSet(java.util.HashSet) ListUtils(com.alibaba.excel.util.ListUtils) WriteCellData(com.alibaba.excel.metadata.data.WriteCellData) WriteTemplateAnalysisCellTypeEnum(com.alibaba.excel.enums.WriteTemplateAnalysisCellTypeEnum) AnalysisCell(com.alibaba.excel.write.metadata.fill.AnalysisCell) StringUtils(com.alibaba.excel.util.StringUtils) Map(java.util.Map) Cell(org.apache.poi.ss.usermodel.Cell) FillWrapper(com.alibaba.excel.write.metadata.fill.FillWrapper) Sheet(org.apache.poi.ss.usermodel.Sheet) Iterator(java.util.Iterator) WriteHandlerUtils(com.alibaba.excel.util.WriteHandlerUtils) Collection(java.util.Collection) Set(java.util.Set) EqualsAndHashCode(lombok.EqualsAndHashCode) Objects(java.util.Objects) List(java.util.List) WriteContext(com.alibaba.excel.context.WriteContext) ExcelGenerateException(com.alibaba.excel.exception.ExcelGenerateException) FieldUtils(com.alibaba.excel.util.FieldUtils) MapUtils(com.alibaba.excel.util.MapUtils) RowWriteHandlerContext(com.alibaba.excel.write.handler.context.RowWriteHandlerContext) FillConfig(com.alibaba.excel.write.metadata.fill.FillConfig) CellWriteHandlerContext(com.alibaba.excel.write.handler.context.CellWriteHandlerContext) PoiUtils(org.apache.poi.hssf.usermodel.PoiUtils) Optional(java.util.Optional) Row(org.apache.poi.ss.usermodel.Row) AllArgsConstructor(lombok.AllArgsConstructor) CellStyle(org.apache.poi.ss.usermodel.CellStyle) WriteSheetHolder(com.alibaba.excel.write.metadata.holder.WriteSheetHolder) CellDataTypeEnum(com.alibaba.excel.enums.CellDataTypeEnum) CellWriteHandlerContext(com.alibaba.excel.write.handler.context.CellWriteHandlerContext) ArrayList(java.util.ArrayList) AnalysisCell(com.alibaba.excel.write.metadata.fill.AnalysisCell) RowWriteHandlerContext(com.alibaba.excel.write.handler.context.RowWriteHandlerContext) WriteCellData(com.alibaba.excel.metadata.data.WriteCellData) ExcelContentProperty(com.alibaba.excel.metadata.property.ExcelContentProperty) HashMap(java.util.HashMap) Map(java.util.Map) AnalysisCell(com.alibaba.excel.write.metadata.fill.AnalysisCell) Cell(org.apache.poi.ss.usermodel.Cell) HashSet(java.util.HashSet)

Example 5 with FillConfig

use of com.alibaba.excel.write.metadata.fill.FillConfig 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));
}
Also used : 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)

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