How to put color to specific cells in ods file using java

504 views Asked by At

Here I am able to merge/span the cell using 'setColumnSpannedNumber()' but could not set background color of the cell and alignment.I am using odfdom-java-0.8.6.jar currently..please suggest me a way to set the color for the cells. Thank you.

 try 
    {
            document = OdfSpreadsheetDocument.newSpreadsheetDocument();
            OdfOfficeSpreadsheet contentRoot = document.getContentRoot();
            Node node = contentRoot.getFirstChild();
                while (node != null) {
                    contentRoot.removeChild(node);
                    node = contentRoot.getFirstChild();
                }
            } catch (Exception e) {
               signature throws Exception
                throw new ReportFileGenerationException("Cannot create new ODF spread sheet document. Error: "+ e.getMessage(), e);
            }
            OdfTable table = OdfTable.newTable(document);

    for (int i = 0; i < report.size(); i++) {
        List<String> row = report.get(i);

        for (int j = 0; j < row.size(); j++) {          
            String str= row.get(j);                
    String newStr = str.replaceAll("[\u0000-\u001f]", "");              
        OdfTableCell cell = table.getCellByPosition(j, i);

            if(i==0 && j==17)
            {        
               cell.setColumnSpannedNumber(4);
                cell.setCellBackgroundColor(new Color("#ffff00"));
               cell.setHorizontalAlignment("center");                   
            }

            else if(i==0 && j==21)
            {
                cell.setColumnSpannedNumber(4);                 
           }
            else if(i==0 && j==25)
            {
                cell.setColumnSpannedNumber(4);
            }
            else if(i==0 && j==29)
            {
                cell.setColumnSpannedNumber(4);
            }
            else if(i==0 && j==33)
            {
                cell.setColumnSpannedNumber(4);
            }
   cell.setStringValue(newStr);

     }
    }

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    try {
        document.save(os);
        return os.toByteArray();
    } catch (Exception e) {     
        throw new ReportFileGenerationException("Cannot save the ODF spread sheet document as byte array. Error: "
                + e.getMessage(), e);
    } finally {
        Helper.close(os);
    }
}

}
1

There are 1 answers

0
Lyrl On

I use the API property CellBackColor, not CellBackgroundColor. Also HoriJustify rather than HorizontalAlignment.

At least in StarBasic, this is how I set background colors:

Dim Yellow As Long : Yellow = 16777113
Dim Blue As Long : Blue = 13434879
Dim White As Long : White = -1
Dim Red As Long : Red = 15425853

cell.setCellBackColor(Yellow)

If I want a new color, I manually recolor the background and then use a macro to read out the Long value associated with that color.

And to center align:

cell.setHoriJustify(2)