我的.NET C#应用程序中有一个函数,用于将数据从DataGridView
导出到CSV
文件,并使用Excel
打开文件。
有没有办法使用OpenOffice Calc
代替?
即
如何从我的代码中启动Calc
应用程序并指向我所创建的CSV
文件?
答案 0 :(得分:0)
我找到了一篇文章
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();
答案 1 :(得分:0)
Soner Gonul 向您解释了如何导出到CSV
。
要使用OpenOffice Calc打开文件,只需获取OpenOffice Calc可执行文件的路径,然后在参数中使用文件路径启动它
Process.Start("pathToYourOpenOfficeCalc.exe","pathToYourCSVFile");
这就是全部。