简体   繁体   中英

Setting different background color to xls file cells in java

I want to create an xls file and set different color to cells. Excel file has a 15 cell and in last 9 cells always sets the last cell color.

Could you please help what is wrong in code ?

Here is the code:

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFPalette;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;

public class TestC {

private static HSSFWorkbook wb = new HSSFWorkbook();
private static List colors = new ArrayList<>();

public static void main(String[] args) throws InterruptedException, IOException {
    colors.add(0, new byte[] { (byte) 240, (byte) 240, (byte) 240 });
    colors.add(1, new byte[] { (byte) 128, (byte) 128, (byte) 128 });
    colors.add(2, new byte[] { (byte) 240, (byte) 240, (byte) 240 });
    colors.add(3, new byte[] { (byte) 128, (byte) 0, (byte) 128 });
    colors.add(4, new byte[] { (byte) 255, (byte) 0, (byte) 0 });
    colors.add(5, new byte[] { (byte) 128, (byte) 0, (byte) 0 });
    colors.add(6, new byte[] { (byte) 0, (byte) 255, (byte) 255 });
    colors.add(7, new byte[] { (byte) 0, (byte) 25, (byte) 255 });
    colors.add(8, new byte[] { (byte) 200, (byte) 255, (byte) 255 });
    colors.add(9, new byte[] { (byte) 0, (byte) 255, (byte) 64 });
    colors.add(10, new byte[] { (byte) 0, (byte) 128, (byte) 255 });
    colors.add(11, new byte[] { (byte) 212, (byte) 208, (byte) 200 });
    colors.add(12, new byte[] { (byte) 112, (byte) 208, (byte) 200 });
    colors.add(13, new byte[] { (byte) 250, (byte) 87, (byte) 22 });
    colors.add(14, new byte[] { (byte) 0, (byte) 255, (byte) 206 });
    colors.add(15, new byte[] { (byte) 0, (byte) 120, (byte) 255 });

    HSSFSheet sheet = wb.createSheet();

    int colorIdx = 0;
    for (int r = 0; r < 5; r++) {
        HSSFRow row = sheet.getRow(r);
        if (row == null) {
            row = sheet.createRow(r);
        }
        for (int c = 0; c < 3; c++) {
            colorIdx++;
            HSSFCell cell = row.createCell(c);
            byte[] color = (byte[]) colors.get(colorIdx);
            cell.setCellStyle(getCellStyle(color[0], color[1], color[2]));
            cell.setCellValue(colorIdx);
        }
    }

    FileOutputStream out = new FileOutputStream("D:\\test_dir\\colorful.xls");
    wb.write(out);
    out.close();
}

private static HSSFCellStyle getCellStyle(byte red, byte green, byte blue) {
    HSSFCellStyle style = wb.createCellStyle();
    HSSFFont font = wb.createFont();
    style.setFont(font);

    // set background color

    System.out.println("Colorrr  === " + red + " " + green + " " + blue);
    HSSFColor hsColor = new HSSFColor();
    hsColor = setColor(red, green, blue);
    style.setFillForegroundColor(hsColor.getIndex());
    style.setFillBackgroundColor(hsColor.getIndex());
    style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

    return style;
}

private static HSSFColor setColor(byte r, byte g, byte b) {
    HSSFPalette palette = wb.getCustomPalette();
    HSSFColor hssfColor = null;
    hssfColor = palette.findColor(r, g, b);
    if (hssfColor == null) {
        palette.setColorAtIndex(HSSFColor.LAVENDER.index, r, g, b);
        hssfColor = palette.getColor(HSSFColor.LAVENDER.index);
    }
    return hssfColor;
}

I have changed the setColor() method like this and it works.

private static short colorIdx = 0x10;

private static  HSSFColor setColor(byte r, byte g, byte b) {
    HSSFPalette palette = wb.getCustomPalette();
    palette.setColorAtIndex(colorIdx, r, g, b);
    HSSFColor hssfColor = palette.getColor(colorIdx);
    colorIdx++;     
    return hssfColor;
}

Here is a way of setting background color of a cell using apache-poi -

FileInputStream ins = new FileInputStream(new
     File("/driver/dir/myfile.xls"));  
Workbook wb = new HSSFWorkbook(ins);

for(int i=0; i< wb.getNumberOfSheets();i++){
   Sheet sheet = wb.getSheetAt(i);        
   if(sheet!=null){
      Row row = sheet.getRow(0);
      Iterator<Cell> ct = row.iterator();
      while(ct.hasNext()){
         Cell cell = (Cell) ct.next();
         if(cell!=null){
            HSSFCellStyle hssfstyle = (HSSFCellStyle) cell.getCellStyle();
            hssfstyle.setFillBackgroundColor(HSSFColor.YELLOW.index);
            hssfstyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
            cell.setStyle(hssfstyle);
         }
      }
   }
}  

For more details you may visit the link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM