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 = new DataSet();
    createConnection(dataDir);
    DataTable empresas = new DataTable(executeQuery("SELECT * from Empresas"), "Empresas");
    ds.getTables().add(empresas);
    DataTable centrosDetails = new DataTable(executeQuery("SELECT * from Centros"), "Centros");
    ds.getTables().add(centrosDetails);
    ds.getRelations().add(new DataRelation(
            "EmpresasToCentros",
            "Empresas",
            "Centros",
            new String[]{"ID_1"},
            new String[]{"ID_1"}));

    Document doc = new Document(dataDir + "plantilla.doc");
    doc.getMailMerge().executeWithRegions(ds);
    doc.save(dataDir + "Out.doc");
    assert doc.getMailMerge().getFieldNames().length == 0 : "There was a problem with mail merge"; //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 = new File(dataDir, "DB.mdb");
    String connectionString = "jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)};" +
            "DBQ=" + dataBase + "";
    mConnection = DriverManager.getConnection(connectionString);
}

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,