DataTableUtils { public static List<DataTable> mergeTablesSpanningMultiplePages(List<DataTable> dataTables) { List<DataTable> results = new ArrayList<>(); DataTable current = null; for (DataTable dt : dataTables) { if (current != null) { if (current.hasSameHeaderBlanksIgnoredAs(dt)) { addBlankColumnsToMatchHeaders(current, dt); } if (current.hasSameColumnCountAs(dt)) { current = current.mergeWith(dt); } else { results.add(current); current = dt; } } else { current = dt; } } if (current != null) { results.add(current); } return results; } private  DataTableUtils(); static List<DataTable> mergeTablesSpanningMultiplePages(List<DataTable> dataTables); static List<DataTable> mergeComplementaryColumns(List<DataTable> dataTables); static void addBlankColumnsToMatchHeaders(DataTable a, DataTable b); static boolean areComplementary(List<String> column1, List<String> column2);  }
@Test public void mergeWithAccountingBlankHeaders_scenario1() { DataTable dt = new DataTable(1) .addRow("H1", "H2", "H3") .addRow("A1", "A2", "A3"); DataTable dt2 = new DataTable(3) .addRow("H1", " ", "", "H2", "H3") .addRow("C1", "CX", "CY", "C2", "C3"); List<DataTable> mergedList = DataTableUtils.mergeTablesSpanningMultiplePages(Arrays.asList(dt, dt2)); assertThat(mergedList.size(), is(1)); DataTable merged = mergedList.get(0); assertThat(merged.toString(), is("\n" + "+--------------+\n" + "|H1| | |H2|H3|\n" + "+--------------+\n" + "|A1| | |A2|A3|\n" + "+--------------+\n" + "|C1|CX|CY|C2|C3|\n" + "+--------------+\n")); }
@Test public void mergeWithAccountingBlankHeaders_scenario2() { DataTable dt = new DataTable(1) .addRow("H1", "", "H2", "H3") .addRow("A1", "AX", "A2", "A3"); DataTable dt2 = new DataTable(3) .addRow("H1", "H2", "", "H3") .addRow("C1", "C2", "CX", "C3"); List<DataTable> mergedList = DataTableUtils.mergeTablesSpanningMultiplePages(Arrays.asList(dt, dt2)); assertThat(mergedList.size(), is(1)); DataTable merged = mergedList.get(0); assertThat(merged.toString(), is("\n" + "+--------------+\n" + "|H1| |H2| |H3|\n" + "+--------------+\n" + "|A1|AX|A2| |A3|\n" + "+--------------+\n" + "|C1| |C2|CX|C3|\n" + "+--------------+\n")); }