В python, используя pyuno, я могу сделать это вот так:
table = self.model.createInstance("com.sun.star.text.TextTable")
В С# это не работает. Вот мой тестовый код (я понимаю, что, вероятно, мне не нужны все те, которые используют инструкции, но я адаптирую код другого):
using System;
using unoidl.com.sun.star.lang;
using unoidl.com.sun.star.uno;
using unoidl.com.sun.star.bridge;
using unoidl.com.sun.star.frame;
using unoidl.com.sun.star.document;
using unoidl.com.sun.star.text;
using unoidl.com.sun.star.container;
using unoidl.com.sun.star.util;
using unoidl.com.sun.star.table;
using unoidl.com.sun.star.beans;
namespace FromScratch
{
class MainClass
{
public static void Main(string[] args)
{
XComponentContext componentContext =
uno.util.Bootstrap.bootstrap();
XMultiServiceFactory multiServiceFactory = (XMultiServiceFactory)
componentContext.getServiceManager();
XTextDocument document;
XComponentLoader loader = (XComponentLoader)
multiServiceFactory.createInstance
("com.sun.star.frame.Desktop");
document = (XTextDocument) loader.loadComponentFromURL
("private:factory/swriter", "_blank", 0,
new PropertyValue[0]);
XText text = document.getText();
XTextCursor cursor = text.createTextCursor();
XTextTable table = (XTextTable)
multiServiceFactory.createInstance
("com.sun.star.text.TextTable");
table.initialize(2, 2);
text.insertTextContent(cursor, table, false);
}
}
}
Большинство из них, похоже, работают нормально, но когда он добирается до этой строки:
table.initialize(2, 2);
Я получаю ошибку времени выполнения:
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object
at FromScratch.MainClass.Main (System.String[] args) [0x00063] in /home/matthew/Desktop/OpenOfficeSample/FromScratch/Main.cs:37
По-видимому, эта строка:
XTextTable table = (XTextTable)
multiServiceFactory.createInstance
("com.sun.star.text.TextTable");
фактически не устанавливает таблицу.
Что здесь происходит?
Решение (из OOoForum.org):
Вы должны получить текстовую таблицу из мультисервиса документов factory, а не из мультисервиса factory менеджера сервиса. Вы можете сделать это, отбросив ваш документ (модель) до XMultiServiceFactory и вызвав его метод createInstance.
XTextTable table = (XTextTable)
((XMultiServiceFactory)document).createInstance
("com.sun.star.text.TextTable");
См. DevGuide.