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

(-)src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java (-18 / +41 lines)
Lines 1671-1676 Link Here
1671
     * @param column the column index
1671
     * @param column the column index
1672
     */
1672
     */
1673
    public void autoSizeColumn(short column) {
1673
    public void autoSizeColumn(short column) {
1674
    	autoSizeColumn(column, false);
1675
    }
1676
    
1677
    /**
1678
     * Adjusts the column width to fit the contents.
1679
     *
1680
     * This process can be relatively slow on large sheets, so this should
1681
     *  normally only be called once per column, at the end of your
1682
     *  processing.
1683
     *
1684
     * You can specify whether the content of merged cells should be considered or ignored.  
1685
     *  Default is to ignore merged cells.
1686
     *   
1687
     * @param column the column index
1688
     * @param useMergedCells whether to use the contents of merged cells when calculating the width of the column
1689
     */
1690
    public void autoSizeColumn(short column, boolean useMergedCells) {
1674
        AttributedString str;
1691
        AttributedString str;
1675
        TextLayout layout;
1692
        TextLayout layout;
1676
        /**
1693
        /**
Lines 1679-1691 Link Here
1679
         * '0' looks to be a good choice.
1696
         * '0' looks to be a good choice.
1680
         */
1697
         */
1681
        char defaultChar = '0';
1698
        char defaultChar = '0';
1682
1699
       
1683
        /**
1700
        /**
1684
         * This is the multiple that the font height is scaled by when determining the
1701
         * This is the multiple that the font height is scaled by when determining the
1685
         * boundary of rotated text.
1702
         * boundary of rotated text.
1686
         */
1703
         */
1687
        double fontHeightMultiple = 2.0;
1704
        double fontHeightMultiple = 2.0;
1688
1705
       
1689
        FontRenderContext frc = new FontRenderContext(null, true, true);
1706
        FontRenderContext frc = new FontRenderContext(null, true, true);
1690
1707
1691
        HSSFWorkbook wb = new HSSFWorkbook(book);
1708
        HSSFWorkbook wb = new HSSFWorkbook(book);
Lines 1697-1717 Link Here
1697
        int defaultCharWidth = (int)layout.getAdvance();
1714
        int defaultCharWidth = (int)layout.getAdvance();
1698
1715
1699
        double width = -1;
1716
        double width = -1;
1717
        rows:
1700
        for (Iterator it = rowIterator(); it.hasNext();) {
1718
        for (Iterator it = rowIterator(); it.hasNext();) {
1701
            HSSFRow row = (HSSFRow) it.next();
1719
            HSSFRow row = (HSSFRow) it.next();
1702
            HSSFCell cell = row.getCell(column);
1720
            HSSFCell cell = row.getCell(column);
1703
1721
1704
            boolean isCellInMergedRegion = false;
1722
            if (cell == null) continue;
1705
            for (int i = 0 ; i < getNumMergedRegions() && ! isCellInMergedRegion; i++) {
1723
1706
                isCellInMergedRegion = getMergedRegionAt(i).contains(row.getRowNum(), column);
1724
            int colspan = 1;
1725
            for (int i = 0 ; i < getNumMergedRegions(); i++) {
1726
                if (getMergedRegionAt(i).contains(row.getRowNum(), column)) {
1727
                	if (!useMergedCells) {
1728
                    	// If we're not using merged cells, skip this one and move on to the next. 
1729
                		continue rows;
1730
                	}
1731
                	cell = row.getCell(getMergedRegionAt(i).getColumnFrom());
1732
                	colspan = 1+ getMergedRegionAt(i).getColumnTo() - getMergedRegionAt(i).getColumnFrom();
1733
                }
1707
            }
1734
            }
1708
1735
1709
            if (cell == null | isCellInMergedRegion) continue;
1710
1711
            HSSFCellStyle style = cell.getCellStyle();
1736
            HSSFCellStyle style = cell.getCellStyle();
1712
            HSSFFont font = wb.getFontAt(style.getFontIndex());
1737
            HSSFFont font = wb.getFontAt(style.getFontIndex());
1713
            //the number of spaces to indent the text in the cell
1714
            int indention = style.getIndention();
1715
1738
1716
            if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
1739
            if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
1717
                HSSFRichTextString rt = cell.getRichStringCellValue();
1740
                HSSFRichTextString rt = cell.getRichStringCellValue();
Lines 1744-1752 Link Here
1744
                        trans.concatenate(
1767
                        trans.concatenate(
1745
                        AffineTransform.getScaleInstance(1, fontHeightMultiple)
1768
                        AffineTransform.getScaleInstance(1, fontHeightMultiple)
1746
                        );
1769
                        );
1747
                        width = Math.max(width, layout.getOutline(trans).getBounds().getWidth() / defaultCharWidth + indention);
1770
                        width = Math.max(width, ((layout.getOutline(trans).getBounds().getWidth() / colspan) / defaultCharWidth) + cell.getCellStyle().getIndention());
1748
                    } else {
1771
                    } else {
1749
                        width = Math.max(width, layout.getBounds().getWidth() / defaultCharWidth + indention);
1772
                        width = Math.max(width, ((layout.getBounds().getWidth() / colspan) / defaultCharWidth) + cell.getCellStyle().getIndention());
1750
                    }
1773
                    }
1751
                }
1774
                }
1752
            } else {
1775
            } else {
Lines 1789-1807 Link Here
1789
                        trans.concatenate(
1812
                        trans.concatenate(
1790
                        AffineTransform.getScaleInstance(1, fontHeightMultiple)
1813
                        AffineTransform.getScaleInstance(1, fontHeightMultiple)
1791
                        );
1814
                        );
1792
                        width = Math.max(width, layout.getOutline(trans).getBounds().getWidth() / defaultCharWidth + indention);
1815
                        width = Math.max(width, ((layout.getOutline(trans).getBounds().getWidth() / colspan) / defaultCharWidth) + cell.getCellStyle().getIndention());
1793
                    } else {
1816
                    } else {
1794
                        width = Math.max(width, layout.getBounds().getWidth() / defaultCharWidth + indention);
1817
                        width = Math.max(width, ((layout.getBounds().getWidth() / colspan) / defaultCharWidth) + cell.getCellStyle().getIndention());
1795
                    }
1818
                    }
1796
                }
1819
                }
1797
            }
1820
            }
1798
1821
1799
            if (width != -1) {
1822
        }
1800
                if (width > Short.MAX_VALUE) { //calculated width can be greater that Short.MAX_VALUE!
1823
        if (width != -1) {
1801
                     width = Short.MAX_VALUE;
1824
            if (width > Short.MAX_VALUE) { //width can be bigger that Short.MAX_VALUE!
1802
                }
1825
            	width = Short.MAX_VALUE;
1803
                sheet.setColumnWidth(column, (short) (width * 256));
1804
            }
1826
            }
1827
            sheet.setColumnWidth(column, (short) (width * 256));
1805
        }
1828
        }
1806
    }
1829
    }
1807
1830

Return to bug 44893