在java中为xls文件单元设置不同的背景颜色

时间:2015-11-09 13:32:26

标签: java apache-poi

我想创建一个xls文件并为单元格设置不同的颜色。 Excel文件有15个单元格,最后9个单元格始终设置最后一个单元格颜色。

你能帮忙解决代码中出了什么问题吗?

以下是代码:

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;
}

2 个答案:

答案 0 :(得分:1)

我已经改变了这样的setColor()方法并且它可以工作。

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;
}

答案 1 :(得分:0)

以下是使用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);
         }
      }
   }
}  

有关详情,请访问link