简体   繁体   English

创建一个libreoffice基于文本的数据源并使用java设置设置

[英]Create a libreoffice text-based datasource and set settings with java

I need to create a LibreOffice Text-based-Datasource within Java. 我需要在Java中创建一个LibreOffice基于文本的数据源。 My needs are to deliver the user an .csv file with a header row and n-many value rows. 我的需求是为用户提供带有标题行和n多个值行的.csv文件。 This csv File is the datasource to execute a mail merge job. 此csv文件是执行邮件合并作业的数据源。 The execution of the mail merge job already works perfectly if I create the datasource manually with the LibreOffice wizard for creating new databases. 如果我使用LibreOffice向导手动创建数据源以创建新数据库,则邮件合并作业的执行已经完美。

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 我已经能够创建一个新的数据源并使用follogwing代码示例进行注册

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/ 在执行此代码后,我在C:/ tmp /中有一个名为1.odb的文件

Now there is also a "NewDataSourceName" Datasource registered in LibreOffice. 现在在LibreOffice中还注册了一个“NewDataSourceName”数据源。 这是新创建的数据源

The problem is that this datasource doesnt use the .csv file which is located also at C:/tmp/ . 问题是这个数据源不使用位于C:/ tmp /的.csv文件。

This is happening because my java program saved the datasoure with the following settings 发生这种情况是因为我的java程序使用以下设置保存了数据 创建的1.odb文件的设置

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. 现在我可以选择第二个复选框(逗号分隔值-Dateien(* .csv)并将fielddelimiter从“ ”更改为“ ; ”, 并且将正确配置1.odb

I googled alot and found some non-java solutions to set the settings while creating the datasource eg 我google了很多,发现一些非java解决方案来设置创建数据源的设置,例如

::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? 有人知道如何在java中设置此设置吗?

Sincerly. 诚恳。

So after multiple times of reading the LibreOffice API I found the right way to do this. 因此,在多次阅读LibreOffice API后,我发现了正确的方法。

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. 现在我的.odb文件具有正确的设置,我可以进行邮件合并。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM