wood burning stoves*
The moose likes I/O and Streams and the fly likes How to append in excel sheet using Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "How to append in excel sheet using Java" Watch "How to append in excel sheet using Java" New topic
Author

How to append in excel sheet using Java

Manisha Rana
Greenhorn

Joined: Apr 11, 2009
Posts: 3
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
Marshal

Joined: Mar 22, 2005
Posts: 42365
    
  64
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.


Ping & DNS - my free Android networking tools app
Manisha Rana
Greenhorn

Joined: Apr 11, 2009
Posts: 3
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
Marshal

Joined: Mar 22, 2005
Posts: 42365
    
  64
That's what the HSSFSheet.createRow method does.
Manisha Rana
Greenhorn

Joined: Apr 11, 2009
Posts: 3


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

Joined: Mar 28, 2003
Posts: 11490
    
  95

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


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to append in excel sheet using Java