java poi 设置单元格颜色_如何获取给定单元格的(Java Apache POI HSSF)背景颜色?...

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 rowIterator= sheet.iterator();

while (rowIterator.hasNext()) {

Row row=rowIterator.next();

Iterator cellIterator=row.cellIterator();

while (cellIterator.hasNext()) {

Cell cell = (Cell) cellIterator.next();

switch (cell.getCellType()) {

case Cell.CELL_TYPE_BOOLEAN:

System.out.println(cell.getBooleanCellValue()+"tt");

System.out.println(cell.getCellStyle().getFillForegroundColor());

break;

case Cell.CELL_TYPE_NUMERIC:

System.out.println(cell.getNumericCellValue()+"tt");

System.out.println(cell.getCellStyle().getFillForegroundColor());

break;

case Cell.CELL_TYPE_STRING:

System.out.println(cell.getStringCellValue()+"tt");

//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

*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值