https://bz.apache.org/bugzilla/show_bug.cgi?id=66052

            Bug ID: 66052
           Summary: XSSFColor cannot be used the same time as
                    org.apache.poi.ss.util classes
           Product: POI
           Version: unspecified
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: normal
          Priority: P2
         Component: XSSF
          Assignee: dev@poi.apache.org
          Reporter: axel.richter.pri...@web.de
  Target Milestone: ---

If one tries using XSSFCellStyle having XSSFColor as interior fill color and
then tries using org.apache.poi.ss.util classes (PropertyTemplate or CellUtil
or RegionUtil) then after this all cell interiors are filled black, because
IndexedColors 0 is set for the interior.

That is because org.apache.poi.ss.util classes do not take into account the RGB
interior cell fill and org.apache.poi.xssf.usermodel.IndexedColorMap has no
meaningful usage until now.

Complete example to test:

import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.PropertyTemplate;
import org.apache.poi.xssf.usermodel.*;
import org.apache.commons.codec.binary.Hex;

class SSUtilVsXSSFColor {

 public static void main(String[] args) throws Exception {

  try (Workbook workbook = new XSSFWorkbook(); 
       FileOutputStream fileout = new
FileOutputStream("./SSUtilVsXSSFColor.xlsx") ) {

   CellStyle cellStyle = workbook.createCellStyle();
   if (cellStyle instanceof XSSFCellStyle) {
    XSSFCellStyle xssfCellStyle = (XSSFCellStyle)cellStyle;
        XSSFWorkbook xssfWorkbook = (XSSFWorkbook)workbook;
    String rgbS = "FFFF00";
    byte[] rgbB = Hex.decodeHex(rgbS);
        IndexedColorMap colorMap =
xssfWorkbook.getStylesSource().getIndexedColors();
    XSSFColor color = new XSSFColor(rgbB, colorMap);
    xssfCellStyle.setFillForegroundColor(color);
    xssfCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);        
   }

   int startDataRow = 6; // row 7 (index 0-based)
   int endDataRow = 11; // row 12 (index 0-based)
   int startDataColumn = 1; // column B (index 0-based)
   int endDataColumn = 10; // column K (index 0-based)

   Sheet sheet = workbook.createSheet();

   for (int r = startDataRow; r <= endDataRow; r++) {
    Row row = sheet.createRow(r);
    for (int c = startDataColumn; c <= endDataColumn; c++) {
     Cell cell = row.createCell(c);
     cell.setCellValue(cell.getAddress().formatAsString());
         cell.setCellStyle(cellStyle);
    }
   }

   PropertyTemplate propertyTemplate = new PropertyTemplate();
   propertyTemplate.drawBorders(new CellRangeAddress(startDataRow, endDataRow,
startDataColumn, endDataColumn), 
    BorderStyle.MEDIUM, BorderExtent.ALL);

   propertyTemplate.applyBorders(sheet); // after this all cell interiors are
filled black, because IndexedColors 0 is set 
   // same is usimg all other org.apache.poi.ss.util classes which manipulate
cell styles (CellUtil or RegionUtil) 

   workbook.write(fileout);

  }
 }
}

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@poi.apache.org
For additional commands, e-mail: dev-h...@poi.apache.org

Reply via email to