View | Details | Raw Unified | Return to bug 61454
Collapse All | Expand All

(-)a/src/examples/src/org/apache/poi/ss/examples/html/ToHtml.java (-5 / +58 lines)
Lines 30-35 import java.util.HashSet; Link Here
30
import java.util.Iterator;
30
import java.util.Iterator;
31
import java.util.Map;
31
import java.util.Map;
32
import java.util.Set;
32
import java.util.Set;
33
import java.util.TreeMap;
33
34
34
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
35
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
35
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
36
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
Lines 95-100 public class ToHtml { Link Here
95
            BorderStyle.SLANTED_DASH_DOT, "dashed 2pt",
96
            BorderStyle.SLANTED_DASH_DOT, "dashed 2pt",
96
            BorderStyle.THICK, "solid 3pt",
97
            BorderStyle.THICK, "solid 3pt",
97
            BorderStyle.THIN, "dashed 1pt");
98
            BorderStyle.THIN, "dashed 1pt");
99
    
100
    private static final int IDX_TABLE_WIDTH = -2;
101
    private static final int IDX_HEADER_COL_WIDTH = -1;
102
    
98
103
99
    @SuppressWarnings({"unchecked"})
104
    @SuppressWarnings({"unchecked"})
100
    private static <K, V> Map<K, V> mapFor(Object... mapping) {
105
    private static <K, V> Map<K, V> mapFor(Object... mapping) {
Lines 355-371 public class ToHtml { Link Here
355
360
356
    public void printSheet(Sheet sheet) {
361
    public void printSheet(Sheet sheet) {
357
        ensureOut();
362
        ensureOut();
358
        out.format("<table class=%s>%n", DEFAULTS_CLASS);
363
        Map<Integer, Integer> widths = computeWidths(sheet);
359
        printCols(sheet);
364
        int tableWidth = widths.get(IDX_TABLE_WIDTH); 
365
        out.format("<table class=%s style=\"width:%dpx;\">%n", DEFAULTS_CLASS, tableWidth);
366
        printCols(widths);
360
        printSheetContent(sheet);
367
        printSheetContent(sheet);
361
        out.format("</table>%n");
368
        out.format("</table>%n");
362
    }
369
    }
370
    
371
    /**
372
     * computes the column widths, defined by the sheet. 
373
     * 
374
     * @param sheet
375
     * @return Map with key: column index; value: column width in pixels
376
     *     <br>special keys: 
377
     *     <br>{@link #IDX_HEADER_COL_WIDTH} - width of the header column
378
     *     <br>{@link #IDX_TABLE_WIDTH} - width of the entire table 
379
     */
380
    private Map<Integer, Integer> computeWidths(Sheet sheet) {
381
        Map<Integer, Integer> ret = new TreeMap<Integer, Integer>();
382
        int tableWidth = 0;
363
383
364
    private void printCols(Sheet sheet) {
365
        out.format("<col/>%n");
366
        ensureColumnBounds(sheet);
384
        ensureColumnBounds(sheet);
385
        
386
        // compute width of the header column
387
        int lastRowNum = sheet.getLastRowNum();
388
        int headerCharCount = String.valueOf(lastRowNum).length();
389
        int headerColWidth = poiWidthToPixels((headerCharCount + 1) * 256);
390
        ret.put(IDX_HEADER_COL_WIDTH, headerColWidth);
391
        tableWidth += headerColWidth;
392
393
        for (int i = firstColumn; i < endColumn; i++) {
394
            int colWidth = poiWidthToPixels(sheet.getColumnWidth(i));
395
            ret.put(i, colWidth);
396
            tableWidth += colWidth;
397
        }
398
        
399
        ret.put(IDX_TABLE_WIDTH, tableWidth);
400
        return ret ;
401
    }
402
    
403
    /**
404
     * Taken from: {@link https://stackoverflow.com/questions/22259731/apache-poi-width-calculations}
405
     * @param widthUnits
406
     * @return
407
     */
408
    private int poiWidthToPixels(final double widthUnits) {
409
        if (widthUnits <= 256) {
410
            return (int) Math.round((widthUnits / 28));
411
        } else {
412
            return (int) (Math.round(widthUnits * 9 / 256));
413
        }
414
    }
415
416
    private void printCols(Map<Integer, Integer> widths) {
417
        int headerColWidth = widths.get(IDX_HEADER_COL_WIDTH);
418
        out.format("<col style=\"width:%dpx\"/>%n", headerColWidth);
367
        for (int i = firstColumn; i < endColumn; i++) {
419
        for (int i = firstColumn; i < endColumn; i++) {
368
            out.format("<col/>%n");
420
            int colWidth = widths.get(i);
421
            out.format("<col style=\"width:%dpx;\"/>%n", colWidth);
369
        }
422
        }
370
    }
423
    }
371
424
(-)a/src/examples/src/org/apache/poi/ss/examples/html/excelStyle.css (-3 / +4 lines)
Lines 30-36 Link Here
30
	text-indent: 0;
30
	text-indent: 0;
31
	letter-spacing: 0;
31
	letter-spacing: 0;
32
	word-spacing: 0;
32
	word-spacing: 0;
33
	white-space: normal;
33
	white-space: pre-wrap;
34
	unicode-bidi: normal;
34
	unicode-bidi: normal;
35
	vertical-align: 0;
35
	vertical-align: 0;
36
	background-image: none;
36
	background-image: none;
Lines 40-46 Link Here
40
	padding: 0;
40
	padding: 0;
41
	margin: 0;
41
	margin: 0;
42
	border-collapse: collapse;
42
	border-collapse: collapse;
43
	white-space: pre;
44
	vertical-align: bottom;
43
	vertical-align: bottom;
45
	font-style: normal;
44
	font-style: normal;
46
	font-family: sans-serif;
45
	font-family: sans-serif;
Lines 48-53 Link Here
48
	font-weight: normal;
47
	font-weight: normal;
49
	font-size: 10pt;
48
	font-size: 10pt;
50
	text-align: right;
49
	text-align: right;
50
	table-layout: fixed;
51
    word-wrap: break-word;
52
    overflow-wrap: break-word;
51
}
53
}
52
54
53
.excelDefaults td {
55
.excelDefaults td {
54
- 

Return to bug 61454