poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ye...@apache.org
Subject svn commit: r655282 - /poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
Date Sun, 11 May 2008 09:03:24 GMT
Author: yegor
Date: Sun May 11 02:03:24 2008
New Revision: 655282

URL: http://svn.apache.org/viewvc?rev=655282&view=rev
Log:
 Added boolean flag to turn on use of merged columns in autosize

Modified:
    poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java

Modified: poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java?rev=655282&r1=655281&r2=655282&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java Sun May 11 02:03:24 2008
@@ -1685,6 +1685,23 @@
      * @param column the column index
      */
     public void autoSizeColumn(short column) {
+    	autoSizeColumn(column, false);
+    }
+    
+    /**
+     * Adjusts the column width to fit the contents.
+     *
+     * This process can be relatively slow on large sheets, so this should
+     *  normally only be called once per column, at the end of your
+     *  processing.
+     *
+     * You can specify whether the content of merged cells should be considered or ignored.
 
+     *  Default is to ignore merged cells.
+     *   
+     * @param column the column index
+     * @param useMergedCells whether to use the contents of merged cells when calculating
the width of the column
+     */
+    public void autoSizeColumn(short column, boolean useMergedCells) {
         AttributedString str;
         TextLayout layout;
         /**
@@ -1693,13 +1710,13 @@
          * '0' looks to be a good choice.
          */
         char defaultChar = '0';
-
+       
         /**
          * This is the multiple that the font height is scaled by when determining the
          * boundary of rotated text.
          */
         double fontHeightMultiple = 2.0;
-
+       
         FontRenderContext frc = new FontRenderContext(null, true, true);
 
         HSSFWorkbook wb = new HSSFWorkbook(book);
@@ -1711,21 +1728,27 @@
         int defaultCharWidth = (int)layout.getAdvance();
 
         double width = -1;
+        rows:
         for (Iterator it = rowIterator(); it.hasNext();) {
             HSSFRow row = (HSSFRow) it.next();
             HSSFCell cell = row.getCell(column);
 
-            boolean isCellInMergedRegion = false;
-            for (int i = 0 ; i < getNumMergedRegions() && ! isCellInMergedRegion;
i++) {
-                isCellInMergedRegion = getMergedRegionAt(i).contains(row.getRowNum(), column);
-            }
+            if (cell == null) continue;
 
-            if (cell == null | isCellInMergedRegion) continue;
+            int colspan = 1;
+            for (int i = 0 ; i < getNumMergedRegions(); i++) {
+                if (getMergedRegionAt(i).contains(row.getRowNum(), column)) {
+                	if (!useMergedCells) {
+                    	// If we're not using merged cells, skip this one and move on to the
next. 
+                		continue rows;
+                	}
+                	cell = row.getCell(getMergedRegionAt(i).getColumnFrom());
+                	colspan = 1+ getMergedRegionAt(i).getColumnTo() - getMergedRegionAt(i).getColumnFrom();
+                }
+            }
 
             HSSFCellStyle style = cell.getCellStyle();
             HSSFFont font = wb.getFontAt(style.getFontIndex());
-            //the number of spaces to indent the text in the cell
-            int indention = style.getIndention();
 
             if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
                 HSSFRichTextString rt = cell.getRichStringCellValue();
@@ -1758,9 +1781,9 @@
                         trans.concatenate(
                         AffineTransform.getScaleInstance(1, fontHeightMultiple)
                         );
-                        width = Math.max(width, layout.getOutline(trans).getBounds().getWidth()
/ defaultCharWidth + indention);
+                        width = Math.max(width, ((layout.getOutline(trans).getBounds().getWidth()
/ colspan) / defaultCharWidth) + cell.getCellStyle().getIndention());
                     } else {
-                        width = Math.max(width, layout.getBounds().getWidth() / defaultCharWidth
+ indention);
+                        width = Math.max(width, ((layout.getBounds().getWidth() / colspan)
/ defaultCharWidth) + cell.getCellStyle().getIndention());
                     }
                 }
             } else {
@@ -1803,19 +1826,19 @@
                         trans.concatenate(
                         AffineTransform.getScaleInstance(1, fontHeightMultiple)
                         );
-                        width = Math.max(width, layout.getOutline(trans).getBounds().getWidth()
/ defaultCharWidth + indention);
+                        width = Math.max(width, ((layout.getOutline(trans).getBounds().getWidth()
/ colspan) / defaultCharWidth) + cell.getCellStyle().getIndention());
                     } else {
-                        width = Math.max(width, layout.getBounds().getWidth() / defaultCharWidth
+ indention);
+                        width = Math.max(width, ((layout.getBounds().getWidth() / colspan)
/ defaultCharWidth) + cell.getCellStyle().getIndention());
                     }
                 }
             }
 
-            if (width != -1) {
-                if (width > Short.MAX_VALUE) { //calculated width can be greater that
Short.MAX_VALUE!
-                     width = Short.MAX_VALUE;
-                }
-                sheet.setColumnWidth(column, (short) (width * 256));
+        }
+        if (width != -1) {
+            if (width > Short.MAX_VALUE) { //width can be bigger that Short.MAX_VALUE!
+            	width = Short.MAX_VALUE;
             }
+            sheet.setColumnWidth(column, (short) (width * 256));
         }
     }
 



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message