• 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

How to append in excel sheet using Java

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anybody help me as how to append in excel sheet using Java. I am able to write i existing excel sheet using HSSF but is not able to append. So please suggest something regarding the sameappendexcel sheet
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

What exactly do you mean by "append in excel sheet"? You can use the HSSFWorkbook.createSheet methods to append new sheets to an Excel document. If that's not what you're looking for then please describe in more detail what you're trying to do.
 
Manisha Rana
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By append i mean that i want to add data in a new row without losing the data that already exists.
I want row to be appended at a particular row number.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's what the HSSFSheet.createRow method does.
 
Manisha Rana
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This is the code i am using and the error i am getting is

Exception in thread "main" java.lang.ClassCastException: java.lang.String
at com.excel.Write2.writeExcel(Write2.java:85)
at com.excel.Write2.main(Write2.java:139)
Can you tell me hw to rectify it.
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stacktraces are only useful if you include all the code, or if you ensure that line numbers match up. I put your code between BB [code] and [/code] tags in the hope that it would make it more readable, however since line numbers don't match up and your indentation was not good, it didn't really help.

Anyway - you should consider using JDK 5 generics and the new for loop - it will make the issues far more obvious. For example, if I change your initialization of the data to be added like so:

And I then change the method signature of writeExcel to also use generics:

Then change the loop over the collection:

Now, thanks to generics, the compiler immediately highlights your error: You are trying to cast lValue to a StringBuffer, when you originally only put Strings into your collection. Hence the error java.lang.ClassCastException: java.lang.String

Since you don't use lSqlLine in your code anywhere you could remove that line without any problems.

That will probably result in a brand new runtime problem for you:

I very much doubt that you want to be setting the cell contents to the string representation of the collection (usually will come out as just the address of the collection in memory). You probably want to be storing your lValue (which is still a String) here.

Regards, Andrew
 
Amateurs built google. Professionals built the titanic. We can't find the guy that built this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic