Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-24-2008, 05:00 PM
Member
 
Join Date: Jan 2008
Posts: 3
chandpuri is on a distinguished road
SQL Query resultset into Excel Sheet
Dear all,
I am very new to java and am writing a program to put the resultset from an SQL query into an excel sheet. I already have downloaded the library jexcelapi library.
Please let me know how could I proceed further on this. It would be great if someone could give me a sample code to see.
Thanks a ton.

Cheers
Chandpuri
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-01-2008, 06:33 PM
Member
 
Join Date: Jan 2008
Posts: 3
chandpuri is on a distinguished road
Any one any clue?
Hi guys,
I dont believe that no one has any clue on writing recordsets into excel from SQL using Java.
I thought there were good knowledgable people.
Cmon guys

Best Regards,
G
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-01-2008, 08:08 PM
jelly's Avatar
Member
 
Join Date: Jan 2008
Location: Somerset, UK
Posts: 46
jelly is on a distinguished road
If you have downloaded JExcelApi then there are whole set of demos included, try working your way through the write one - it is comprehensive. Be aware that JExcelApi does not support xslx files i.e. the latest version of excel. If you simply want to get data from a result set into excel then create a .csv file, make the first row your column headers and and each other row the data; excel will open it drectly and convert it on the fly. For example a result with two rows, each row has three columns - age, town, country . Create a csv file that looks something like the following from the data:

Code:
age, town, country 42, london, uk 19, glasgow, scotland
__________________
-- Hope that helps
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 03-27-2008, 12:06 AM
Member
 
Join Date: Mar 2008
Posts: 1
ad1234 is on a distinguished road
Did u made any progress ?

I am working on same thing and thought if you can guide !
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 05-25-2008, 02:40 AM
Member
 
Join Date: May 2008
Posts: 2
Tony Sidaway is on a distinguished road
Quote:
Originally Posted by chandpuri View Post
Dear all,
I am very new to java and am writing a program to put the resultset from an SQL query into an excel sheet. I already have downloaded the library jexcelapi library.
Please let me know how could I proceed further on this. It would be great if someone could give me a sample code to see.
Thanks a ton.

Cheers
Chandpuri
There is a tutorial page for jexcelapi linked from their sourceforge page.

I'm not familiar with jexcelapi, but the basics of writing a ResultSet to any other tabular medium are pretty straightforward. To access the ResultSet, use boolean ResultSet.next(), and the appropriate getter method for the column information. To write to the worksheet, use whatever setter is most appropriate to the data type. Really it's just a couple of nested for loops.

To determine the data types of the ResultSet's columns, use ResultSet.getMetaData().getColumnType(int) (the return value is an int whose interpretation is defined in java.sql.Types)

Recently I wrote some code to write Excel 2007 (OpenXML) workbooks from a database, and after researching the available software I decided to write it myself from scratch. There are a couple of good powerpoint presentations on Microsoft's website explaining how to write a workbook file in a form that is acceptable to Excel 2007. The format is very simple and also comforms to a published international standard, so unless you need to write Excel files that can be interpreted by earlier versions of Excel I recommend this approach.

After writing the peripheral code to produce the workbook the strategy I chose was to write an interface for a worksheet and implement it in the most basic manner, in an abstract class BasicWorksheet, that knows how to write the XML using the "nested for loops" logic, from any implementation of the interface.

The upshot was that I was then able to write a subclass, ResultSetWorksheet, that implemented the interface using the methods provided by ResultSet and ResultSetMetaData. Immediately it was able to write a worksheet from a ResultSet.

One thing I was careful about in implementing the abstract class was to avoid relying on the code that writes the worksheet file knowing the number of rows in the worksheet. One facet of a ResultSet is that one does not always know how many rows it contains until after one has scanned every row of data. By writing carefully avoiding references to the number of rows, I was able to produce worksheet files from a single SQL select statement. This kind of "single pass" design can be important for performance. The logic tests to see if the number of rows is known, and if not it omits the optional "dimension" element (which must precede the sheetData element if it is present) from the XML. An alternative approach would be to assemble the worksheet as a two-dimensional array in memory prior to writing it out.

Still another approach would be to model the spreadsheet as an updateable ResultSet, and use a jdbc driver for Excel files. One example of this, which I haven't investigated, is apparently xlsql, also hosted on sourceforge.

Last edited by Tony Sidaway : 05-25-2008 at 04:14 PM. Reason: clarification
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 06-05-2008, 05:07 AM
Member
 
Join Date: May 2008
Posts: 2
Tony Sidaway is on a distinguished road
Since writing the Excel code, I've been investigating a more general solution for OpenXML in Java, called OpenXML4J. It's available under either the Apache v2 license or a BSD license, and it will handle all details of packaging and unpackaging OpenXML (Office 2007) files. In my opinion it's a bit on the heavy side for the kind of one-off project I was asked to do, but if you need to do a lot of work in OpenXML then this would almost certainly provide a solid basis for interoperability. It's definitely worth investigating.

You could use the same basic algorithm I described in the earlier posting to translate data from a ResultSet to an OpenXML document; indeed it would be a trivial matter to write OpenXML4J implementations of the Workbook and Worksheet interfaces.

Last edited by Tony Sidaway : 06-05-2008 at 05:10 AM.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 06-05-2008, 04:08 PM
Member
 
Join Date: Jun 2008
Posts: 5
JForum4Hari is on a distinguished road
Hi,

U can very well use POI for excel data manipulation.The required jar files include:
poi-3.0-alpha3-20061212
poi-contrib-3.0-alpha3-20061212
poi-scratchpad-3.0-alpha3-20061212
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Save data from Excel sheet to servlet chandu.v Java Servlet 2 04-10-2008 03:51 PM
Regarding retrieval of sheet names from a excel workbook yuvaraj23 New To Java 0 03-11-2008 04:20 PM
How to set the AutoFilter to Excel Sheet with Wither POI or JXL lnarayana_boga Advanced Java 0 01-29-2008 11:05 AM
no of fonts and cells exceeding error in excel sheet. iimasd AWT / Swing 0 11-06-2007 08:58 AM
JExcel: Copy a cell content from one sheet to another sheet in the same workbook. ukbasak Advanced Java 0 08-02-2007 02:31 PM


All times are GMT +3. The time now is 01:57 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org