ASF Bugzilla – Attachment 35254 Details for
Bug 61454
[PATCH] Example ToHtml does not respect column width and long cell content breaks html layout
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
[patch]
Proposed patch for Bug 61454 - git patch file
gitpatch_bug61454.diff (text/plain), 4.90 KB, created by
Christian Fröhler
on 2017-08-24 20:27:40 UTC
(
hide
)
Description:
Proposed patch for Bug 61454 - git patch file
Filename:
MIME Type:
Creator:
Christian Fröhler
Created:
2017-08-24 20:27:40 UTC
Size:
4.90 KB
patch
obsolete
>From ca7da58c9ffac7e098b594cf3034cacae9a0a267 Mon Sep 17 00:00:00 2001 >From: =?UTF-8?q?Christian=20Fro=CC=88hler?= <Christian.Froehler@gmx.net> >Date: Thu, 24 Aug 2017 17:24:13 -0300 >Subject: [PATCH] Bug 61454 [PATCH] Example ToHtml does not respect column > 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(-) > >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 2a7fe69..17d303e 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 <K, V> Map<K, V> mapFor(Object... mapping) { >@@ -355,17 +360,65 @@ 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 = 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<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); > } > } > >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 1083b63..ad9e419 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 { >-- >2.10.1 (Apple Git-78) >
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 61454
:
35253
| 35254 |
35255
|
35256