How do I get table information in word2003 using POI?

86 Views Asked by At

I try to get the width of each cell and then calculate their greatest common divisor to get the colspan of each cell, but how do I get the cell Rowspan? I tried using the method TableRow.getRowHeight(), but no matter what the table looks like, it's always going to be 0. I am trying to use this method to convert to HTML, if you have a better way to convert complex tables to HTML, please help me, thanks!

1

There are 1 best solutions below

1
Hanliu On
public static Integer getRowspan(TableCell cell, int rowNum, int colNum, Table table) {
    int rowSpan = 0;
    if (cell.isVerticallyMerged()) {
        if (cell.isFirstVerticallyMerged()) {
            for (int i = rowNum + 1; i < table.numRows(); i++) {
                TableCell tableCell = table.getRow(i).getCell(colNum);
                if (tableCell.isFirstVerticallyMerged() || !tableCell.isVerticallyMerged()) {
                    rowSpan = i - rowNum;
                    break;
                } else if (i == table.numRows() - 1) {
                    rowSpan = i - rowNum + 1;
                }
            }
        }
    }
    return rowSpan;
}


public static Integer getColspan(TableCell cell, int rowNum, int colNum, Table table) {
    int colSpan = 0;
    if (cell.isMerged()) {
        if (cell.isFirstMerged()) {
            for (int i = colNum + 1; i < table.numRows(); i++) {
                TableCell tableCell = table.getRow(rowNum).getCell(i);
                if (tableCell.isFirstMerged() || !tableCell.isMerged()) {
                    colSpan = i - colNum;
                    break;
                } else if (i == table.numRows() - 1) {
                    colSpan = i - colNum + 1;
                }
            }
        }
    }
    return colSpan;
}