Two Laptop Bag
The moose likes Java in General and the fly likes Creating excel file in java. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Creating excel file in java." Watch "Creating excel file in java." New topic

Creating excel file in java.

Nitin Jawarkar
Ranch Hand

Joined: Dec 18, 2004
Posts: 79

I have created excel file using java but when i m getting data from database using resultset then it get data fine and create excel file also but now when i want to add heading to excel file also at top only then if i add that heading using resultset then it repeat for each row which must be only on top of file, my code look like this-

Connection con = DriverManager.getConnection("jdbc dbc:temp;use;pass");
Statement stmt = con.createStatement();
String sql = "select top 20 text1,text2,text3 from text";
ResultSet rs = stmt.executeQuery(sql);
String fname = rs.getString(1);
String hname = fname.substring(0,3);
String head = "text1 text2 text3";
String s1 = rs.getString(1);
String s2 = rs.getString(2);
String s3 = rs.getString(3);
String rec = s1+" "+s2+" "+s3;

where LogFile is the another class which writes the error file and excel file, i m also use the first col's first three char and current date for excel filename like "bom_20_01_2005" in the above code i m getting heading name outside the while loop but still it gives me invalid cursor state error. so can any body suggest me where should i write code for heading.


Cheers<br />Nitin
Sathvathsan Sampath
Ranch Hand

Joined: Oct 03, 2000
Posts: 96
Not very sure if this would be useful

- Sathvathsan Sampath
sandip mense

Joined: Oct 14, 2003
Posts: 28

jakarta supplies API's for creating, writing and mainiting excel sheets. You can check out the details at
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

There is also a Java Excel API available under the GNU Lesser General Public License. It provides the ability to read, write and modify Excel spreadsheets.

You can alos take a look at this thread, which discussed this topic, for some other suggestions.
Carl Trusiak

Joined: Jun 13, 2000
Posts: 3340
The error you are getting has nothing to do with Excel.

You MUST call next on a ResultSet before using it. This causes it to navigate to the first record the first time called and then each record in order afterward.

If you need the first record for this then do something like:

I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
I agree. Here's the link:
subject: Creating excel file in java.
It's not a secret anymore!