YOUR FEEDBACK
José D'Andrade wrote: "...it may never be released..." Why? "...if Midori isn’t heir to Windows Mi...
AJAXWorld RIA Conference
$300 Savings Expire August 8
Register Today and SAVE!

2007 West
GOLD SPONSORS:
Active Endpoints
Your SOA Needs BPEL for Orchestration
BEA
Virtualized SOA: Adaptive Infrastructure for Demanding Applications
Nexaweb
Overcoming Bandwidth Challenges with Nexaweb
TIBCO
What is Service Virtualization?
SILVER SPONSORS:
WSO2
Using Web Services Technologies and FOSS Solutions
Click For 2007 East
Event Webcasts

2008 East
PLATINUM SPONSORS:
Appcelerator
Think Fast: Accelerate AJAX Development with Appcelerator
GOLD SPONSORS:
DreamFace Interactive
The Ultimate Framework for Creating Personalized Web 2.0 Mashups
ICEsoft
AJAX and Social Computing for the Enterprise
Kaazing
Enterprise Comet: Real–Time, Real–Time, or Real–Time Web 2.0?
Nexaweb
Now Playing: Desktop Apps in the Browser!
Sun
jMaki as an AJAX Mashup Framework
POWER PANELS:
The Business Value
of RIAs
What Lies Beyond AJAX?
KEYNOTES:
Douglas Crockford
Can We Fix the Web?
Anthony Franco
2008: The Year of the RIA
Click For 2007 Event Webcasts

SYS-CON.TV
TOP THREE LINKS YOU MUST CLICK ON


WebLogic Tutorial: "Integrating Apache Poi in WebLogic Server"
The Apache Jakarta POI project provides components for the access and generation of Excel documents

for (int i=1;resultSet.next(); i++)
         {
     row=sheet1.createRow(i);
row.createCell((short)0).setCellValue(resultSet.getString(1));
row.createCell((short)1).setCellValue(resultSet.getString(2));
row.createCell((short)2).setCellValue(resultSet.getString(3));
row.createCell((short)3).setCellValue(resultSet.getString(4));
row.createCell((short)4).setCellValue(resultSet.getString(5));
}

Create a FileOutputStream to output the Excel spreadsheet to an XLS file. An XLS file represents an Excel spreadsheet:

FileOutputStream output=new FileOutputStream(new File("c:/excel/catalog.xls"));

Output the Excel spreadsheet to an XLS file:

wb.write(output);

The ExcelWebLogic.jsp JSP used to generate an Excel spreadsheet is available in the References section.

To run the ExcelWebLogic.jsp JSP in the WebLogic Server, copy the JSP to the <weblogic81>\samples\server\examples\build\mainWebApp directory. Run the JSP with the URL http://localhost:7001/ExcelWebLogic.jsp.

An Excel spreadsheet gets generated which may be opened in Excel (http://office.microsoft.com/en-us/FX010858001033.aspx) or the Excel Viewer tool (http://office.microsoft.com/en-us/assistance/HA011620741033.aspx).

Storing an Excel Document in a Database Table
In this section we will store an Excel spreadsheet in a MySQL database table with the Apache POI API. The example Excel document stored is the spreadsheet, catalog.xls, which was generated in the previous section. The Excel spreadsheet is stored in MySQL table Catalog. Drop the Catalog table from which the Excel document was generated in the previous section with the MySQL command:

MySQL>DROP table Catalog;

Develop a JSP application to store the example Excel document in the MySQL database. In the JSP application import the Apache POI packages org.apache.poi.poifs.filesystem and org.apache.poi.hssf.usermodel. The org.apache.poi.poifs.filesystem package has classes to create an Excel workbook and the org.apache.poi.hssf.usermodel package has classes that represent an Excel workbook, spreadsheet, spreadsheet row, and row cell.

<%@ page import="org.apache.poi.poifs.filesystem.*,
org.apache.poi.hssf.usermodel.*, java.sql.*,
java.io.*,javax.naming.InitialContext"%>

As in the previous section, obtain a JDBC connection from the MySQL datasource:

InitialContext initialContext = new InitialContext();
       javax.sql.DataSource ds = (javax.sql.DataSource)
       initialContext.lookup("MySqlDS");
    java.sql.Connection conn = ds.getConnection();

Create java.sql.Statement from the JDBC connection:

Statement stmt=conn.createStatement();

Create a MySQL table in which the Excel spreadsheet will be stored:

String createTable="CREATE TABLE Catalog(CatalogId VARCHAR(25) PRIMARY KEY,Journal
VARCHAR(25),Section VARCHAR(25),Edition VARCHAR(25),Title Varchar(125),Author Varchar(25))";

stmt.execute(createTable);

Create a POIFSFileSystem to read the Excel document:

File catalogExcel=new File("C:/ExcelWebLogic/catalog.xls");
FileInputStream inputStream=new FileInputStream(catalogExcel);
POIFSFileSystem fileSystem=new POIFSFileSystem(inputStream);

Obtain a HSSF workbook from the POIFSFileSystem:

HSSFWorkbook wb=new HSSFWorkbook(fileSystem);

Obtain an Excel spreadsheet from the Excel workbook:

HSSFSheet sheet1=wb.getSheet("sheet1");

Iterate over the rows in the spreadsheet with a row iterator:

java.util.Iterator rowIterator=sheet1.rowIterator();
HSSFRow row=(HSSFRow)rowIterator.next();

Retrieve the row cell values for each of the rows. For example, the CatalogId row cell value is retrieved with:

String catalogId=row.getCell((short)0).getStringCellValue();

Add a table row for each of the rows in the Excel spreadsheet:

String exceltable="INSERT INTO Catalog VALUES("+"\'"+catalogId+"\
'"+","+"\'"+journal
+"\'"+","+"\'"+section+"\'"+","+"\'"+edition+"\'"+","+"\'"+title+"\
'"+","+"\'"+author+"\'"+")";
stmt.execute(exceltable);

Copy the POIWebLogic.jsp to the <weblogic81>\samples\server\examples\build\mainWebApp directory. Run the JSP with the URL http://localhost:7001/POIWebLogic.jsp. A MySQL database table gets generated from the Excel spreadsheet. The POIWebLogic.jsp used to generate a database table from an Excel spreadsheet is available in the References section.

Conclusion
In this tutorial, an Excel spreadsheet was generated from a MySQL database table and subsequently the spreadsheet was stored in a database table. The WebLogic Server facilitates the conversion from database table to Excel spreadsheet and from spreadsheet to database table by providing a datasource and a J2EE application server to run a JSP application.

About Deepak Vohra
Deepak Vohra is a Sun Certified Java 1.4 Programmer and a Web developer.

About Ajay Vohra
Ajay Vohra is a senior solutions architect with DataSynapse Inc.

YOUR FEEDBACK
bob kennelly wrote: Hello this is just what i've been looking for, it's a very helpfull article! In the article there is a statement saying "see the Reference area" to find the JSP code, can anyone tell me where i can find the reference section please? Thanks very much!
SYS-CON Italy News Desk wrote: The Apache Jakarta POI project provides components for the access and generation of Excel documents. The POI HSSF API is used to generate Excel Workbooks and to add Excel spreadsheets to a workbook. An Excel spreadsheet consists of rows and cells. The layout and fonts of a spreadsheet are also set with the POI HSSF API.
BEA WEBLOGIC LATEST STORIES
Mike Neil is general manager for virtualization strategy in the Windows Server Division at Microsoft. Mike is focused on the delivery of the Windows virtualization technology, including Windows Server 2008 Hyper-V, Microsoft Hyper-V Server and Virtual PC 2007. Mike also directs the tec...
Two of the biggest launches in Rich Internet Application history took place in 2007/2008 when Adobe launched AIR 1.0 in February '08 and Microsoft launched Silverlight (September '07). At the 6th International AJAXWorld RIA Conference & Expo in October SYS-CON Events is delighted to be...
A standard from OASIS called Web Services for Remote Portlets (WSRP) is used so portlets can be decoupled from a portal. In part one (JDJ, Volume. 13, issue 3) of this article, we introduced the relevant standards and specifications and then demonstrated WSRP's capabilities by consumin...
SYS-CON's upcoming '3rd International Virtualization Conference & Expo' faculty includes such distinguished speakers as: Al Aghili (Managed Methods), Alan Chhabra (Egenera), Andi Mann (Enterprise Management Associates), Andrew Conte (APC), Andy Astor (EnterpriseDB), Ariel Cohen (Xsigo ...
From Application Virtualization to Xen, a round-up of the virtualization themes & topics being discussed in NYC June 23-24, 2008 by the world-class speaker faculty at the 3rd International Virtualization Conference & Expo being held by SYS-CON Events in The Roosevelt Hotel, in midtown ...
Red Hat announced that Cybercity has chosen to use the JBoss Enterprise SOA Platform for system integration and middleware. The JBoss solution is expected to reduce Cybercity's total cost of ownership (TCO). In selecting an SOA solution, Cybercity initially evaluated Oracle Fusion, BEA...
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS
SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021

SYS-CON FEATURED WHITEPAPERS

ADS BY GOOGLE
BREAKING NEWS FROM THE WIRES

Check Point® Software Technologies Ltd. (Nasdaq:CHKP), t...