• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Preserving Leading Zeros while exporting to excel using POI

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22787
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic