2009-09-01 109 views
1

我正在研究通過UNO與OpenOffice(swriter)交互的Java API。 對於TextTable,我很難設置TableColumn的「OptimalWidth」屬性。OpenOffice API - 用於表(所有列)的OptimalWidth選項

我試過下面的代碼,它似乎getColumns()方法不能帶我到TableColumn的屬性,讓你只能插入和刪除列。

XTableColumns xColumns = xTextTable.getColumns(); 
XIndexAccess xIndexAccess = (XIndexAccess) UnoRuntime.queryInterface(
     XIndexAccess.class, xColumns); 

for (int i = 0; i < xIndexAccess.getCount(); i++) { 
    XPropertySet xColumnProps = (XPropertySet) UnoRuntime 
      .queryInterface(XPropertySet.class, 
        (Any) xIndexAccess.getByIndex(i)); 
    if (xColumnProps != null) { 
     xColumn.setPropertyValue("OptimalWidth", new Boolean(true)); 
    } 
} 

任何人都可以幫我或給我任何提示設置表的OptimalWidth屬性? 非常感謝您提前!

回答

2

這是相當奧德賽找出來,但我終於成功地創建一個函數,它正是這麼做的:

private void optimizeTableColumnWidths(XTextTable table) 
throws Exception 
{ 
    XTextViewCursorSupplier cursorSupplier = (XTextViewCursorSupplier) 
      UnoRuntime.queryInterface(XTextViewCursorSupplier.class, 
        document.getCurrentController()); 
    XTextViewCursor viewCursor = cursorSupplier.getViewCursor(); 

    String cellName = "A1"; 
    XText cellText = (XText)UnoRuntime.queryInterface(
      XText.class, table.getCellByName(cellName)); 
    XTextCursor cursor = cellText.createTextCursor(); 

    viewCursor.gotoRange(cursor, false); 
    viewCursor.gotoEnd(true); 
    viewCursor.gotoEnd(true); 

    XController controller = document.getCurrentController(); 
    XFrame frame = controller.getFrame(); 
    XDispatchProvider dispatchProvider = (XDispatchProvider) 
     UnoRuntime.queryInterface(XDispatchProvider.class, frame); 

    String unoAction = ".uno:SetOptimalColumnWidth"; 
    String targetFrameName = ""; 
    int searchFlags = 0; 
    PropertyValue[] properties = new PropertyValue[0]; 

    dispatchHelper.executeDispatch(
      dispatchProvider, 
      unoAction, 
      targetFrameName, 
      searchFlags, 
      properties); 
} 

這是很重要的dispatchHelper來自OpenOffice的背景下,而不是從當前文件。我找回了dispatchHelper這樣:

XComponentContext context = Bootstrap.bootstrap(); 
    XMultiComponentFactory factory = 
     context.getServiceManager(); 
    Object dispatchHelperObject = factory.createInstanceWithContext(
     "com.sun.star.frame.DispatchHelper", ooContext); 
    this.dispatchHelper = (XDispatchHelper)UnoRuntime.queryInterface(
     XDispatchHelper.class, dispatchHelperObject); 
+0

我還沒有機會確認你的答案,但它看起來不錯。我會根據Miguel的回答接受你的回答,並在稍後檢查。謝謝! – 2011-02-16 20:26:04

相關問題