Home > Mobile >  why the easy excel set cell style did not work
why the easy excel set cell style did not work

Time:02-15

I am using easy excel api group: 'com.alibaba', name: 'easyexcel', version: '3.0.3' to set the cell color, this is the main.java code:

public class AppStarter {

    public static void main(String[] args) {
        SparkUserExcelRequest request = new SparkUserExcelRequest();
        request.setNickname("test");
        String writePathName = "/Users/dolphin/source/dabai/microservice/soa-zhuolian-org/";
        String fullPathName = writePathName    "a.xlsx";
        if (!new File(writePathName).exists()) {
            new File(writePathName).mkdirs();
        }
        ExcelWriterBuilder writerBuilder = EasyExcel.write(fullPathName, SparkUserExcelRequest.class);
        writerBuilder.excelType(ExcelTypeEnum.XLSX);
        writerBuilder.registerWriteHandler(new CellStyleWriteHandler());
        ExcelWriter writer = writerBuilder.build();
        WriteSheet writeSheet = new WriteSheet();
        writeSheet.setSheetName("mark");
        writer.write(Arrays.asList(request), writeSheet);
        writer.finish();
    }
}

this is the class which change the cell color:

public class CellStyleWriteHandler extends AbstractCellStyleStrategy {

    @Override
    protected void setContentCellStyle(Cell cell, Head head, Integer relativeRowIndex) {
        Workbook workbook = cell.getSheet().getWorkbook();
        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cellStyle.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());
        cell.setCellStyle(cellStyle);
    }
}

and this is the entity class:

@Data
@ExcelIgnoreUnannotated
public class SparkUserExcelRequest implements Serializable {

    @ExcelProperty("user name")
    private String nickname;
}

when I run this code, this cell color did not changed to light blue, why the cell color set failed? what should I do to fix this problem? I want the test cell color changed to blue.

enter image description here

CodePudding user response:

I tried your code, and the color did not change. Tried to downgrade your easyexcel version to 2.2.11 should fix this problem.You could add an issue to make sure is it a bug.

api group: 'com.alibaba', name: 'easyexcel', version: '2.2.11'
  • Related