启动openoffice 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");
这就是启动它。