如何使用java为ods文件中的特定单元格添加颜色
在这里,我可以使用“setColumnSpandNumber()”合并/跨越单元格,但无法设置单元格的背景色和对齐方式。我当前使用的是odfdom-java-0.8.6.jar。请建议我设置单元格颜色的方法。多谢各位如何使用java为ods文件中的特定单元格添加颜色,java,openoffice.org,ods,odfdom,Java,Openoffice.org,Ods,Odfdom,在这里,我可以使用“setColumnSpandNumber()”合并/跨越单元格,但无法设置单元格的背景色和对齐方式。我当前使用的是odfdom-java-0.8.6.jar。请建议我设置单元格颜色的方法。多谢各位 try { document = OdfSpreadsheetDocument.newSpreadsheetDocument(); OdfOfficeSpreadsheet contentRoot = document.g
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);
}
}
}
试试看
{
document=OdfSpreadsheetDocument.newSpreadsheetDocument();
OdfOfficeSpreadsheet contentRoot=document.getContentRoot();
Node Node=contentRoot.getFirstChild();
while(节点!=null){
contentRoot.removeChild(节点);
node=contentRoot.getFirstChild();
}
}捕获(例外e){
签名引发异常
抛出新的ReportFileGenerationException(“无法创建新的ODF电子表格文档。错误:”+e.getMessage(),e);
}
OdfTable table=OdfTable.newTable(文档);
对于(int i=0;i
我使用API属性CellBackColor,而不是CellBackgroundColor。
同样,水平对齐而非水平对齐
至少在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)
如果我想要一种新颜色,我会手动重新着色背景,然后使用宏读取与该颜色关联的长值
和要居中对齐:
cell.setHoriJustify(2)