views:

823

answers:

2

How do I import a CSV file using OpenOffice.org APIs? I want to do this using the same functionality that is also provided when I open the same file using "CVS text" filter.

+1  A: 

Here's the solution using OO.org macro language, but it should be a good place to start: http://user.services.openoffice.org/en/forum/viewtopic.php?f=45&t=12835

alxp
Despite the fact it is in a form of a macro I think I will be able to get something useful out of it. I'm just not sure what FilterOptions are for. The value of "59/9,34,0,1,1/1/1/1/1/1/1/1" is totally cryptic for me. Any pointers?
kovica
More on FilterOption for Spreadsheets on http://api.openoffice.org/docs/DevelopersGuide/Spreadsheet/Spreadsheet.xhtml#1_2_2_3_Filter_Options
kovica
+1  A: 

This is the solution that really works:

XComponentContext xLocalContext = Bootstrap.bootstrap();
XMultiComponentFactory xLocalServiceManager = xLocalContext.getServiceManager();
Object desktop = xLocalServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", xLocalContext);
XComponentLoader xComponentLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, desktop);
PropertyValue[] mypv = new PropertyValue[3];
mypv[0] = new PropertyValue();
mypv[0].Name = new String("FilterName");
mypv[0].Value = new String("Text - txt - csv (StarCalc)");
mypv[1] = new PropertyValue();
mypv[1].Name = "Hidden";
mypv[1].Value = new Boolean(false);
mypv[1] = new PropertyValue();
mypv[1].Name = "CharacterSet";
mypv[1].Value = "UTF-8";
mypv[2] = new PropertyValue();
mypv[2].Name = "FilterOptions";
mypv[2].Value = "59,34,0,1,1/1/2/1/3/5";
String internalFile = ExternalUriReferenceTranslator.create(xLocalContext).translateToInternal("file://" + csvFile.getAbsolutePath());
XComponent comp = xComponentLoader.loadComponentFromURL(internalFile, "_parent", 0, mypv);
kovica