• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to append in excel sheet using Java

 
Manisha Rana
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • 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
  • 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: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's what the HSSFSheet.createRow method does.
 
Manisha Rana
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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.
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11914
209
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic