如何获得给定单元格的(Java Apache POI HSSF)背景色?
我有一个现有的excel电子表格,我正在从中访问和读取值,我正在使用Apache POI HSSF 它的首字母是这样写的:如何获得给定单元格的(Java Apache POI HSSF)背景色?,java,apache-poi,poi-hssf,hssf,Java,Apache Poi,Poi Hssf,Hssf,我有一个现有的excel电子表格,我正在从中访问和读取值,我正在使用Apache POI HSSF 它的首字母是这样写的: HSSFSheet sheet; FileInputStream fis = new FileInputStream(this.file); POIFSFileSystem fs = new POIFSFileSystem(fis); HSSFWorkbook wb = new HSSFWorkbook(fs); this.sheet = wb.getSheet(exshe
HSSFSheet sheet;
FileInputStream fis = new FileInputStream(this.file);
POIFSFileSystem fs = new POIFSFileSystem(fis);
HSSFWorkbook wb = new HSSFWorkbook(fs);
this.sheet = wb.getSheet(exsheet);
我正在迭代工作表中存在的所有单元格,这将生成一个单元格对象:
HSSFCell cell = (HSSFCell) cells.next();
请熟悉该框架的人员解释如何创建(HSSFColor)对象来表示工作表中每个单元格的背景颜色
非常感谢
编辑、更新
我想知道的是:如何为现有单元格的背景色创建/获取HSSFColor对象
cell.getCellStyle().getFillBackgroundColor();
此代码仅返回一个短数字,而不是HSSFColor对象。
感谢您目前的回答。您可以执行以下操作:
HSSFCell myCell = ...;
HSSFCellStyle myStyle = workbook.createCellStyle();
myStyle.setFillBackgroundColor(HSSFColor.BLUE);
myCell.setCellStyle(myStyle);
我相信对于给定的工作簿,样式的数量是有限的;如果可能,您将希望重用相同样式的对象
[编辑:抱歉,这将设置单元格的颜色。若要获取颜色,请使用以下命令:
myCell.getCellStyle().getFillBackgroundColor();
]
[编辑2:查看craig发布的自定义颜色信息,或许您可以尝试:
HSSFColor.getIndexHash().get(myCell.getCellStyle().getFillBackgroundColor())
]HSSFCell类别提供了静态颜色类别,如下所示: 如果要创建自己的自定义颜色,则需要创建和修改自定义调色板。Apache对此也提供了非常明确的指导:
获取颜色: getFillBackgroundColor返回的短值是颜色的Excel索引。 您可以使用指示的最后一个代码RMorrisey,在HSSFColor哈希表中获得与索引对应的颜色 设置颜色: 创建自定义调色板,并在给定索引处更改颜色。然后,将颜色应用于样式
//creating a custom palette for the workbook
HSSFPalette palette = wb.getCustomPalette();
//replacing the standard red with freebsd.org red
palette.setColorAtIndex(HSSFColor.RED.index,
(byte) 153, //RGB red (0-255)
(byte) 0, //RGB green
(byte) 0 //RGB blue
);
// or creating a new Color
HSSFColor myColor = palette.addColor((byte) 153, (byte) 0, (byte) 0);
HSSFCellStyle style = wb.createCellStyle();
style.setFillForegroundColor(myColor);
问候
GuillaumeXSSFCellStyle的背景色信息可以通过以下方法获得:
XSSFCellStyle.getFillForegroundXSSFColor().getCTColor()
您可以将其打印出来,您将看到它的结构。import java.io.File;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import org.apache.poi.hssf.usermodel.HSSFPalette;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
/**
* @author mohdasif_2688@rocketmail.com
*
*/
public class ExcelPractice {
/**
* Must Read :
*
* Code to get the background color from an excel sheet in RGB Format and display on the console
* Save the content of the xls file into another OUTPUT.xls file.
* Using a sample sheet with only first row filled with background color.
* Code uses HSSF which means i am only using xls format.
* Using poi-3.5-FINAL.jar
* Solution with the output provided
* Observation : Some Custom color's are not recognized as they may not be defined
* in the excel color palette thus the code returns the almost similar color code.
*/
public static void main(String[] args) {
try {
FileInputStream fileInputStream=new FileInputStream(new File("D:\\Excel_File.xls"));
HSSFWorkbook workbook=new HSSFWorkbook(fileInputStream);
HSSFSheet sheet=workbook.getSheetAt(0);
Iterator<Row> rowIterator= sheet.iterator();
while (rowIterator.hasNext()) {
Row row=rowIterator.next();
Iterator<Cell> cellIterator=row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = (Cell) cellIterator.next();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
System.out.println(cell.getBooleanCellValue()+"\t\t");
System.out.println(cell.getCellStyle().getFillForegroundColor());
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.println(cell.getNumericCellValue()+"\t\t");
System.out.println(cell.getCellStyle().getFillForegroundColor());
break;
case Cell.CELL_TYPE_STRING:
System.out.println(cell.getStringCellValue()+"\t\t");
//System.out.println(HSSFColor.getIndexHash().get(cell.getCellStyle().getFillBackgroundColor()));
int num=cell.getColumnIndex();
Cell cell1 = row.getCell(num);
CellStyle cellStyle = cell1.getCellStyle();
getColorPattern(cellStyle.getFillForegroundColor());
break;
default:
break;
}
}
System.out.println();
fileInputStream.close();
FileOutputStream fileOutputStream=new FileOutputStream(new File("D:\\OUTPUT.xls"));
workbook.write(fileOutputStream);
fileInputStream.close();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.toString();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//Method to identify the color pattern
private static short[] getColorPattern(short colorIdx){
short[] triplet = null;
HSSFWorkbook workbook=new HSSFWorkbook();
HSSFPalette palette = workbook.getCustomPalette();
HSSFColor color = palette.getColor(colorIdx);
triplet = color.getTriplet();
System.out.println("color : " + triplet[0] +"," + triplet[1] + "," + triplet[2]);
return triplet;
}
}
/** Output of the above code as executed in my system
S.NO.
color : 255,255,0
VTU Number
color : 0,128,0
First Name
color : 51,204,204
Middle Name
color : 255,0,0
Last Name
color : 102,102,153
Branch
color : 255,102,0
E-mail id
color : 0,255,0
Mobile Number
color : 255,255,255
*/
导入java.io.FileInputStream;
导入java.io.FileNotFoundException;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.util.Iterator;
导入org.apache.poi.hssf.usermodel.hssfpalete;
导入org.apache.poi.hssf.usermodel.HSSFSheet;
导入org.apache.poi.hssf.usermodel.HSSFWorkbook;
导入org.apache.poi.hssf.util.HSSFColor;
导入org.apache.poi.ss.usermodel.Cell;
导入org.apache.poi.ss.usermodel.CellStyle;
导入org.apache.poi.ss.usermodel.Row;
/**
*@作者mohdasif_2688@rocketmail.com
*
*/
公共课实践{
/**
*必须阅读:
*
*用于从RGB格式的excel工作表中获取背景色并显示在控制台上的代码
*将xls文件的内容保存到另一个OUTPUT.xls文件中。
*使用仅第一行填充背景色的样本表。
*代码使用HSSF,这意味着我只使用xls格式。
*使用poi-3.5-FINAL.jar
*提供输出的解决方案
*观察:某些自定义颜色无法识别,因为它们可能未定义
*因此,在excel调色板中,代码返回几乎相似的颜色代码。
*/
公共静态void main(字符串[]args){
试一试{
FileInputStream FileInputStream=newfileinputstream(新文件(“D:\\Excel\u File.xls”);
HSSF工作簿=新的HSSF工作簿(fileInputStream);
HSSFSheet sheet=工作簿。getSheetAt(0);
迭代器rowIterator=sheet.Iterator();
while(roweiterator.hasNext()){
行=行迭代器。下一步();
迭代器cellIterator=row.cellIterator();
while(cellIterator.hasNext()){
Cell Cell=(Cell)cellIterator.next();
开关(cell.getCellType()){
case Cell.Cell\u类型\u布尔值:
System.out.println(cell.getBooleanCellValue()+“\t\t”);
System.out.println(cell.getCellStyle().getFillForegroundColor());
打破
case Cell.Cell\u类型\u数值:
System.out.println(cell.getNumericCellValue()+“\t\t”);
System.out.println(cell.getCellStyle().getFillForegroundColor());
打破
case Cell.Cell\u类型\u字符串:
System.out.println(cell.getStringCellValue()+“\t\t”);
//System.out.println(HSSFColor.getIndexHash().get(cell.getCellStyle().getFillBackgroundColor());
int num=cell.getColumnIndex();
Cell cell1=row.getCell(num);
CellStyle CellStyle=cell1.getCellStyle();
getColorPattern(cellStyle.getFillForegroundColor());
打破
违约:
打破
}
}
System.out.println();
fileInputStream.close();
FileOutputStream FileOutputStream=新的FileOutputStream(新文件(“D:\\OUTPUT.xls”);
workbook.write(fileOutputStream);
fileInputStream.close();
}
}catch(filenotfounde异常){
//TODO自动生成的捕捉块
e、 toString();
}
捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
//识别颜色图案的方法
私有静态短[]getColorPattern(短colorIdx){
短[]三元组=空;
HSSFWorkbook=新的HSSFWorkbook();
HSSFPalette调色板=工作簿.getCustomPalette();
HSSFColor color=palete.getColor(colorIdx);
triplet=color.getTriplet();
System.out.println(“颜色:“+triplet[0]+”、“+triplet[1]+”、“+triplet[2]);
返回三元组
cell.getCellStyle().getFillForegroundColorColor().getARGBHex()
val rgb: Array[Byte] = cell.getCellStyle.getFillForegroundColorColor.getRgb
def toInt(b: Byte): Int = {
if (b<0) 256+b else b
}
val rgbInts = rgb.map(toInt)
val color = new Color(rgbInts(0),rgbInts(1),rgbInts(2))
XSSFColor backgroundColor = cell.getCellStyle().
getFillBackgroundXSSFColor();
System.out.println("backgroundColor is "+backgroundColor.getARGBHex());
Output: FFFF0000 //the first FF should be ignored.