【问题标题】:Inserting Sheets in Spreadsheet through c#通过c#在电子表格中插入表格
【发布时间】:2012-08-16 15:19:33
【问题描述】:

我创建了一个项目,该项目读取不同的文件,然后使用电子表格将其放入不同的工作表中。 我使用了 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;         
}

我把一张纸叫作这样使用:

XSpreadsheet newSheet = getSpreadsheet(sheetIndex, xComp);

xComp 在哪里:

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

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

任何帮助将不胜感激。

【问题讨论】:

    标签: c# openoffice-calc


    【解决方案1】:

    我只是简单地看了一下 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;
     } 
    

    示例见本页底部:http://wiki.openoffice.org/wiki/Documentation/DevGuide/Spreadsheets/Working_With_Spreadsheet_Documents

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-27
      • 1970-01-01
      • 2014-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-21
      相关资源
      最近更新 更多