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
This commit is contained in:
Nick Burch 2010-08-03 17:12:02 +00:00
parent ce0fea767f
commit 8486ae97d3
3 changed files with 43 additions and 12 deletions

View File

@ -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>

View File

@ -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());

View File

@ -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