Free Support Forum - aspose.com

Problem executeWithRegions and Datarelation

Hi,


I have a problem with the merge, only the first table is merged. I’m working with the jdk 1.4 and my java code is:

*******
DataSet ds = new DataSet();
DataTable Empresas = new DataTable(executeQuery(“SELECT * from TABLE1”), “Empresas”);
ds.getTables().add(Empresas);
DataTable Centros = new DataTable(executeQuery(“SELECT * from TABLE2”), “Centros”);
ds.getTables().add(Centros);

DataRelation relation=new DataRelation(
“relation”,
Empresas.getTableName(),
Centros.getTableName(),
new String[]{“ID_1”},
new String[]{“ID_2”});

ds.getRelations().add(relation);

Document doc = new Document(“C://aspose//prueba//” + “plantilla.doc”);

doc.getMailMerge().executeWithRegions(ds);
doc.save(“C://aspose//prueba//” + “combinado.doc”);


And the document “plantilla.doc” is:

«TableStart:Empresas»

Empresa: «Nombre»

«TableStart:Centros» «Centro» «TableEnd:Centros»

«TableEnd:Empresas»


****************************************************************

The result of the merge in the "combinado.doc" file is:

Empresa: Abcd S.L.

efg1

hij2

Empresa: Dftu S.A.

«TableStart:Centros» «Centro» «TableEnd:Centros»

******************************************************************

Thank you very much.

Best Regards.


Hi Andoni,


Thanks for your inquiry. Could you please attach your input/output Word documents here for testing? I will investigate the issue on my side and provide you more information.

Best regards,

Hi,


Thanks for your reply, I attached the input/output Word documents.

Best regards.

Hi Andoni,


Thanks for sharing your documents with us. Please try running the following Java code on your side to be able to perform a nested mail merge operation on your template Word document. I have also created a sample database file for you (see attached DB.mdb file).
public static void main(String[] args) throws Exception
{
URI exeDir = Program.class.getResource("").toURI();
String dataDir = new File(exeDir.resolve("…/…/Data")) + File.separator;
DataSet ds <font color="BLUE">=</font> <font color="RED"><b>new</b></font> DataSet<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>

createConnection<font color="BLUE"><b>(</b></font>dataDir<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>

DataTable empresas <font color="BLUE">=</font> <font color="RED"><b>new</b></font> DataTable<font color="BLUE"><b>(</b></font>executeQuery<font color="BLUE"><b>(</b></font><font color="PURPLE">"SELECT * from Empresas"</font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>,</b></font> <font color="PURPLE">"<span style="background-color: rgb(255, 255, 0);">Empresas</span>"</font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
ds<font color="BLUE"><b>.</b></font>getTables<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>.</b></font>add<font color="BLUE"><b>(</b></font>empresas<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>

DataTable centrosDetails <font color="BLUE">=</font> <font color="RED"><b>new</b></font> DataTable<font color="BLUE"><b>(</b></font>executeQuery<font color="BLUE"><b>(</b></font><font color="PURPLE">"SELECT * from Centros"</font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>,</b></font> <font color="PURPLE">"<span style="background-color: rgb(255, 255, 0);">Centros</span>"</font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
ds<font color="BLUE"><b>.</b></font>getTables<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>.</b></font>add<font color="BLUE"><b>(</b></font>centrosDetails<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>

ds<font color="BLUE"><b>.</b></font>getRelations<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>.</b></font>add<font color="BLUE"><b>(</b></font><font color="RED"><b>new</b></font> DataRelation<font color="BLUE"><b>(</b></font>
        <font color="PURPLE">"EmpresasToCentros"</font><font color="BLUE"><b>,</b></font>
        <font color="PURPLE">"<span style="background-color: rgb(255, 255, 0);">Empresas</span>"</font><font color="BLUE"><b>,</b></font>
        <font color="PURPLE">"<span style="background-color: rgb(255, 255, 0);">Centros</span>"</font><font color="BLUE"><b>,</b></font>
        <font color="RED"><b>new</b></font> String<font color="BLUE"><b>[</b></font><font color="BLUE"><b>]</b></font><font color="BLUE"><b>{</b></font><font color="PURPLE">"<span style="background-color: rgb(255, 255, 0);">ID_1</span>"</font><font color="BLUE"><b>}</b></font><font color="BLUE"><b>,</b></font>
        <font color="RED"><b>new</b></font> String<font color="BLUE"><b>[</b></font><font color="BLUE"><b>]</b></font><font color="BLUE"><b>{</b></font><font color="PURPLE">"<span style="background-color: rgb(255, 255, 0);">ID_1</span>"</font><font color="BLUE"><b>}</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>

Document doc <font color="BLUE">=</font> <font color="RED"><b>new</b></font> Document<font color="BLUE"><b>(</b></font>dataDir <font color="BLUE">+</font> <font color="PURPLE">"plantilla.doc"</font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>

doc<font color="BLUE"><b>.</b></font>getMailMerge<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>.</b></font>executeWithRegions<font color="BLUE"><b>(</b></font>ds<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>

doc<font color="BLUE"><b>.</b></font>save<font color="BLUE"><b>(</b></font>dataDir <font color="BLUE">+</font> <font color="PURPLE">"Out.doc"</font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>

assert doc<font color="BLUE"><b>.</b></font>getMailMerge<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>.</b></font>getFieldNames<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>.</b></font>length <font color="BLUE">=</font><font color="BLUE">=</font> <font color="BROWN">0</font> <font color="BLUE">:</font> <font color="PURPLE">"There was a problem with mail merge"</font><font color="BLUE"><b>;</b></font> <font color="GREEN"><i>//ExSkip

}

protected static ResultSet executeQuery(String query) throws Exception
{
return createStatement().executeQuery(query);
}

public static void createConnection(String dataDir) throws Exception
{
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);

File dataBase <font color="BLUE">=</font> <font color="RED"><b>new</b></font> File<font color="BLUE"><b>(</b></font>dataDir<font color="BLUE"><b>,</b></font> <font color="PURPLE">"DB.mdb"</font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>

String connectionString <font color="BLUE">=</font> <font color="PURPLE">"jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)};"</font> <font color="BLUE">+</font>
        <font color="PURPLE">"DBQ="</font> <font color="BLUE">+</font> dataBase <font color="BLUE">+</font> <font color="PURPLE">""</font><font color="BLUE"><b>;</b></font>

mConnection <font color="BLUE">=</font> DriverManager<font color="BLUE"><b>.</b></font>getConnection<font color="BLUE"><b>(</b></font>connectionString<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>

}

private static Connection mConnection;

public static Statement createStatement() throws Exception
{
return mConnection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
}

I hope, this helps.

Best regards,

Hi,


It works!!! Perfect!!! Thanks!!!

My problem was in my Statement , my Statement was different than yours.

Best Regards.
Hi Andoni,

It's great you were able to find what you were looking for. Please let us know any time you have any further queries.

Best regards,