通过c#在电子表格中插入表格

Mat*_*don 8 c# openoffice-calc

我创建了一个读取不同文件的项目,然后使用电子表格将其放入不同的工作表中.我使用了Open office calc电子表格,因此使用以下代码打开一个空白文件:

public XSpreadsheet getSpreadsheet(int nIndex, XComponent xComp)
{
    XSpreadsheets xSheets = ((XSpreadsheetDocument)xComp).getSheets();
    XIndexAccess xSheetsIA = (XIndexAccess)xSheets;
    XSpreadsheet xSheet =(XSpreadsheet)xSheetsIA.getByIndex(nIndex).Value;

    return xSheet;         
}
Run Code Online (Sandbox Code Playgroud)

我打电话给一张表就像这样使用:

XSpreadsheet newSheet = getSpreadsheet(sheetIndex, xComp);
Run Code Online (Sandbox Code Playgroud)

在哪里xComp:

string filePathway = @"file:///c:/temp/blank.ods";  
PropertyValue[] propVals = new PropertyValue[0];
XComponent oCalcuDoc = oDesktop.loadComponentFromURL(filePathway, "_blank", 0, propVals);
Run Code Online (Sandbox Code Playgroud)

但是我的问题是需要使用在运行应用程序之前已经插入电子表格中所需的工作表数来设置文件blank.ods.这并不理想,因为并不总是知道所需的纸张数量.有没有办法从我的应用程序中插入工作表?

任何帮助,将不胜感激.

Las*_*sen 7

我只是简单地看了一下OpenOffice API,发现了这个:http://www.openoffice.org/api/docs/common/ref/com/sun/star/sheet/XSpreadsheets.html

..它说明了界面XSpreadsheets:

提供按名称访问电子表格以及插入,复制,删除和重新排列电子表格的方法.

它包括以下方法:

insertNewByName,根据API文档:

将新工作表插入集合中.

请参阅:http://www.openoffice.org/api/docs/common/ref/com/sun/star/sheet/XSpreadsheets.html#insertNewByName

... 等等.

我绝不是OpenOffice API专家 - 我只是简要介绍了他们的API文档 - 希望这可以指出你正确的方向.

实际上,该文档包含如何在文档中添加新工作表的示例:

 /** Inserts a new empty spreadsheet with the specified name.
 @param xDocument The XSpreadsheetDocument interface of the document.
 @param aName The name of the new sheet.
 @param nIndex The insertion index.
 @return The XSpreadsheet interface of the new sheet.
 */
 public com.sun.star.sheet.XSpreadsheet insertSpreadsheet(
     com.sun.star.sheet.XSpreadsheetDocument xDocument,
     String aName, short nIndex ) {

     // Collection of sheets
     com.sun.star.sheet.XSpreadsheets xSheets = xDocument.getSheets();
     com.sun.star.sheet.XSpreadsheet xSheet = null;

     try {
         xSheets.insertNewByName(aName, nIndex);
         xSheet = xSheets.getByName( aName );
     } catch (Exception ex) {
     }

     return xSheet;
 } 
Run Code Online (Sandbox Code Playgroud)

该示例可以在本页底部看到:http://wiki.openoffice.org/wiki/Documentation/DevGuide/Spreadsheets/Working_With_Spreadsheet_Documents