使用Apache ODF Toolkit Simple API和Java对libreoffice calc单元格进行格式化
我需要使用Apache ODF Toolking创建一个ods文档,并格式化其单元格的内容
我能够为日期和简单的数字设置格式,但由于某种原因,当我尝试用科学符号格式化时,它不起作用。它无法解析字符串或将E00包装为“E00”
SpreadsheetDocument document = SpreadsheetDocument.newSpreadsheetDocument();
document.removeSheet(0);
document.setLocale(Locale.US);
Table sheet = document.appendSheet("My chart");
List<Row> rows = sheet.appendRows(10);
sheet.getColumnByIndex(0).setWidth(27.06);
Row headerRow = rows.get(0);
headerRow.getCellByIndex(0).setStringValue("This is a string");
Row dateRow = rows.get(1);
Calendar date = GregorianCalendar.getInstance();
dateRow.getCellByIndex(0).setDateValue(date);
dateRow.getCellByIndex(0).setFormatString("yyyy-MM-dd");
Row numRow = rows.get(1);
numRow.getCellByIndex(0).setDoubleValue(9.12345678);
//numRow.getCellByIndex(0).setFormatString("0.000"); // works
//numRow.getCellByIndex(0).setFormatString("0.00E+00"); // crashes
numRow.getCellByIndex(0).setFormatString("0.00E00"); // does not work, it becomes 0.00"E00"
document.save(new File("c:\\Users\\enrico\\Desktop\\openoffice.ods"));
如果有人能帮忙,我将不胜感激,谢谢
完整代码在这里https://gist.github.com/EnricoScantamburlo/b06d3a9e52682276b8ca4f6bf51e6f6a
# 1 楼答案
基本上,Apache ODF Toolkit(从0.8.2版开始)不支持您想要做的事情。 能够使用科学的数字格式。ods文件底层库(在您的例子中是ApacheODF工具)应该能够生成
<number:scientific-number>
号样式。有关科学数字样式的更多信息,请参见http://docs.oasis-open.org/office/v1.1/OS/OpenDocument-v1.1-html/OpenDocument-v1.1.html#14.7.1.Number%20Style|outline似乎ApacheODF工具包将
E0
视为一种货币或其他特殊符号,而不是生成类似这就产生了
好消息是,Apache ODF Toolkit是开源的,因此您可以提供修复,这将非常简单,只需修复
OdfNumberStyle.buildFromFormat