Ajitav Dutta Ajitav Dutta - 1 year ago 60
Java Question

How to create a Td with colspan attribute using wffweb?

I am writing an java application to create a HTML file which will be used a report. I am using wffweb to create the HTML document.
I am trying to programmatically create a table as mention below:

<table>
<tr>
<td> Column 1 <td>
<td> Column 2 <td>
<td> Column 3 <td>
<td> Column 3 <td>
</tr>
<tr>
<td> Column 1 <td>
<td colspan="3"> Column 2 <td>
</tr>
</table>


Is there any possible way to create a column which spans multiple column using wffweb?

Answer Source

You can use CustomAttribute class for providing colspan attribute to the td TAG. Here is the sample code

    public static void main(String[] args) {

    Html html = new Html(null) {
        Body body = new Body(this) {
            Table table = new Table(this, new CustomAttribute("border",
                    "1px")) {

                {
                    Tr tr = new Tr(this) {
                        Td td1 = new Td(this) {
                            Blank cellContent = new Blank(this, "1");
                        };
                        Td td2 = new Td(this) {
                            Blank cellContent = new Blank(this,
                                    "First Name");
                        };
                        Td td3 = new Td(this) {
                            Blank cellContent = new Blank(this, "Last Name");
                        };
                    };
                    Tr tr1 = new Tr(this) {
                        Td td1 = new Td(this, new CustomAttribute(
                                "colspan", "3")) {
                            Blank cellContent = new Blank(this,
                                    "First Name");
                        };

                    };

                }

            };
        };
    };

    html.setPrependDocType(true);
    System.out.println(html.toHtmlString());
}