Search in sources :

Example 11 with WriteCellData

use of com.alibaba.excel.metadata.data.WriteCellData in project RuoYi-Flowable-Plus by KonBAI-Q.

the class ExcelDictConvert method convertToExcelData.

@Override
public WriteCellData<String> convertToExcelData(Object object, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
    if (ObjectUtil.isNull(object)) {
        return new WriteCellData<>("");
    }
    ExcelDictFormat anno = getAnnotation(contentProperty.getField());
    String type = anno.dictType();
    String value = Convert.toStr(object);
    String label;
    if (StringUtils.isBlank(type)) {
        label = ExcelUtil.convertByExp(value, anno.readConverterExp(), anno.separator());
    } else {
        label = SpringUtils.getBean(DictService.class).getDictLabel(type, value, anno.separator());
    }
    return new WriteCellData<>(label);
}
Also used : WriteCellData(com.alibaba.excel.metadata.data.WriteCellData) ExcelDictFormat(com.ruoyi.common.annotation.ExcelDictFormat)

Example 12 with WriteCellData

use of com.alibaba.excel.metadata.data.WriteCellData in project easyexcel by alibaba.

the class WriteContextImpl method addOneRowOfHeadDataToExcel.

private void addOneRowOfHeadDataToExcel(Row row, Integer rowIndex, Map<Integer, Head> headMap, int relativeRowIndex) {
    for (Map.Entry<Integer, Head> entry : headMap.entrySet()) {
        Head head = entry.getValue();
        int columnIndex = entry.getKey();
        ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(null, currentWriteHolder.excelWriteHeadProperty().getHeadClazz(), head.getFieldName());
        CellWriteHandlerContext cellWriteHandlerContext = WriteHandlerUtils.createCellWriteHandlerContext(this, row, rowIndex, head, columnIndex, relativeRowIndex, Boolean.TRUE, excelContentProperty);
        WriteHandlerUtils.beforeCellCreate(cellWriteHandlerContext);
        Cell cell = row.createCell(columnIndex);
        cellWriteHandlerContext.setCell(cell);
        WriteHandlerUtils.afterCellCreate(cellWriteHandlerContext);
        WriteCellData<String> writeCellData = new WriteCellData<>(head.getHeadNameList().get(relativeRowIndex));
        cell.setCellValue(writeCellData.getStringValue());
        cellWriteHandlerContext.setCellDataList(ListUtils.newArrayList(writeCellData));
        cellWriteHandlerContext.setFirstCellData(writeCellData);
        WriteHandlerUtils.afterCellDispose(cellWriteHandlerContext);
    }
}
Also used : Head(com.alibaba.excel.metadata.Head) WriteCellData(com.alibaba.excel.metadata.data.WriteCellData) CellWriteHandlerContext(com.alibaba.excel.write.handler.context.CellWriteHandlerContext) ExcelContentProperty(com.alibaba.excel.metadata.property.ExcelContentProperty) Map(java.util.Map) Cell(org.apache.poi.ss.usermodel.Cell)

Example 13 with WriteCellData

use of com.alibaba.excel.metadata.data.WriteCellData in project easyexcel by alibaba.

the class CellDataDataTest method data.

private List<CellDataWriteData> data() throws Exception {
    List<CellDataWriteData> list = new ArrayList<>();
    CellDataWriteData cellDataData = new CellDataWriteData();
    cellDataData.setDate(new WriteCellData<>(DateUtils.parseDate("2020-01-01 01:01:01")));
    WriteCellData<Integer> integer1 = new WriteCellData<>();
    integer1.setType(CellDataTypeEnum.NUMBER);
    integer1.setNumberValue(BigDecimal.valueOf(2L));
    cellDataData.setInteger1(integer1);
    cellDataData.setInteger2(2);
    WriteCellData<?> formulaValue = new WriteCellData<>();
    FormulaData formulaData = new FormulaData();
    formulaValue.setFormulaData(formulaData);
    formulaData.setFormulaValue("B2+C2");
    cellDataData.setFormulaValue(formulaValue);
    list.add(cellDataData);
    return list;
}
Also used : WriteCellData(com.alibaba.excel.metadata.data.WriteCellData) FormulaData(com.alibaba.excel.metadata.data.FormulaData) ArrayList(java.util.ArrayList)

Example 14 with WriteCellData

use of com.alibaba.excel.metadata.data.WriteCellData in project easyexcel by alibaba.

the class WriteTest method handlerStyleWrite.

/**
 * 拦截器形式自定义样式
 * <p>
 * 1. 创建excel对应的实体对象 参照{@link DemoData}
 * <p>
 * 2. 创建一个style策略 并注册
 * <p>
 * 3. 直接写即可
 */
@Test
public void handlerStyleWrite() {
    // 方法1 使用已有的策略 推荐
    // HorizontalCellStyleStrategy 每一行的样式都一样 或者隔行一样
    // AbstractVerticalCellStyleStrategy 每一列的样式都一样 需要自己回调每一页
    String fileName = TestFileUtil.getPath() + "handlerStyleWrite" + System.currentTimeMillis() + ".xlsx";
    // 头的策略
    WriteCellStyle headWriteCellStyle = new WriteCellStyle();
    // 背景设置为红色
    headWriteCellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
    WriteFont headWriteFont = new WriteFont();
    headWriteFont.setFontHeightInPoints((short) 20);
    headWriteCellStyle.setWriteFont(headWriteFont);
    // 内容的策略
    WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
    // 这里需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND 不然无法显示背景颜色.头默认了 FillPatternType所以可以不指定
    contentWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
    // 背景绿色
    contentWriteCellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());
    WriteFont contentWriteFont = new WriteFont();
    // 字体大小
    contentWriteFont.setFontHeightInPoints((short) 20);
    contentWriteCellStyle.setWriteFont(contentWriteFont);
    // 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
    HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
    // 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
    EasyExcel.write(fileName, DemoData.class).registerWriteHandler(horizontalCellStyleStrategy).sheet("模板").doWrite(data());
    // 方法2: 使用easyexcel的方式完全自己写 不太推荐 尽量使用已有策略
    // @since 3.0.0-beta2
    fileName = TestFileUtil.getPath() + "handlerStyleWrite" + System.currentTimeMillis() + ".xlsx";
    EasyExcel.write(fileName, DemoData.class).registerWriteHandler(new CellWriteHandler() {

        @Override
        public void afterCellDispose(CellWriteHandlerContext context) {
            // 判断不是头的情况 如果是fill 的情况 这里会==null 所以用not true
            if (BooleanUtils.isNotTrue(context.getHead())) {
                // 第一个单元格
                // 只要不是头 一定会有数据 当然fill的情况 可能要context.getCellDataList() ,这个需要看模板,因为一个单元格会有多个 WriteCellData
                WriteCellData<?> cellData = context.getFirstCellData();
                // 这里需要去cellData 获取样式
                // 很重要的一个原因是 WriteCellStyle 和 dataFormatData绑定的 简单的说 比如你加了 DateTimeFormat
                // ,已经将writeCellStyle里面的dataFormatData 改了 如果你自己new了一个WriteCellStyle,可能注解的样式就失效了
                // 然后 getOrCreateStyle 用于返回一个样式,如果为空,则创建一个后返回
                WriteCellStyle writeCellStyle = cellData.getOrCreateStyle();
                writeCellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
                // 这里需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND
                writeCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
            // 这样样式就设置好了 后面有个FillStyleCellWriteHandler 默认会将 WriteCellStyle 设置到 cell里面去 所以可以不用管了
            }
        }
    }).sheet("模板").doWrite(data());
    // 方法3: 使用poi的样式完全自己写 不推荐
    // @since 3.0.0-beta2
    // 坑1:style里面有dataformat 用来格式化数据的 所以自己设置可能导致格式化注解不生效
    // 坑2:不要一直去创建style 记得缓存起来 最多创建6W个就挂了
    fileName = TestFileUtil.getPath() + "handlerStyleWrite" + System.currentTimeMillis() + ".xlsx";
    EasyExcel.write(fileName, DemoData.class).registerWriteHandler(new CellWriteHandler() {

        @Override
        public void afterCellDispose(CellWriteHandlerContext context) {
            // 判断不是头的情况 如果是fill 的情况 这里会==null 所以用not true
            if (BooleanUtils.isNotTrue(context.getHead())) {
                Cell cell = context.getCell();
                // 拿到poi的workbook
                Workbook workbook = context.getWriteWorkbookHolder().getWorkbook();
                // 这里千万记住 想办法能复用的地方把他缓存起来 一个表格最多创建6W个样式
                // 不同单元格尽量传同一个 cellStyle
                CellStyle cellStyle = workbook.createCellStyle();
                cellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
                // 这里需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND
                cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
                cell.setCellStyle(cellStyle);
                // 由于这里没有指定dataformat 最后展示的数据 格式可能会不太正确
                // 这里要把 WriteCellData的样式清空, 不然后面还有一个拦截器 FillStyleCellWriteHandler 默认会将 WriteCellStyle 设置到
                // cell里面去 会导致自己设置的不一样
                context.getFirstCellData().setWriteCellStyle(null);
            }
        }
    }).sheet("模板").doWrite(data());
}
Also used : WriteCellData(com.alibaba.excel.metadata.data.WriteCellData) CellWriteHandlerContext(com.alibaba.excel.write.handler.context.CellWriteHandlerContext) WriteCellStyle(com.alibaba.excel.write.metadata.style.WriteCellStyle) CellWriteHandler(com.alibaba.excel.write.handler.CellWriteHandler) WriteFont(com.alibaba.excel.write.metadata.style.WriteFont) WriteCellStyle(com.alibaba.excel.write.metadata.style.WriteCellStyle) CellStyle(org.apache.poi.ss.usermodel.CellStyle) Cell(org.apache.poi.ss.usermodel.Cell) Workbook(org.apache.poi.ss.usermodel.Workbook) HorizontalCellStyleStrategy(com.alibaba.excel.write.style.HorizontalCellStyleStrategy) Test(org.junit.Test)

Aggregations

WriteCellData (com.alibaba.excel.metadata.data.WriteCellData)14 ArrayList (java.util.ArrayList)4 ExcelContentProperty (com.alibaba.excel.metadata.property.ExcelContentProperty)3 CellWriteHandlerContext (com.alibaba.excel.write.handler.context.CellWriteHandlerContext)3 BigDecimal (java.math.BigDecimal)3 Test (org.junit.Test)3 FormulaData (com.alibaba.excel.metadata.data.FormulaData)2 WriteCellStyle (com.alibaba.excel.write.metadata.style.WriteCellStyle)2 WriteFont (com.alibaba.excel.write.metadata.style.WriteFont)2 ExcelDictFormat (com.ruoyi.common.annotation.ExcelDictFormat)2 InputStream (java.io.InputStream)2 Cell (org.apache.poi.ss.usermodel.Cell)2 WriteContext (com.alibaba.excel.context.WriteContext)1 NullableObjectConverter (com.alibaba.excel.converters.NullableObjectConverter)1 WriteConverterContext (com.alibaba.excel.converters.WriteConverterContext)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 ExcelWriteDataConvertException (com.alibaba.excel.exception.ExcelWriteDataConvertException)1