我在.NET C#應用程序中有一個函數,該函數將數據從DataGridView
導出爲CSV
文件,並使用Excel
打開文件。啓動openoffice calc並打開csv文件
有沒有辦法使用OpenOffice Calc
來代替? 也就是說 如何從我的代碼開始Calc
應用程序並指向我所做的CSV
文件?
我在.NET C#應用程序中有一個函數,該函數將數據從DataGridView
導出爲CSV
文件,並使用Excel
打開文件。啓動openoffice calc並打開csv文件
有沒有辦法使用OpenOffice Calc
來代替? 也就是說 如何從我的代碼開始Calc
應用程序並指向我所做的CSV
文件?
我發現了一篇文章給你
http://www.opendocument4all.com/download/OpenOffice.net.pdf
而這個代碼給你一個想法;
XStorable2 xs = (XStorable2)mxDocument;
xs.storeToURL("file:///C:/oo.xls", new unoidl.com.sun.star.beans.PropertyValue[0]);
我也發現了這個鏈接,你
Creating an OpenOffice Writer Document with C#
Creating an OpenOffice Calc Document with C#
編輯:
using System;
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;
XComponentContext oStrap = uno.util.Bootstrap.bootstrap();
XMultiServiceFactory oServMan = (XmultiServiceFactory) oStrap.getServiceManager();
XComponentLoader oDesk = (XComponentLoader) oServMan.createInstance("com.sun.star.frame.Desktop");
string url = @"private:factory/swriter";
PropertyValue[] propVals = new PropertyValue[0];
XComponent oDoc = oDesk.loadComponentFromURL(url, "_blank", 0, propVals);
string docText = "This will be my first paragraph.\n\r";
docText += "This will be my second paragraph.\n\r";
And then this is written to the body of the document:
((XTextDocument)oDoc).getText().setString(docText);
string fileName = @"C:\Reports\test.odt";
fileName = "file:///" + fileName.Replace(@"\", "/");
And then the file is saved to disk:
((XStorable)oDoc).storeAsURL(fileName, propVals);
((Xcomponent)oDoc).dispose();
Soner Gonul解釋瞭如何輸出到CSV
。
要打開的OpenOffice Calc的文件剛剛拿到的OpenOffice Calc的可執行文件的路徑,並與您的文件路徑中的參數
Process.Start("pathToYourOpenOfficeCalc.exe","pathToYourCSVFile");
這就是啓動它。