我正在使用 Java 自动创建和修改 Open Office Calc 文档。
我想知道如何获取电子表格中的工作表数量。我似乎找不到任何计数、长度、大小或类似的函数。
这是我的代码。提前致谢!
public static void openDocument(String filename)
{
try
{
// Get the remote office component context
xContext = Bootstrap.bootstrap();
// Get the remote office service manager
XMultiComponentFactory xMCF = xContext.getServiceManager();
// Get the root frame (i.e. desktop) of openoffice framework.
oDesktop = xMCF.createInstanceWithContext("com.sun.star.frame.Desktop", xContext);
// Desktop has 3 interfaces. The XComponentLoader interface provides ability to load components.
XComponentLoader xCompLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class,
oDesktop);
PropertyValue[] loadProps = new PropertyValue[0];
xSpreadsheetComponent = xCompLoader.loadComponentFromURL(getUpdatedPath(filename), "_blank", 0, loadProps);
xStorable = (XStorable) UnoRuntime.queryInterface(XStorable.class, xSpreadsheetComponent);
xSpreadsheetDocument = (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class,
xSpreadsheetComponent);
xSpreadsheets = xSpreadsheetDocument.getSheets();
// Need code here to get number of sheets
}
catch (Exception e)
{
e.printStackTrace();
}
最佳答案
这更像是一条评论(因为我不知道 Java 的正确语法 - 也许您需要在 xSpreadsheets 上执行 .queryInterface?),但作为包含图像的答案发布。使用 Bernard Marcelly 的对象检查工具 XRay ( http://bernard.marcelly.perso.sfr.fr/index2.html ) 显示 XSpreadsheets 对象具有方法 .getCount()。我使用 OpenOffice Basic 测试了此方法,它按预期工作。
关于java - 使用 Java 获取 Open Office 中的工作表数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34278327/