Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何向多个单元格poi添加不同的背景色_Java_Apache Poi - Fatal编程技术网

Java 如何向多个单元格poi添加不同的背景色

Java 如何向多个单元格poi添加不同的背景色,java,apache-poi,Java,Apache Poi,我需要为XLS文件的单元格添加不同的背景颜色和边框 这是我的密码 Row r = sh.createRow(sh.getPhysicalNumberOfRows()); CellStyle style = wb.createCellStyle(); style = wb.createCellStyle(r.getPhyscal); //region style.setFillForegroundColor(IndexedColors.ORANGE.getInde

我需要为XLS文件的单元格添加不同的背景颜色和边框 这是我的密码

  Row r = sh.createRow(sh.getPhysicalNumberOfRows());
     CellStyle style = wb.createCellStyle();
     style = wb.createCellStyle(r.getPhyscal);
     //region
style.setFillForegroundColor(IndexedColors.ORANGE.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    org.apache.poi.ss.usermodel.Cell c = r.createCell(1);
    c.setCellValue((String) jComboBox1.getSelectedItem());


 c.setCellStyle(style);
    //sA   
     style.setFillForegroundColor(IndexedColors.RED.getIndex());
    style.setFillPattern(CellStyle.SOLID_FOREGROUND);
        c = r.createCell(2);
        c.setCellValue((String) jT1.getText() );
  c.setCellStyle(style);
这就是我需要的结果

但这就是我得到的结果


您必须为每种颜色创建一个新的
CellStyle

CellStyle orangeStyle = wb.createCellStyle(r.getPhyscal);     
orangeStyle.setFillForegroundColor(IndexedColors.ORANGE.getIndex());
orangeStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);

CellStyle redStyle = wb.createCellStyle(r.getPhyscal);
redStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
redStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);

..

并将配件样式附加到每个受影响的单元格

为每列创建不同的单元格样式?是的,为每列创建不同的单元格样式颜色带边框为我在文件xl中写入的每列创建不同的celsl样式颜色带边框对于每种颜色,您需要一个新的
单元格!e
对象。在示例代码中,只创建一个并更改其属性。这样,对于具有该样式的所有单元格,只有最后一个设置才有效。不要那样做。相反,根据需要创建尽可能多的单元格样式,颜色、字体、边框等的不同组合对应一个单元格样式。