@@ -, +, @@ width and long cell content breaks html layout --- .../org/apache/poi/ss/examples/html/ToHtml.java | 63 ++++++++++++++++++++-- .../org/apache/poi/ss/examples/html/excelStyle.css | 6 ++- 2 files changed, 62 insertions(+), 7 deletions(-) --- a/src/examples/src/org/apache/poi/ss/examples/html/ToHtml.java +++ a/src/examples/src/org/apache/poi/ss/examples/html/ToHtml.java @@ -30,6 +30,7 @@ import java.util.HashSet; import java.util.Iterator; import java.util.Map; import java.util.Set; +import java.util.TreeMap; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; @@ -95,6 +96,10 @@ public class ToHtml { BorderStyle.SLANTED_DASH_DOT, "dashed 2pt", BorderStyle.THICK, "solid 3pt", BorderStyle.THIN, "dashed 1pt"); + + private static final int IDX_TABLE_WIDTH = -2; + private static final int IDX_HEADER_COL_WIDTH = -1; + @SuppressWarnings({"unchecked"}) private static Map mapFor(Object... mapping) { @@ -355,17 +360,65 @@ public class ToHtml { public void printSheet(Sheet sheet) { ensureOut(); - out.format("%n", DEFAULTS_CLASS); - printCols(sheet); + Map widths = computeWidths(sheet); + int tableWidth = widths.get(IDX_TABLE_WIDTH); + out.format("
%n", DEFAULTS_CLASS, tableWidth); + printCols(widths); printSheetContent(sheet); out.format("
%n"); } + + /** + * computes the column widths, defined by the sheet. + * + * @param sheet + * @return Map with key: column index; value: column width in pixels + *
special keys: + *
{@link #IDX_HEADER_COL_WIDTH} - width of the header column + *
{@link #IDX_TABLE_WIDTH} - width of the entire table + */ + private Map computeWidths(Sheet sheet) { + Map ret = new TreeMap(); + int tableWidth = 0; - private void printCols(Sheet sheet) { - out.format("%n"); ensureColumnBounds(sheet); + + // compute width of the header column + int lastRowNum = sheet.getLastRowNum(); + int headerCharCount = String.valueOf(lastRowNum).length(); + int headerColWidth = poiWidthToPixels((headerCharCount + 1) * 256); + ret.put(IDX_HEADER_COL_WIDTH, headerColWidth); + tableWidth += headerColWidth; + + for (int i = firstColumn; i < endColumn; i++) { + int colWidth = poiWidthToPixels(sheet.getColumnWidth(i)); + ret.put(i, colWidth); + tableWidth += colWidth; + } + + ret.put(IDX_TABLE_WIDTH, tableWidth); + return ret ; + } + + /** + * Taken from: {@link https://stackoverflow.com/questions/22259731/apache-poi-width-calculations} + * @param widthUnits + * @return + */ + private int poiWidthToPixels(final double widthUnits) { + if (widthUnits <= 256) { + return (int) Math.round((widthUnits / 28)); + } else { + return (int) (Math.round(widthUnits * 9 / 256)); + } + } + + private void printCols(Map widths) { + int headerColWidth = widths.get(IDX_HEADER_COL_WIDTH); + out.format("%n", headerColWidth); for (int i = firstColumn; i < endColumn; i++) { - out.format("%n"); + int colWidth = widths.get(i); + out.format("%n", colWidth); } } --- a/src/examples/src/org/apache/poi/ss/examples/html/excelStyle.css +++ a/src/examples/src/org/apache/poi/ss/examples/html/excelStyle.css @@ -30,7 +30,7 @@ text-indent: 0; letter-spacing: 0; word-spacing: 0; - white-space: normal; + white-space: pre-wrap; unicode-bidi: normal; vertical-align: 0; background-image: none; @@ -40,7 +40,6 @@ padding: 0; margin: 0; border-collapse: collapse; - white-space: pre; vertical-align: bottom; font-style: normal; font-family: sans-serif; @@ -48,6 +47,9 @@ font-weight: normal; font-size: 10pt; text-align: right; + table-layout: fixed; + word-wrap: break-word; + overflow-wrap: break-word; } .excelDefaults td { --