问题
I need to create a LibreOffice Text-based-Datasource within Java. My needs are to deliver the user an .csv file with a header row and n-many value rows. This csv File is the datasource to execute a mail merge job. The execution of the mail merge job already works perfectly if I create the datasource manually with the LibreOffice wizard for creating new databases.
In my environment I'm not able to create a database for every user who use the application.
I'm already able to create a new datasource and register it with the follogwing samples of code
XSingleServiceFactory service = UnoRuntime.queryInterface(XSingleServiceFactory.class,
msf.createInstance("com.sun.star.sdb.DatabaseContext"));
Object datasourceObject = service.createInstance();
XNamingService namingService = UnoRuntime.queryInterface(XNamingService.class, service);
XDocumentDataSource datasource = UnoRuntime.queryInterface(XDocumentDataSource.class, datasourceObject);
XStorable store = (XStorable) UnoRuntime.queryInterface(XStorable.class, datasource.getDatabaseDocument());
XModel model = UnoRuntime.queryInterface(XModel.class, datasource.getDatabaseDocument());
store.storeAsURL("file:///C:/tmp/1.odb", model.getArgs());
namingService.registerObject("NewDataSourceName", datasource);
XPropertySet datasourceProperties = UnoRuntime.queryInterface(XPropertySet.class, datasource);
csvPath = "C:/tmp/";
datasourceProperties.setPropertyValue("URL", "sdbc:flat:" + csvPath);
store.store();
After the execution of this code I have a file named 1.odb in C:/tmp/
Now there is also a "NewDataSourceName" Datasource registered in LibreOffice.
The problem is that this datasource doesnt use the .csv file which is located also at C:/tmp/.
This is happening because my java program saved the datasoure with the following settings
Now I could select the 2nd checkbox (Comma separated value-Dateien (*.csv) and change the fielddelimiter from "," to ";" and the 1.odb would be correctly configured.
I googled alot and found some non-java solutions to set the settings while creating the datasource e.g.
::odbSource:Settings:setPropertyValue("Extension" , "csv")
::odbSource:Settings:setPropertyValue("HeaderLine" , TRUE)
::odbSource:Settings:setPropertyValue("FieldDelimiter" , ";")
::odbSource:Settings:setPropertyValue("StringDelimiter" , '"')
::odbSource:Settings:setPropertyValue("DecimalDelimiter" , ".")
::odbSource:Settings:setPropertyValue("ThousandDelimiter", "")
Do someone know how I can set this settings within java?
Sincerly.
回答1:
So after multiple times of reading the LibreOffice API I found the right way to do this.
It's so simple I still cant belive that this worked for me. This are the relevant lines
XSingleServiceFactory service = UnoRuntime.queryInterface(XSingleServiceFactory.class,
msf.createInstance("com.sun.star.sdb.DatabaseContext"));
Object datasourceObject = service.createInstance();
XDocumentDataSource datasourceDocument =
UnoRuntime.queryInterface(XDocumentDataSource.class, datasourceObject);
XPropertySet datasourceProperties =
UnoRuntime.queryInterface(XPropertySet.class, datasourceObject);
XPropertySet datasourceSettings =
UnoRuntime.queryInterface(XPropertySet.class,
datasourceProperties.getPropertyValue("Settings"));
datasourceSettings.setPropertyValue("Extension", "csv");
datasourceSettings.setPropertyValue("FieldDelimiter", ";");
Now my .odb file has the right settings and I'm able to do the mail merge.
来源:https://stackoverflow.com/questions/33522433/create-a-libreoffice-text-based-datasource-and-set-settings-with-java