in poi-scratchpad/src/main/java/org/apache/poi/hwpf/converter/WordToHtmlConverter.java [617:743]
protected void processTable( HWPFDocumentCore hwpfDocument, Element flow,
Table table )
{
Element tableHeader = htmlDocumentFacade.createTableHeader();
Element tableBody = htmlDocumentFacade.createTableBody();
final int[] tableCellEdges = AbstractWordUtils
.buildTableCellEdgesArray( table );
final int tableRows = table.numRows();
int maxColumns = Integer.MIN_VALUE;
for ( int r = 0; r < tableRows; r++ )
{
maxColumns = Math.max( maxColumns, table.getRow( r ).numCells() );
}
for ( int r = 0; r < tableRows; r++ )
{
TableRow tableRow = table.getRow( r );
Element tableRowElement = htmlDocumentFacade.createTableRow();
StringBuilder tableRowStyle = new StringBuilder();
WordToHtmlUtils.addTableRowProperties( tableRow, tableRowStyle );
// index of current element in tableCellEdges[]
int currentEdgeIndex = 0;
final int rowCells = tableRow.numCells();
for ( int c = 0; c < rowCells; c++ )
{
TableCell tableCell = tableRow.getCell( c );
if ( tableCell.isVerticallyMerged()
&& !tableCell.isFirstVerticallyMerged() )
{
currentEdgeIndex += getNumberColumnsSpanned(
tableCellEdges, currentEdgeIndex, tableCell );
continue;
}
Element tableCellElement;
if ( tableRow.isTableHeader() )
{
tableCellElement = htmlDocumentFacade
.createTableHeaderCell();
}
else
{
tableCellElement = htmlDocumentFacade.createTableCell();
}
StringBuilder tableCellStyle = new StringBuilder();
WordToHtmlUtils.addTableCellProperties( tableRow, tableCell,
r == 0, r == tableRows - 1, c == 0, c == rowCells - 1,
tableCellStyle );
int colSpan = getNumberColumnsSpanned( tableCellEdges,
currentEdgeIndex, tableCell );
currentEdgeIndex += colSpan;
if ( colSpan == 0 ) {
continue;
}
if ( colSpan != 1 ) {
tableCellElement.setAttribute( "colspan",
String.valueOf( colSpan ) );
}
final int rowSpan = getNumberRowsSpanned( table,
tableCellEdges, r, c, tableCell );
if ( rowSpan > 1 ) {
tableCellElement.setAttribute( "rowspan",
String.valueOf( rowSpan ) );
}
processParagraphes( hwpfDocument, tableCellElement, tableCell,
table.getTableLevel() );
if ( !tableCellElement.hasChildNodes() )
{
tableCellElement.appendChild( htmlDocumentFacade
.createParagraph() );
}
if ( tableCellStyle.length() > 0 ) {
htmlDocumentFacade.addStyleClass( tableCellElement,
tableCellElement.getTagName(),
tableCellStyle.toString() );
}
tableRowElement.appendChild( tableCellElement );
}
if ( tableRowStyle.length() > 0 ) {
tableRowElement.setAttribute( "class", htmlDocumentFacade
.getOrCreateCssClass( "r", tableRowStyle.toString() ) );
}
if ( tableRow.isTableHeader() )
{
tableHeader.appendChild( tableRowElement );
}
else
{
tableBody.appendChild( tableRowElement );
}
}
final Element tableElement = htmlDocumentFacade.createTable();
tableElement
.setAttribute(
"class",
htmlDocumentFacade
.getOrCreateCssClass( "t",
"table-layout:fixed;border-collapse:collapse;border-spacing:0;" ) );
if ( tableHeader.hasChildNodes() )
{
tableElement.appendChild( tableHeader );
}
if ( tableBody.hasChildNodes() )
{
tableElement.appendChild( tableBody );
flow.appendChild( tableElement );
}
else
{
LOG.atWarn().log("Table without body starting at [{}; {})", box(table.getStartOffset()),box(table.getEndOffset()));
}
}