Bug 45805 - Error in methode sheet.getColumnWidth(0)
Summary: Error in methode sheet.getColumnWidth(0)
Alias: None
Product: POI
Classification: Unclassified
Component: HSSF (show other bugs)
Version: 3.0-dev
Hardware: PC Windows XP
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
Depends on:
Reported: 2008-09-15 04:30 UTC by Thorsten
Modified: 2008-09-16 15:08 UTC (History)
0 users

sheet.getColumnWidth(0) returns negative value (1.88 KB, application/zip)
2008-09-15 04:30 UTC, Thorsten

Note You need to log in before you can comment on or make changes to this bug.
Description Thorsten 2008-09-15 04:30:46 UTC
Created attachment 22564 [details]
sheet.getColumnWidth(0) returns negative value

On Mon, 15 Sep 2008, Thorsten Bux wrote:
> i tried to get the width of a column with the sheet.getColumnWidth(0)
> method. If the column width in excel is bigger than a specific size (i
> think bigger 127 / 890px) then the method sheet.getColumnWidht(0)
> returns a negative value.

Probably a unsigned 16 bit vs signed 16 bit issue. Probably best if you
create a new bug on bugzilla, and upload a simple test case that shows it

Comment 1 Josh Micich 2008-09-16 15:08:02 UTC
Pretty easy to reproduce this bug:

sheet.setColumnWidth((short)0, (short)40000);
assertEquals((short)40000, sheet.getColumnWidth((short)0));

Excel column widths are 16 bit unsigned values, but POI was using java shorts for that purpose.  To fix this bug (svn r696075 / r696084), several methods with this problem have been deprecated and replaced with versions that use ints.  

There is a work-around to java's lack of 16-bit unsigned shorts.  Simply convert (signed) shorts to ints by ANDing with 2^^16-1.  For example:

int width = sheet.getColumnWidth((short)0) & 0xFFFF;

However, once you have this bug-fix, the same code can be written:

int width = sheet.getColumnWidth(0);