簡體   English   中英

設置背景自定義顏色,XSSFWorkbook

[英]Setting background custom color, XSSFWorkbook

我使用此代碼將excel中的字體更改為我定義的顏色

        Color sColor = new Color (value,0,0);
        XSSFColor userColor = new XSSFColor(sColor);

        CellStyle style = wb.createCellStyle();
        XSSFFont font = wb.createFont();

        font.setColor(userColor);
        style.setFont(font);
        cell.setCellStyle(style);

我可以用相同的方式更改單元格的背景嗎?

我在這里看到了在Apache POI中設置背景自定義顏色不適用於XSSF的問題,並且使用了以下代碼:

        XSSFCellStyle cellStyle = wb.createCellStyle();
        XSSFColor color = new XSSFColor(new java.awt.Color(value, 0, 0));
        ((XSSFCellStyle)cellStyle).setFillBackgroundColor(color);

        cell.setCellStyle(cellStyle);

背景仍然總是白色。

確保代碼的所有其他部分都正確編寫,因為更改字體后它可以工作。

我的計算機上裝有Office 2010

謝謝所有我現在找到解決方案

        XSSFCellStyle cellStyle = wb.createCellStyle();

        XSSFColor color = new XSSFColor(new java.awt.Color(value, 0, 0));

        ((XSSFCellStyle)cellStyle).setFillBackgroundColor(color);
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cellStyle.setFillForegroundColor(color); 
        cell.setCellStyle(cellStyle);

創建單元格樣式對象:

CellStyle backgroundStyle = workbook.createCellStyle(); 

設置自定義顏色:

backgroundStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());

為單元格添加樣式:

cell.setCellStyle(backgroundStyle);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM