Как поставить цвет для определенных ячеек в файле ods, используя Java

1

Здесь я могу объединить/пролететь ячейку, используя 'setColumnSpannedNumber()', но не смог установить цвет фона ячейки и alignment.Я использую odfdom-java-0.8.6.jar в настоящее время..пожалуйста, предложите мне способ установить цвет для клеток. Спасибо.

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

}
Теги:
openoffice.org
ods
odfdom

1 ответ

0

Я использую свойство API CellBackColor, а не CellBackgroundColor. Также HoriJustify, а не HorizontalAlignment.

По крайней мере, в StarBasic, я устанавливаю цвета фона:

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)

Если я хочу новый цвет, я вручную перекрашу фон, а затем с помощью макроса зачитаю значение Long, связанное с этим цветом.

И выравнивание по центру:

cell.setHoriJustify(2)

Ещё вопросы

Сообщество Overcoder
Наверх
Меню