创建一个libreoffice基于文本的数据源并使用java设置设置 [英] Create a libreoffice text-based datasource and set settings with java
问题描述
我需要在Java中创建一个LibreOffice基于文本的数据源。我的需要是递送用户一个.csv文件与一个标题行和n多个值行。此csv文件是用于执行邮件合并作业的数据源。如果我使用LibreOffice向导手动创建数据源来创建新数据库,那么邮件合并作业的执行已经完成了。
在我的环境中,我无法创建为使用应用程序的每个用户提供一个数据库。
我已经能够创建一个新的数据源,并用代码的follogwing示例注册它
XSingleServiceFactory service = UnoRuntime.queryInterface(XSingleServiceFactory.class,
msf.createInstance(com.sun.star.sdb.DatabaseContext));
对象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();
执行此代码后,我有一个名为 1.odb in:C:/ tmp /
现在在LibreOffice中注册了一个NewDataSourceName数据源。
问题是,此数据源不使用.csv文件, C:/ tmp / 。
这是因为我的java程序使用以下设置保存了datasoure:
现在我可以选择第二个复选框(逗号分隔值 - Dateien(* .csv),并将fielddelimiter从, strong>; 和 1.odb 的配置。
在创建数据源时设置设置例如
:: 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,)
有人知道我如何在java中设置此设置?
Sincerly。
所以经过多次阅读LibreOffice API,我发现了正确的方法。 >
这么简单我还是不能相信这对我有用。
这是相关的行
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,;);
现在我的.odb文件有正确的设置,我可以做邮件合并。 / p>
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.
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.
这篇关于创建一个libreoffice基于文本的数据源并使用java设置设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!