Bug 45805

Summary: Error in methode sheet.getColumnWidth(0)
Product: POI Reporter: Thorsten <thorstenbux>
Component: HSSFAssignee: POI Developers List <dev>
Severity: normal    
Priority: P2    
Version: 3.0-dev   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Attachments: sheet.getColumnWidth(0) returns negative value

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