I need to generate html table from 2d array containing only data, colspan and rowspan. I have no need for margins, heights and widths. Only td and tr. I know table cell width and height, result is always rectangular. I need to do it in java but any hint is welcome.
Here is an example cell class:
public class Cell {
String label;
int colSpan, rowSpan;
public Cell(String label, int colSpan, int rowSpan) {
this.label = label;
this.colSpan = colSpan;
this.rowSpan = rowSpan;
}
}
And here is an example of 2d array containing data and rowspan/colspan information;
public class Test {
public static void main(String[] args) {
List<List<Cell>> rows = new ArrayList<>();
List<Cell> row1 = new ArrayList<>();
Cell cell11 = new Cell("total", 1, 9);
Cell cell12 = new Cell("mid_total", 1, 3);
Cell cell13 = new Cell("detail", 1, 1);
row1.add(cell11);
row1.add(cell12);
row1.add(cell13);
rows.add(row1);
List<Cell> row2 = new ArrayList<>();
Cell cell21 = new Cell("mid_total", 1, 3);
Cell cell22 = new Cell("detail", 1, 1);
row2.add(cell21);
row2.add(cell22);
rows.add(row2);
List<Cell> row3 = new ArrayList<>();
Cell cell31 = new Cell("mid_total", 1, 3);
Cell cell32 = new Cell("detail", 1, 1);
row3.add(cell31);
row3.add(cell32);
rows.add(row3);
List<Cell> row4 = new ArrayList<>();
Cell cell41 = new Cell("detail", 1, 1);
row4.add(cell41);
rows.add(row4);
List<Cell> row5 = new ArrayList<>();
Cell cell51 = new Cell("detail", 1, 1);
row5.add(cell51);
rows.add(row5);
List<Cell> row6 = new ArrayList<>();
Cell cell61 = new Cell("detail", 1, 1);
row6.add(cell61);
rows.add(row6);
List<Cell> row7 = new ArrayList<>();
Cell cell71 = new Cell("detail", 1, 1);
row7.add(cell71);
rows.add(row7);
List<Cell> row8 = new ArrayList<>();
Cell cell81 = new Cell("detail", 1, 1);
row8.add(cell81);
rows.add(row8);
List<Cell> row9 = new ArrayList<>();
Cell cell91 = new Cell("detail", 1, 1);
row9.add(cell91);
rows.add(row9);
}
}
And here's how it should look like:
+-------+-----------+--------+
| total | mid_total | detail |
+ + +--------+
| | | detail |
+ + +--------+
| | | detail |
+ +-----------+--------+
| | mid_total | detail |
+ + +--------+
| | | detail |
+ + +--------+
| | | detail |
+ +-----------+--------+
| | mid_total | detail |
+ + +--------+
| | | detail |
+ + +--------+
| | | detail |
+-------+-----------+--------+