1

在这里,我可以使用“setColumnSpannedNumber()”合并/跨越单元格,但无法设置单元格的背景颜色和对齐方式。我目前正在使用 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);
    }
}

}
4

1 回答 1

0

我使用 API 属性 CellBackColor,而不是 CellBackgroundColor。还有 HoriJustify 而不是 Horizo​​ntalAlignment。

至少在 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)
于 2015-04-07T18:18:35.350 回答