簡體   English   中英

如何獲取單元格字體顏色

[英]How to get cell font color

我正在准備用於驗證 Excel 工作表內容的代碼。 某些條件下的某些字段用紅色字體填充,因此我需要有此信息進行驗證。

我試過cell.getCellStyle().getFontIndex() 我看到對於填充黑色的字段,它返回 1 個值,對於由紅色填充的字段,它返回 3,但是當我嘗試workbook.getFontAt(1).getColor() ,我在這兩種情況下都收到 0。

Font.getColor返回索引顏色的索引。 這適用於舊的二進制 Excel 文件格式*.xls ( HSSF ),因為所有顏色只能從調色板中獲取。 但新的 Office Open XML 文件格式*.xlsx ( XSSF ) 還提供直接存儲為RGB顏色,而不是來自調色板。 那么這些顏色將沒有索引。 所以Font.getColor然后返回0

我發現的最兼容的方法是從Font而不是索引中獲取org.apache.poi.ss.usermodel.Color 不幸的是,這只能在HSSFXSSF使用不同的方式XSSF 所以我們需要區分HSSFFontXSSFFont 然后我們可以得到HSSFColorXSSFColor

HSSFXSSF完整示例:

import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.xssf.usermodel.*;

public class ExcelGetFontColor {
    
 //method to get current font from cell
 //gets only font of whole cell not font of possible rich text content
 private static Font getFont(Cell cell) {
  Workbook workbook = cell.getSheet().getWorkbook();
  CellStyle style = cell.getCellStyle();
  return workbook.getFontAt(style.getFontIndex());
 }

 private static Color getFontColor(Font font, Workbook workbook) {
  Color color = null;
  if (font instanceof XSSFFont) {
   XSSFFont xssfFont = (XSSFFont) font;
   color = xssfFont.getXSSFColor();
  } else if (font instanceof HSSFFont) {
   HSSFFont hssfFont = (HSSFFont) font;
   color = hssfFont.getHSSFColor((HSSFWorkbook)workbook);
  }
  return color;
 }
 
 private static String getHexString(Color color) {
  String result = null;
  if (color instanceof XSSFColor) {
   byte[] rgb = ((XSSFColor)color).getRGB();
   result = toHexString(rgb); 
  } else if (color instanceof HSSFColor) {
   short[] triplet = ((HSSFColor)color).getTriplet();
   result = toHexString(triplet);
  }
  return result;
 }

 private static String toHexString(Object arrayOfNumbers) {
  String hex = "";
  if (arrayOfNumbers instanceof byte[]) {
   for (byte b : (byte[])arrayOfNumbers) {
    hex += String.format("%02X", b);
   }
  } else if (arrayOfNumbers instanceof short[]){
   for (short s : (short[])arrayOfNumbers) {
    hex += String.format("%02X", s);
   }      
  }
  return hex;
 }
    
 public static void main(String[] args) throws Exception {

  String inFilePath = "./ExcelExampleIn.xlsx"; String outFilePath = "./ExcelExampleOut.xlsx";
  //String inFilePath = "./ExcelExampleIn.xls"; String outFilePath = "./ExcelExampleOut.xls";
  
  try (Workbook workbook = WorkbookFactory.create(new FileInputStream(inFilePath));
       FileOutputStream out = new FileOutputStream(outFilePath ) ) {

   for (Sheet sheet : workbook) {
    for (Row row : sheet) {
     for (Cell cell : row) {
      System.out.print("Cell: " + cell.getAddress());
      Font font = getFont(cell);
      //System.out.print(", Font:" + font);
      System.out.print(", Font:" + font.getFontName());
      System.out.print(", FontColorIndex: " + font.getColor());
      Color color = getFontColor(font, workbook);    
      //System.out.print(", FontColor: " + color);
      System.out.println(", FontColorHEX: " + getHexString(color));
     }
    }
   }
   
   workbook.write(out);
  }
 }
}

暫無
暫無

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

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