poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n...@apache.org
Subject svn commit: r981969 - in /poi/trunk/src: documentation/content/xdocs/status.xml java/org/apache/poi/hssf/usermodel/HSSFSheet.java testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java
Date Tue, 03 Aug 2010 17:12:03 GMT
Author: nick
Date: Tue Aug  3 17:12:02 2010
New Revision: 981969

URL: http://svn.apache.org/viewvc?rev=981969&view=rev
Log:
Fix bug #49694 - Use DataFormatter when autosizing columns, to better match the real display
width of formatted cells

Modified:
    poi/trunk/src/documentation/content/xdocs/status.xml
    poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
    poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java

Modified: poi/trunk/src/documentation/content/xdocs/status.xml
URL: http://svn.apache.org/viewvc/poi/trunk/src/documentation/content/xdocs/status.xml?rev=981969&r1=981968&r2=981969&view=diff
==============================================================================
--- poi/trunk/src/documentation/content/xdocs/status.xml (original)
+++ poi/trunk/src/documentation/content/xdocs/status.xml Tue Aug  3 17:12:02 2010
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.7-beta2" date="2010-??-??">
+           <action dev="POI-DEVELOPERS" type="fix">49694 - Use DataFormatter when autosizing
columns, to better match the real display width of formatted cells</action>
            <action dev="POI-DEVELOPERS" type="add">49441 - Allow overriding and guessing
of HSMF non-unicode string encodings</action>
            <action dev="POI-DEVELOPERS" type="fix">49689 - Allow the setting of user
style names on newly created HSSF cell styles</action>
            <action dev="POI-DEVELOPERS" type="add">Make it easier to tell which content
types each POIXMLTextExtractor handles</action>

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=981969&r1=981968&r2=981969&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 Tue Aug  3 17:12:02 2010
@@ -1765,6 +1765,7 @@ public final class HSSFSheet implements 
         FontRenderContext frc = new FontRenderContext(null, true, true);
 
         HSSFWorkbook wb = HSSFWorkbook.create(_book); // TODO - is it important to not use
_workbook?
+        HSSFDataFormatter formatter = new HSSFDataFormatter();
         HSSFFont defaultFont = wb.getFontAt((short) 0);
 
         str = new AttributedString("" + defaultChar);
@@ -1840,20 +1841,11 @@ public final class HSSFSheet implements 
             } else {
                 String sval = null;
                 if (cellType == HSSFCell.CELL_TYPE_NUMERIC) {
-                    String dfmt = style.getDataFormatString();
-                    String format = dfmt == null ? null : dfmt.replaceAll("\"", "");
-                    double value = cell.getNumericCellValue();
+                    // Try to get it formatted to look the same as excel
                     try {
-                        NumberFormat fmt;
-                        if ("General".equals(format))
-                            sval = "" + value;
-                        else
-                        {
-                            fmt = new DecimalFormat(format);
-                            sval = fmt.format(value);
-                        }
+                        sval = formatter.formatCellValue(cell);
                     } catch (Exception e) {
-                        sval = "" + value;
+                        sval = "" + cell.getNumericCellValue();
                     }
                 } else if (cellType == HSSFCell.CELL_TYPE_BOOLEAN) {
                     sval = String.valueOf(cell.getBooleanCellValue());

Modified: poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java?rev=981969&r1=981968&r2=981969&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java Tue Aug  3 17:12:02
2010
@@ -45,6 +45,7 @@ import org.apache.poi.ss.usermodel.BaseT
 import org.apache.poi.ss.usermodel.DataValidation;
 import org.apache.poi.ss.usermodel.DataValidationConstraint;
 import org.apache.poi.ss.usermodel.DataValidationHelper;
+import org.apache.poi.ss.usermodel.DateUtil;
 import org.apache.poi.ss.util.CellRangeAddress;
 import org.apache.poi.ss.util.CellRangeAddressList;
 import org.apache.poi.util.TempFile;
@@ -565,6 +566,43 @@ public final class TestHSSFSheet extends
         assertTrue(sheet3.getColumnWidth(0) >= minWithRow1And2);
         assertTrue(sheet3.getColumnWidth(0) <= maxWithRow1And2);
     }
+    
+    public void testAutoSizeDate() throws Exception {
+       HSSFWorkbook wb = new HSSFWorkbook();
+       HSSFSheet s = wb.createSheet("Sheet1");
+       HSSFRow r = s.createRow(0);
+       r.createCell(0).setCellValue(1);
+       r.createCell(1).setCellValue(123456);
+       
+       // Will be sized fairly small
+       s.autoSizeColumn((short)0);
+       s.autoSizeColumn((short)1);
+       
+       // Size ranges due to different fonts on different machines
+       assertTrue("Single number column too small", s.getColumnWidth(0) > 350); 
+       assertTrue("Single number column too big",   s.getColumnWidth(0) < 500); 
+       assertTrue("6 digit number column too small", s.getColumnWidth(1) > 1500); 
+       assertTrue("6 digit number column too big",   s.getColumnWidth(1) < 2000);
+       
+       // Set a date format
+       HSSFCellStyle cs = wb.createCellStyle();
+       HSSFDataFormat f = wb.createDataFormat();
+       cs.setDataFormat(f.getFormat("yyyy-mm-dd MMMM hh:mm:ss"));
+       r.getCell(0).setCellStyle(cs);
+       r.getCell(1).setCellStyle(cs);
+       
+       assertEquals(true, DateUtil.isCellDateFormatted(r.getCell(0)));
+       assertEquals(true, DateUtil.isCellDateFormatted(r.getCell(1)));
+       
+       // Should get much bigger now
+       s.autoSizeColumn((short)0);
+       s.autoSizeColumn((short)1);
+
+       assertTrue("Date column too small", s.getColumnWidth(0) > 4750); 
+       assertTrue("Date column too small", s.getColumnWidth(1) > 4750); 
+       assertTrue("Date column too big", s.getColumnWidth(0) < 6500); 
+       assertTrue("Date column too big", s.getColumnWidth(0) < 6500); 
+    }
 
     /**
      * Setting ForceFormulaRecalculation on sheets



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


Mime
View raw message