• Post Reply Bookmark Topic Watch Topic
  • New Topic

Preserving Leading Zeros while exporting to excel using POI  RSS feed

 
Benjamin Samuel
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want to preserve leading zeros while exporting the data into an xls using Apache POI. If I set the data type to text, the leading zeros are getting removed on double clicking the in cell. Below is how i set the value:


please help. How can this be achieved via POI?

Thanks
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could try a little trick I always use in Excel: put a ' in front of the value. Although it will be part of the cell's actual value, Excel won't show it.
 
Benjamin Samuel
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob, Thank you for your time.

I tried with an apostrophe before the number with leading zeros. But when i open the xls it shows the cell value as '00012345. If i double click on the cell, then the value is displayed without a '

What could be the workaround for this?

From the xls that i generate programatically, the user needs to copy the value to another application. And the above scenario will copy the text along with the apostrophe(') sign.
 
Himanshu Kansal
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method setCellValue(String) is deprecated. Use setCellValue(HSSFRichText) and try. Tell if this does not work.

Regards
 
Benjamin Samuel
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Himanshu. I tried with HSSFRichTextString and the code is given below:




Couldnt attach the generated xls.

The second cell displays 000005544 and when i navigate to this cell and double click on it and come out of it, the leading zeros are lost.
The fourth cell displays '000005544 and when i double click on this one, the apostrophe is gone and the leading zeros are retained.

The problem is: The user is going to copy the values from the xls i have generated. While copying only the number is expected to be copied. The user may/may not double click on the cells in the xls generated. How can I achieve this? please help.

Thanks
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried using the createCell method that takes two parameters, and which allows you to specify the cell type?

you should use the createCell methods that take int parameters, by the way - the short varieties are deprecated.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!