因此,我終於解決了這個問題,並希望拯救他人再次經歷這個榛子。的HEADACE我的基本點是:
- 使用正斜槓而不是反斜線(例如其
C:/
沒有C:\
)
- 使用應設置爲用來保存文檔的引擎
Filtername
。可能的值包括writer8
,calc8
,MS Excel 97
,因此對於電子表格你顯然需要使用calc8
- 如果你不希望出現這種情況的OpenOffice中於地面彈起並等待它得到填補你的數據,然後使用
PropertyValue
和將Hidden
設置爲true
。
快樂編碼,不要忘記安裝OpenOffice的SDK,以便能夠添加UNOIDL引用:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using unoidl.com.sun.star.uno;
using unoidl.com.sun.star.lang;
using unoidl.com.sun.star.frame;
using unoidl.com.sun.star.beans;
using unoidl.com.sun.star.sheet;
using unoidl.com.sun.star.container;
using unoidl.com.sun.star.table;
using unoidl.com.sun.star.text;
namespace TimeScanner {
class ReportGenerator {
private const string fileName =
@"file:///C:/Documents and Settings/My Documents/Hours Report.ods";
//Concrete Methods
internal XComponent openCalcSheet() {
XComponentContext oStrap = uno.util.Bootstrap.bootstrap();
XMultiServiceFactory oServMan = (XMultiServiceFactory)oStrap.getServiceManager();
XComponentLoader desktop = (XComponentLoader)oServMan.createInstance("com.sun.star.frame.Desktop");
string url = @"private:factory/scalc";
PropertyValue[] loadProps = new PropertyValue[1];
loadProps[0] = new PropertyValue();
loadProps[0].Name = "Hidden";
loadProps[0].Value = new uno.Any(true);
//PropertyValue[] loadProps = new PropertyValue[0];
XComponent document = desktop.loadComponentFromURL(url, "_blank", 0, loadProps);
return document;
}
public void writeToSheet(XComponent document) {
XSpreadsheets oSheets = ((XSpreadsheetDocument)document).getSheets();
XIndexAccess oSheetsIA = (XIndexAccess) oSheets;
XSpreadsheet sheet = (XSpreadsheet) oSheetsIA.getByIndex(0).Value;
XCell cell = sheet.getCellByPosition(0, 0); //A1
((XText)cell).setString("Cost");
cell = sheet.getCellByPosition(1, 0); //B1
cell.setValue(200);
cell = sheet.getCellByPosition(1, 2); //B3
cell.setFormula("=B1 * 1.175");
}
public void saveCalcSheet(XComponent oDoc) {
PropertyValue[] propVals = new PropertyValue[1];
propVals[0] = new PropertyValue();
propVals[0].Name = "FilterName";
propVals[0].Value = new uno.Any("calc8");
((XStorable)oDoc).storeToURL(fileName, propVals);
}
}
}