Page 1 of 1

Create table and filling them with dynamic content

PostPosted: Mon Oct 10, 2011 2:33 am
by Stijn
Dear Jason,

I have been using docx4j to build database report based upon data retrieved from a database. I am trying to build a table with dynamic content from the database. Because every field has a different length, every column needs to have a custom length. I have not been able to find a way to accomplish this. I have managed to get the data in the table by changing the createTable() method to accept a two dimensional array of strings as a parameter. From this two dimensional array it then reads the row and column count and uses that to create the table. I use the method setValue() to insert the value into the right cell (still within the createTable() method). The columns adjust to the right size perfectly, however when there is a white space in the string the column will not adjust the width and place the second part on a new line within this row.

To illustrate I made the following example in a monospaced font:

Code: Select all
What I want:

+-----------+----------------+-----+
|r1 c1 space|r1 c2           |r1 c3|
+-----------+----------------+-----+
|r2 c1      |r2 c2 otherspace|r2 c3|
+-----------+----------------+-----+
|r3 c1      |r3 c2           |r3 c3|
+-----------+----------------+-----+


Code: Select all
what I get:

+-----+----------+--+
|r1   |r1        |r1|
|c1   |c2        |c3|
|space|          |  |
+-----+----------+--+
|r2   |r2        |r2|
|c1   |c2        |c3|
|     |otherspace|  |
+-----+----------+--+
|r3   |r3        |r3|
|c1   |c2        |c3|
+-----+----------+--+


Where of course the rX cY stands for (row X, Column Y).

Could you please assist me with this? As you might know from my previous post I am quite new to docx4j so if you could please explain me how to do this I would very much appreciate that.

Stijn

Re: Create table and filling them with dynamic content

PostPosted: Mon Oct 10, 2011 9:31 am
by jason
You need to use a nonbreaking space (in Word, this is Ctrl-Shift space) ..

You need to use the character U+00A0 for this. (The XML will look the same in an ordinary text editor, but you can use SCUniPad to see/confirm you have a different character to a normal space which is U+0020)

Hope this helps .. Jason

Re: Create table and filling them with dynamic content

PostPosted: Wed Oct 12, 2011 12:45 am
by Stijn
Dear Jason,

I don't really understand how to do this. It reads Strings from a database and these strings need to be put into the database. How do I replace every space in a String with a non breaking one? String.replace() is not really the way to go I suppose?

Stijn