Author: onealj
Date: Tue Sep 19 03:26:20 2017
New Revision: 1808816
URL: http://svn.apache.org/viewvc?rev=1808816&view=rev
Log:
bug 61454: add column width to example Spreadsheet ToHtml; thanks to Christian
Froehler
Modified:
poi/trunk/src/examples/src/org/apache/poi/ss/examples/html/ToHtml.java
poi/trunk/src/examples/src/org/apache/poi/ss/examples/html/excelStyle.css
Modified: poi/trunk/src/examples/src/org/apache/poi/ss/examples/html/ToHtml.java
URL:
http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/ss/examples/html/ToHtml.java?rev=1808816&r1=1808815&r2=1808816&view=diff
==============================================================================
--- poi/trunk/src/examples/src/org/apache/poi/ss/examples/html/ToHtml.java
(original)
+++ poi/trunk/src/examples/src/org/apache/poi/ss/examples/html/ToHtml.java Tue
Sep 19 03:26:20 2017
@@ -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 <K, V> Map<K, V> mapFor(Object... mapping) {
@@ -355,17 +360,61 @@ public class ToHtml {
public void printSheet(Sheet sheet) {
ensureOut();
- out.format("<table class=%s>%n", DEFAULTS_CLASS);
- printCols(sheet);
+ Map<Integer, Integer> widths = computeWidths(sheet);
+ int tableWidth = widths.get(IDX_TABLE_WIDTH);
+ out.format("<table class=%s style=\"width:%dpx;\">%n", DEFAULTS_CLASS,
tableWidth);
+ printCols(widths);
printSheetContent(sheet);
out.format("</table>%n");
}
+
+ /**
+ * computes the column widths, defined by the sheet.
+ *
+ * @param sheet
+ * @return Map with key: column index; value: column width in pixels
+ * <br>special keys:
+ * <br>{@link #IDX_HEADER_COL_WIDTH} - width of the header column
+ * <br>{@link #IDX_TABLE_WIDTH} - width of the entire table
+ */
+ private Map<Integer, Integer> computeWidths(Sheet sheet) {
+ Map<Integer, Integer> ret = new TreeMap<Integer, Integer>();
+ int tableWidth = 0;
- private void printCols(Sheet sheet) {
- out.format("<col/>%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<Integer, Integer> widths) {
+ int headerColWidth = widths.get(IDX_HEADER_COL_WIDTH);
+ out.format("<col style=\"width:%dpx\"/>%n", headerColWidth);
for (int i = firstColumn; i < endColumn; i++) {
- out.format("<col/>%n");
+ int colWidth = widths.get(i);
+ out.format("<col style=\"width:%dpx;\"/>%n", colWidth);
}
}
@@ -462,4 +511,4 @@ public class ToHtml {
}
return "";
}
-}
\ No newline at end of file
+}
Modified:
poi/trunk/src/examples/src/org/apache/poi/ss/examples/html/excelStyle.css
URL:
http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/ss/examples/html/excelStyle.css?rev=1808816&r1=1808815&r2=1808816&view=diff
==============================================================================
--- poi/trunk/src/examples/src/org/apache/poi/ss/examples/html/excelStyle.css
(original)
+++ poi/trunk/src/examples/src/org/apache/poi/ss/examples/html/excelStyle.css
Tue Sep 19 03:26:20 2017
@@ -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 {
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]