【问题标题】:How to put color to specific cells in ods file using java如何使用java为ods文件中的特定单元格添加颜色
【发布时间】:2015-03-29 14:19:08
【问题描述】:

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

}

【问题讨论】:

    标签: java openoffice.org ods odfdom


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-23
      • 2015-01-28
      • 2011-10-09
      • 2016-12-02
      • 2020-05-17
      • 1970-01-01
      • 2018-03-21
      • 2012-10-05
      相关资源
      最近更新 更多