Bug 49694 - autoSizeColumn does not respect dates in HSSF
Summary: autoSizeColumn does not respect dates in HSSF
Status: RESOLVED FIXED
Alias: None
Product: POI
Classification: Unclassified
Component: HSSF (show other bugs)
Version: 3.6-FINAL
Hardware: PC All
: P2 minor (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-08-03 12:29 UTC by Christoph
Modified: 2010-08-03 13:13 UTC (History)
0 users



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Christoph 2010-08-03 12:29:55 UTC
The method sheet.autoSizeColumn for HSSF does not respect formats for dates. It rather tries to format the date as a numeric, fails and falls back to a very simple to-string-transformation:

try {
  NumberFormat fmt;
  if ("General".equals(format))
    sval = "" + value;
  else
  {
    // --> fails, because the format is no Decimal Format
    fmt = new DecimalFormat(format);
    sval = fmt.format(value);
  }
} catch (Exception e) {
  sval = "" + value;
}

Hence, the width for date cells is not calculated corretly. 

Sample Code ist here:
//-----------------------------------
Workbook workBook = new HSSFWorkbook();
Sheet sheet = workBook.createSheet("First Sheet");

Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue(new GregorianCalendar(2010, 7, 3));
DataFormat dataFormat = workBook.getCreationHelper().createDataFormat();
CellStyle cellStyle = workBook.createCellStyle();
cellStyle.setDataFormat(dataFormat.getFormat("dd.mm.yyyy"));
cell.setCellStyle(cellStyle);

row = sheet.createRow(1);
cell = row.createCell(0);
cell.setCellValue("small");

sheet.autoSizeColumn(0);

workBook.write(outputStream);
//-----------------------------------
Comment 1 Nick Burch 2010-08-03 13:13:07 UTC
HSSFDataFormatter now looks to be up to the job of correctly formatting the cells for an excel-like display

I've updated HSSFSheet to use this, in r981969.