easyexcelアノテーションベースのコールバックベースのより細かいセルフォーマット(1)

   でeasyexcelで。各列の形式の正確な制御を実現するために、カスタム注釈を通じて、セルの書式を設定するには、あなたがでデータを作成したい場合は、この記事では、我々は注釈を介して各列のセルフォーマットの正確な制御を達成しているが、同じコラム、できます異なるスタイルがあり、それを達成することはできません。たとえば、同じ列の値の場合、値は赤いフォントを使用する必要がある場合もあれば、デフォルトのフォントを使用する必要がある場合もあります。この場合、達成するためにコールバックを追加できます。

 

   以下のコードとeasyexcelはセルをフォーマットし、カスタムアノテーションを介して各列のフォーマットの正確な制御を実現します。記事の違いは、コンストラクターで渡され、次にTheで渡される必要がある2つのコンシューマーがあることです。 2人のコンシューマーはそれぞれ以下のコード1と2で呼び出されます

@Slf4j
public class ExcelStyleAnnotationAndContainsCallBackCellWriteHandler extends HorizontalCellStyleStrategy {

    private Class<?> c;
    private ThreeConsumer<Font,Cell,Field> fontConsumer;
    private ThreeConsumer<CellStyle,Cell,Field> cellStyleConsumer;
    public ExcelStyleAnnotationAndContainsCallBackCellWriteHandler(Class<?> c, WriteCellStyle headWriteCellStyle,
                                                                   WriteCellStyle contentWriteCellStyle,
                                                                   ThreeConsumer<Font,Cell,Field> fontConsumer,
                                                                   ThreeConsumer<CellStyle,Cell,Field> cellStyleConsumer) {
        super(headWriteCellStyle, contentWriteCellStyle);
        this.c = c;
        this.fontConsumer = fontConsumer;
        this.cellStyleConsumer = cellStyleConsumer;
    }

    @Override
    protected void setContentCellStyle(Cell cell, Head head, Integer relativeRowIndex) {
        try {
            Field declaredField = c.getDeclaredField(head.getFieldName());
            ExcelStyle annotation = declaredField.getAnnotation(ExcelStyle.class);
            if (annotation != null) {
                Workbook wb = cell.getSheet().getWorkbook();
                CellStyle cellStyle = wb.createCellStyle();
                Font font = wb.createFont();
                font.setFontName(annotation.fontName());
                font.setFontHeightInPoints(annotation.fontHeightInPoints());
                //1
                if (fontConsumer != null) {
                    fontConsumer.accept(font,cell,declaredField);
                }
                cellStyle.setFont(font);
                cellStyle.setAlignment(annotation.horizontalAlignment());
                cellStyle.setVerticalAlignment(annotation.verticalAlignment());
                cellStyle.setWrapText(annotation.wrapText());
                //2
                if (cellStyleConsumer != null) {
                    cellStyleConsumer.accept(cellStyle,cell,declaredField);
                }
                cell.setCellStyle(cellStyle);
            }else {
                super.setContentCellStyle(cell,head,relativeRowIndex);
            }
        } catch (NoSuchFieldException e) {
            log.error("ExcelStyleAnnotationCellWriteHandler error{0}",e);
        }

    }
}
@FunctionalInterface
public interface ThreeConsumer<T, U, V>  {
    void accept(T t, U u,V v);
}

       

        FontConsumerの例:フィールドがpercentOfExpend(支出の割合)またはpercentOfIncome(収入の割合)の場合、値が0未満の場合、赤いフォントで設定されます。

    public static void fontConsumer(Font font, Cell cell, Field field){
        if(isPercentPropertyAndLtZero(cell,field)){
            String stringCellValue = cell.getStringCellValue();
            BigDecimal value = new BigDecimal(stringCellValue);

            BigDecimal zero = new BigDecimal("0");
            if (value.compareTo(zero)  < 0) {
                font.setColor(Font.COLOR_RED);
            }
        }
    }

   private static boolean isPercentPropertyAndLtZero(Cell cell, Field field){
        return StringUtils.isNotBlank(cell.getStringCellValue()) && ("percentOfExpend".equals(field.getName()) || "percentOfIncome".equals(field.getName()));
    }

        転送:

       EasyExcelFactory.write(httpServletResponse.getOutputStream(), XX.class)
                .registerWriteHandler(EasyExcelUtil.getDefaultStyleForCustomAnnotationAndContainsCallBack(XX.class,XX::fontConsumer,null))
                .sheet("xxxxx").doWrite(list);


    public static ExcelStyleAnnotationAndContainsCallBackCellWriteHandler getDefaultStyleForCustomAnnotationAndContainsCallBack(Class<?> c,
                                                                                                                                ThreeConsumer<Font, Cell, Field> fontConsumer,
                                                                                                                                ThreeConsumer<CellStyle,Cell,Field> cellStyleConsumer) {
        return new ExcelStyleAnnotationAndContainsCallBackCellWriteHandler(c,getHeadWriteCellStyle(), getContentWriteCellStyle(),fontConsumer,cellStyleConsumer);
    }


    private static WriteCellStyle getHeadWriteCellStyle(){
        // 头的策略
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontHeightInPoints((short)14);
        headWriteFont.setFontName("宋体");
        headWriteCellStyle.setWriteFont(headWriteFont);
        return headWriteCellStyle;
    }
    private static WriteCellStyle getContentWriteCellStyle(){
        // 内容的策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        WriteFont contentWriteFont = new WriteFont();
        contentWriteFont.setFontName("宋体");
        contentWriteFont.setFontHeightInPoints((short)13);
        contentWriteCellStyle.setWriteFont(contentWriteFont);
        return contentWriteCellStyle;
    }

 

おすすめ

転載: blog.csdn.net/sinat_33472737/article/details/109174405