aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorNick Burch <nick@apache.org>2010-08-03 17:12:02 +0000
committerNick Burch <nick@apache.org>2010-08-03 17:12:02 +0000
commit7c0f62f764f06ce10768c37bd9837de7153b95e6 (patch)
tree3456053335adf90e2d42be65ed60b5ebbbdc0f61 /src
parent62499bc4655a6409834d75825ccd68c1524e6775 (diff)
downloadpoi-7c0f62f764f06ce10768c37bd9837de7153b95e6.tar.gz
poi-7c0f62f764f06ce10768c37bd9837de7153b95e6.zip
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
Diffstat (limited to 'src')
-rw-r--r--src/documentation/content/xdocs/status.xml1
-rw-r--r--src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java16
-rw-r--r--src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java38
3 files changed, 43 insertions, 12 deletions
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 @@
<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>
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