alibaba / easyexcel

快速、简洁、解决大文件内存溢出的java处理Excel工具
https://easyexcel.opensource.alibaba.com
Apache License 2.0
32.26k stars 7.51k forks source link

CellWriteHandler重写并使用后 仍显示0 usages,并无响应 #3454

Open 1059183326 opened 1 year ago

1059183326 commented 1 year ago
package com.ste.qr.stats.export;

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.write.handler.AbstractCellWriteHandler;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import org.apache.poi.ss.usermodel.*;

import java.text.NumberFormat;
import java.text.ParseException;
import java.util.List;

//设置表头、内容的行高
public class CustomCellWriteHandler implements CellWriteHandler {
    private static NumberFormat nf = NumberFormat.getPercentInstance();

    @Override
    public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead) {
        row.setHeight((short) 288);
    }

    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<WriteCellData<?>> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
        int cellIndex = cell.getColumnIndex();
        int columnWidth = 25;
//        WriteFont writeFont = new WriteFont();
        Workbook workbook = writeSheetHolder.getSheet().getWorkbook();
        CellStyle cellStyle = CellStyleUtil.cellStyle(workbook);
        writeSheetHolder.getSheet().setColumnWidth(cellIndex, columnWidth * 150);
//        标题颜色
        if (cell.getRowIndex() == 3) {
            cellStyle.setFillForegroundColor(IndexedColors.LIGHT_TURQUOISE.getIndex());
        }
        if (cell.getRowIndex() > 1 && cell.getRowIndex() < 4 && cell.getColumnIndex() > 23 && cell.getColumnIndex() < 26) {
            cellStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
        }
        if (cell.getRowIndex() > 0 && cell.getRowIndex() < 4 && cell.getColumnIndex() > 25 && cell.getColumnIndex() < 33) {
            cellStyle.setFillForegroundColor(IndexedColors.ROSE.getIndex());
        }
////        负数字体颜色
//        if (cell.getRowIndex() > 3 && cell.getColumnIndex() < 36) {
//            try {
//                if (nf.parse(cell.getStringCellValue()).doubleValue() < 0) {
//                    writeFont.setColor(IndexedColors.RED.getIndex());
//                    cellStyle.setFont(workbook.createFont());
//                    cell.setCellStyle(cellStyle);
//                }
//            } catch (ParseException e) {
//                throw new RuntimeException(e);
//            }
//        }
////        部组名行背景颜色设置
//        if (cell.getColumnIndex() == 1) {
//            if (nf.parse(cell.getStringCellValue().equals())){
//
//            }
//        }

    }
}

问题描述

重写方法后,用registerwritehandler运用,但仍显示 0 usages 没有被运用

ljluestc commented 5 months ago

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;

import java.util.ArrayList;
import java.util.List;

public class ExcelWriterExample {

    public static void main(String[] args) {
        // Your data list
        List<List<Object>> data = new ArrayList<>();
        // Initialize EasyExcel writer
        ExcelWriter excelWriter = EasyExcel.write("output.xlsx").build();
        // Initialize write sheet
        WriteSheet writeSheet = EasyExcel.writerSheet("Sheet1").build();

        // Register your custom cell write handler
        CustomCellWriteHandler customCellWriteHandler = new CustomCellWriteHandler();

        // Attach the custom cell write handler to the write sheet
        writeSheet.setCustomWriteHandler(customCellWriteHandler);

        // Write data to the excel file
        excelWriter.write(data, writeSheet);

        // Close the writer
        excelWriter.finish();
    }
}