[英]Get text and background color of a cell in .xls sheet as hex in java
我需要驗證單元格折扣的文本的十六進制顏色是#FF1493,同一單元格的背景的十六進制顏色是#FFC0CB。
我得到背景的rgb顏色,但我無法將其轉換為十六進制,我無法獲得文本的十六進制顏色。
請在下面找到代碼和表格的屏幕截圖,以便讓您了解有關細胞的問題。
String fileFormat = FileManagement.getFileFormat(new java.io.File(fileName));
List<String> content = new ArrayList<>();
HSSFWorkbook workbookXLS = null;
try {
InputStream ExcelFileToRead = new FileInputStream(fileName);
//Getting the workbook instance for .XLS('old' excel format) file
workbookXLS = new HSSFWorkbook(ExcelFileToRead);
} catch (FileNotFoundException | NotOLE2FileException ex) {
fail(ex.getMessage() + "! File: " + fileName);
}
//getting the first or specific sheet from the workbook
HSSFSheet sheetXLS = workbookXLS.getSheetAt(0);
HSSFRow rowXLS;
HSSFCell cellXLS;
//Iterating all the rows in the sheet
Iterator rows = sheetXLS.rowIterator();
while (rows.hasNext()) {
rowXLS = (HSSFRow) rows.next();
if (rowXLS.getRowNum() = 7) {
cellXLS = rowXLS.getCell(0);
if (cellXLS.getCellTypeEnum() == CellType.STRING) {
if (cellXLS.getStringCellValue().equals("loads : Id")) {
Color colorFG = cellXLS.getCellStyle().getFillForegroundColorColor();
short[] rgb = HSSFColor.toHSSFColor(colorFG).getTriplet(); //this returns the rgb color of the background -> 255 192 203
System.out.println("THE FIRST COLOR RGB IS " + rgb[0] + "SECOND " + rgb[1] + "THIRD " + rgb[2]);
Color colorBG = cellXLS.getCellStyle().getFillBackgroundColorColor();
short[] hshs = HSSFColor.toHSSFColor(colorBG).getTriplet(); //this returns -> 0 0 0
System.out.println("THE SECOND COLOR RGB IS" + hshs[0] + "SECOND " + hshs[1] + "THIRD " + hshs[2]);
}
} else if (cellXLS.getCellTypeEnum() == CellType.NUMERIC) {
content.add(String.valueOf(cellXLS.getNumericCellValue()));
}
}
}[![enter image description here][1]][1]
任何幫助將受到高度贊賞。
你可以嘗試這個:
String hex = String.format("#%02x%02x%02x", rgb[0], rgb[1], rgb[2]);
或者對於大寫字母X:
String hex = String.format("#%02X%02X%02X", rgb[0], rgb[1], rgb[2]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.