From: Javen O'Neal Date: Tue, 19 Sep 2017 03:26:20 +0000 (+0000) Subject: bug 61454: add column width to example Spreadsheet ToHtml; thanks to Christian Froehler X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=7a52b8c93bebc4800f4a1b1131dec705cdb7746c;p=poi.git bug 61454: add column width to example Spreadsheet ToHtml; thanks to Christian Froehler git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1808816 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/examples/src/org/apache/poi/ss/examples/html/ToHtml.java b/src/examples/src/org/apache/poi/ss/examples/html/ToHtml.java index 5f77d02ead..1034a3b2f3 100644 --- a/src/examples/src/org/apache/poi/ss/examples/html/ToHtml.java +++ b/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,61 @@ 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 = widthToPixels((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 ; + } + + /** + * Probably platform-specific, but appears to be a close approximation on some systems + * @param widthUnits POI's native width unit (twips) + * @return the approximate number of pixels for a typical display + */ + protected int widthToPixels(final double widthUnits) { + 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); } } @@ -462,4 +511,4 @@ public class ToHtml { } return ""; } -} \ No newline at end of file +} diff --git a/src/examples/src/org/apache/poi/ss/examples/html/excelStyle.css b/src/examples/src/org/apache/poi/ss/examples/html/excelStyle.css index 1083b637a3..db2c526f63 100644 --- a/src/examples/src/org/apache/poi/ss/examples/html/excelStyle.css +++ b/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 {