From: Nick Burch Date: Tue, 3 Aug 2010 17:12:02 +0000 (+0000) Subject: Fix bug #49694 - Use DataFormatter when autosizing columns, to better match the real... X-Git-Tag: REL_3_7_BETA2~8 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=7c0f62f764f06ce10768c37bd9837de7153b95e6;p=poi.git Fix bug #49694 - Use DataFormatter when autosizing columns, to better match the real display width of formatted cells git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@981969 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 840486d617..b554d8cea5 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 49694 - Use DataFormatter when autosizing columns, to better match the real display width of formatted cells 49441 - Allow overriding and guessing of HSMF non-unicode string encodings 49689 - Allow the setting of user style names on newly created HSSF cell styles Make it easier to tell which content types each POIXMLTextExtractor handles diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java index 0d584fda81..33af66680e 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java @@ -1765,6 +1765,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet { 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 org.apache.poi.ss.usermodel.Sheet { } 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()); diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java index 8420c15a8c..cf9d7bba57 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java @@ -45,6 +45,7 @@ import org.apache.poi.ss.usermodel.BaseTestSheet; 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 BaseTestSheet { 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