使用C#的OpenOffice Writer出现问题

时间:2011-01-29 14:28:35

标签: c# openoffice-writer

我正在用C#创建一个OO Writer文档。

任何帮助都会受到赞赏 - 我不知道我是要来还是去,我已经尝试了很多变化....

使用C#,有没有人成功获得以下工作?我只有一个简单的2列表,并希望将列宽设置为不同的值(此阶段的实际值不重要 - 只是宽度不同)。

此代码改编自各种Web源,作为如何进行列宽的示例。我无法让它发挥作用......

//For OpenOffice....  
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.text;  
using unoidl.com.sun.star.beans;  
..............................  
XTextTable odtTbl = (XTextTable) ((XMultiServiceFactory)oodt).createInstance("com.sun.star.text.TextTable");  
odtTbl.initialize(10, 2);  
XPropertySet xPS = (XPropertySet)odtTbl;  
Object xObj = xPS.getPropertyValue("TableColumnSeparators")**; // << Runtime ERROR**
TableColumnSeparator[] xSeparators = (TableColumnSeparator[])xObj;  
xSeparators[0].Position = 500;  
xSeparators[1].Position = 5000;  
xPS.setPropertyValue("TableColumnSeparators", new uno.Any(typeof(unoidl.com.sun.star.text.XTextTable),xSeparators));  

// Runtime ERROR indicates the ; at the end of the Object line, with message of IllegalArgumentException

现在,这只是所有尝试组合中的一种错误。根本没有多少允许执行,但上面的代码确实运行直到错误。

在C#中执行此操作的正确代码是什么?

此外,将O'Writer标题设置为特定样式(例如“标题1”)的正确C#代码是什么,以便它在文档中看起来和打印样式一样?

谢谢。

1 个答案:

答案 0 :(得分:0)

        unoidl.com.sun.star.uno.XComponentContext localContext = uno.util.Bootstrap.bootstrap();
        unoidl.com.sun.star.lang.XMultiServiceFactory multiServiceFactory = (unoidl.com.sun.star.lang.XMultiServiceFactory)localContext.getServiceManager();
        XComponentLoader componentLoader =(XComponentLoader)multiServiceFactory.createInstance("com.sun.star.frame.Desktop");
        XComponent xComponent = componentLoader.loadComponentFromURL(
        "private:factory/swriter", //a blank writer document
        "_blank", 0, //into a blank frame use no searchflag
        new unoidl.com.sun.star.beans.PropertyValue[0]);//use no additional arguments.
        //object odtTbl = null;
        //odtTbl = ((XMultiServiceFactory)xComponent).createInstance("com.sun.star.text.TextTable");

        XTextDocument xTextDocument = (unoidl.com.sun.star.text.XTextDocument)xComponent;
        XText xText = xTextDocument.getText();
        XTextCursor xTextCursor = xText.createTextCursor();

                    XPropertySet xTextCursorProps = (unoidl.com.sun.star.beans.XPropertySet) xTextCursor;
                    XSimpleText xSimpleText = (XSimpleText)xText;

                    XTextCursor xCursor = xSimpleText.createTextCursor();

        object objTextTable = null;
        objTextTable = ((XMultiServiceFactory)xComponent).createInstance("com.sun.star.text.TextTable");
        XTextTable xTextTable = (XTextTable)objTextTable;
        xTextTable.initialize(2,3);
        xText.insertTextContent(xCursor, xTextTable, false);


        XPropertySet xPS = (XPropertySet)objTextTable;  
        uno.Any xObj = xPS.getPropertyValue("TableColumnSeparators");
        TableColumnSeparator[] xSeparators = (TableColumnSeparator[])xObj.Value;  //!!!! xObj.Value

        xSeparators[0].Position = 2000;  
        xSeparators[1].Position = 3000;
        xPS.setPropertyValue("TableColumnSeparators", new uno.Any(typeof(TableColumnSeparator[]), xSeparators)); //!!!! TableColumnSeparator[]