java - 如何使用java为ods文件中的特定单元格添加颜色

标签 java openoffice.org ods odfdom

在这里,我可以使用“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);
    }
}

}

最佳答案

我使用 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)

关于java - 如何使用java为ods文件中的特定单元格添加颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29330227/

相关文章:

java - 从 Java 到 Coldfusion 的多部分文件传输 - 无前导边界 : %PDF-1. 4

.net - OpenOffice 和 .NET

版本控制压缩文件(docx、odt)

ios - 使用 WebView 打开 .odp、.odt、.ods 文件

sas - 我更改了列名称,但 SAS PROC REPORT 仍使用旧名称

java - 数据表未显示在 Spring MVC 数据中

java - 插入mysql表时出现异常

java - Adhaar 卡 API

java - 使用 Apache Poi 如何创建多值电子表格单元格,以便在 MS Excel 中正确显示,而不仅仅是在 OpenOffice 中

mysql - 在没有 phpMyAdmin 的情况下在 MySQL 中导入 *.ods 文件