@Test public void testWord1() { ReadWordTable readWordTable = new ReadWordTable(); try (InputStream inputStream = ReadWordTableTest.class.getClassLoader().getResourceAsStream("table1.docx"); XWPFDocument document = new XWPFDocument(inputStream);) { List<XWPFTable> tables = document.getTables(); tableHtml = readWordTable.readTable(tables.get(0)); expected = "<table><tr><td></td><td colspan='2'></td><td></td><td></td><td></td><td></td><td></td></tr><tr><td rowspan='25'></td><td rowspan='15'></td><td rowspan='5'></td><td></td><td rowspan='25'></td><td rowspan='25'></td><td rowspan='25'></td><td rowspan='3'></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td></td><td rowspan='2'></td></tr><tr><td></td></tr><tr><td rowspan='10'></td><td></td><td></td></tr><tr><td></td><td rowspan='4'></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td></td><td rowspan='2'></td></tr><tr><td></td></tr><tr><td></td><td rowspan='3'></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td rowspan='9'></td><td rowspan='9'></td><td></td><td></td></tr><tr><td></td><td rowspan='9'></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td colspan='2'></td><td></td></tr></table>"; assertThat(tableHtml, is(expected)); } catch (IOException e) { e.printStackTrace(); } }
public String readTable(XWPFTable table) throws IOException { int tableRowsSize = table.getRows().size(); StringBuilder tableToHtmlStr = new StringBuilder("<table>"); for (int i = 0; i < tableRowsSize; i++) { tableToHtmlStr.append("<tr>"); int tableCellsSize = table.getRow(i).getTableCells().size(); for (int j = 0; j < tableCellsSize; j++) { if (isOmitCell(i, j)) { continue; } XWPFTableCell tableCell = table.getRow(i).getCell(j); CTTcPr tcPr = tableCell.getCTTc().getTcPr(); int colspan = getColspan(tcPr); if (colspan > 1) { tableToHtmlStr.append("<td colspan='" + colspan + "'"); } else { tableToHtmlStr.append("<td"); } int rowspan = getRowspan(table, i, j); if (rowspan > 1) { tableToHtmlStr.append(" rowspan='" + rowspan + "'>"); } else { tableToHtmlStr.append(">"); } String text = tableCell.getText(); tableToHtmlStr.append(text + "</td>"); } tableToHtmlStr.append("</tr>"); } tableToHtmlStr.append("</table>"); clearTableInfo(); return tableToHtmlStr.toString(); }
ReadWordTable { public String readTable(XWPFTable table) throws IOException { int tableRowsSize = table.getRows().size(); StringBuilder tableToHtmlStr = new StringBuilder("<table>"); for (int i = 0; i < tableRowsSize; i++) { tableToHtmlStr.append("<tr>"); int tableCellsSize = table.getRow(i).getTableCells().size(); for (int j = 0; j < tableCellsSize; j++) { if (isOmitCell(i, j)) { continue; } XWPFTableCell tableCell = table.getRow(i).getCell(j); CTTcPr tcPr = tableCell.getCTTc().getTcPr(); int colspan = getColspan(tcPr); if (colspan > 1) { tableToHtmlStr.append("<td colspan='" + colspan + "'"); } else { tableToHtmlStr.append("<td"); } int rowspan = getRowspan(table, i, j); if (rowspan > 1) { tableToHtmlStr.append(" rowspan='" + rowspan + "'>"); } else { tableToHtmlStr.append(">"); } String text = tableCell.getText(); tableToHtmlStr.append(text + "</td>"); } tableToHtmlStr.append("</tr>"); } tableToHtmlStr.append("</table>"); clearTableInfo(); return tableToHtmlStr.toString(); } }
ReadWordTable { public String readTable(XWPFTable table) throws IOException { int tableRowsSize = table.getRows().size(); StringBuilder tableToHtmlStr = new StringBuilder("<table>"); for (int i = 0; i < tableRowsSize; i++) { tableToHtmlStr.append("<tr>"); int tableCellsSize = table.getRow(i).getTableCells().size(); for (int j = 0; j < tableCellsSize; j++) { if (isOmitCell(i, j)) { continue; } XWPFTableCell tableCell = table.getRow(i).getCell(j); CTTcPr tcPr = tableCell.getCTTc().getTcPr(); int colspan = getColspan(tcPr); if (colspan > 1) { tableToHtmlStr.append("<td colspan='" + colspan + "'"); } else { tableToHtmlStr.append("<td"); } int rowspan = getRowspan(table, i, j); if (rowspan > 1) { tableToHtmlStr.append(" rowspan='" + rowspan + "'>"); } else { tableToHtmlStr.append(">"); } String text = tableCell.getText(); tableToHtmlStr.append(text + "</td>"); } tableToHtmlStr.append("</tr>"); } tableToHtmlStr.append("</table>"); clearTableInfo(); return tableToHtmlStr.toString(); }  }
ReadWordTable { public String readTable(XWPFTable table) throws IOException { int tableRowsSize = table.getRows().size(); StringBuilder tableToHtmlStr = new StringBuilder("<table>"); for (int i = 0; i < tableRowsSize; i++) { tableToHtmlStr.append("<tr>"); int tableCellsSize = table.getRow(i).getTableCells().size(); for (int j = 0; j < tableCellsSize; j++) { if (isOmitCell(i, j)) { continue; } XWPFTableCell tableCell = table.getRow(i).getCell(j); CTTcPr tcPr = tableCell.getCTTc().getTcPr(); int colspan = getColspan(tcPr); if (colspan > 1) { tableToHtmlStr.append("<td colspan='" + colspan + "'"); } else { tableToHtmlStr.append("<td"); } int rowspan = getRowspan(table, i, j); if (rowspan > 1) { tableToHtmlStr.append(" rowspan='" + rowspan + "'>"); } else { tableToHtmlStr.append(">"); } String text = tableCell.getText(); tableToHtmlStr.append(text + "</td>"); } tableToHtmlStr.append("</tr>"); } tableToHtmlStr.append("</table>"); clearTableInfo(); return tableToHtmlStr.toString(); }  String generateOmitCellStr(int row, int col); int getColspan(CTTcPr tcPr); int getRowspan(XWPFTable table, int row, int col); boolean isRestartRow(XWPFTableCell tableCell); boolean isContinueRow(XWPFTableCell tableCell); int getLeftWidth(XWPFTable table, int row, int col); int getCellWidth(XWPFTable table, int row, int col); void addOmitCell(int row, int col); boolean isOmitCell(int row, int col); String readTable(XWPFTable table); void clearTableInfo(); static void main(String[] args); }
ReadWordTable { public String readTable(XWPFTable table) throws IOException { int tableRowsSize = table.getRows().size(); StringBuilder tableToHtmlStr = new StringBuilder("<table>"); for (int i = 0; i < tableRowsSize; i++) { tableToHtmlStr.append("<tr>"); int tableCellsSize = table.getRow(i).getTableCells().size(); for (int j = 0; j < tableCellsSize; j++) { if (isOmitCell(i, j)) { continue; } XWPFTableCell tableCell = table.getRow(i).getCell(j); CTTcPr tcPr = tableCell.getCTTc().getTcPr(); int colspan = getColspan(tcPr); if (colspan > 1) { tableToHtmlStr.append("<td colspan='" + colspan + "'"); } else { tableToHtmlStr.append("<td"); } int rowspan = getRowspan(table, i, j); if (rowspan > 1) { tableToHtmlStr.append(" rowspan='" + rowspan + "'>"); } else { tableToHtmlStr.append(">"); } String text = tableCell.getText(); tableToHtmlStr.append(text + "</td>"); } tableToHtmlStr.append("</tr>"); } tableToHtmlStr.append("</table>"); clearTableInfo(); return tableToHtmlStr.toString(); }  String generateOmitCellStr(int row, int col); int getColspan(CTTcPr tcPr); int getRowspan(XWPFTable table, int row, int col); boolean isRestartRow(XWPFTableCell tableCell); boolean isContinueRow(XWPFTableCell tableCell); int getLeftWidth(XWPFTable table, int row, int col); int getCellWidth(XWPFTable table, int row, int col); void addOmitCell(int row, int col); boolean isOmitCell(int row, int col); String readTable(XWPFTable table); void clearTableInfo(); static void main(String[] args);  }