开发者

Starting openoffice calc and open csv file

I have a function in my .NET C# application which export data from DataGridView to CSV file, and 开发者_开发百科opens the file with Excel.

Is there a way to do this using OpenOffice Calc instead? I.e. How can I start Calc application from my code and pointing to the CSV file I have made?


I found an article for you

http://www.opendocument4all.com/download/OpenOffice.net.pdf

And this code gives you an idea;

XStorable2 xs = (XStorable2)mxDocument;

xs.storeToURL("file:///C:/oo.xls", new unoidl.com.sun.star.beans.PropertyValue[0]);

I also found this links for you

Creating an OpenOffice Writer Document with C#

Creating an OpenOffice Calc Document with C#

C# to OpenOffice Calc

EDIT:

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 explained you how to export to CSV.

To open the file with OpenOffice Calc just get the path of the OpenOffice Calc executable and launch it with your file path in the arguments

Process.Start("pathToYourOpenOfficeCalc.exe","pathToYourCSVFile");

That's all.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜