| By Deepak Vohra, Ajay Vohra | Article Rating: |
|
| February 19, 2006 02:30 PM EST | Reads: |
46,857 |
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.
A database table is often required to be presented in an Excel spreadsheet. Also, a developer's requirement could be to store an Excel spreadsheet in a database table. The Apache POI HSSF project is an API to create an Excel spreadsheet. The data in the Excel spreadsheet generated with the POI HSSF project may be static data in an XML document, or dynamically retrieved data from a database. Also, an Excel document may be converted to an XML document or be stored in a database. In this tutorial we will discuss the procedure to create an Excel spreadsheet from a MySQL database table in WebLogic Server. Subsequently, an Excel spreadsheet will be stored in a database table.
Preliminary Setup
The implementation of the POI HSSF project is provided in the org.apache.poi.hssf.usermodel package. The org.apache.poi.hssf.usermodel package classes are required in the Classpath to generate an Excel spreadsheet or to parse an Excel spreadsheet. Download the Apache POI library poi-bin-2.5.1-final-20040804.zip file and extract the zip file to an installation directory (http://jakarta.apache.org/poi/). Install WebLogic 8.1 Server. Download the MySQL database (www.mysql.com). Extract the MySQL zip file mysql-4.0.25-win32.zip to a directory. Install the MySQL database. Download the MySQL Connector/J JDBC driver (www.mysql.com/products/connector/j/). Extract the MySQL zip file mysql-connector-java-3.1.10.zip to a directory. Add the MySQL JDBC driver jar file, mysql-connector-java-3.1.10-bin.jar, to the <weblogic81>\samples\domains\examples\startExamplesServer script CLASSPATH variable.
Login to the MySQL database with the DOS command:
>mysql
Access the example database test with the command:
mysql>use test
Create an example database table in the MySQL database from which an Excel spreadsheet will be generated. The SQL script to create example table Catalog is shown in Listing 1.
Next, we will add the Apache POI .jar file to the WebLogic Server Classpath and create a JDBC datasource in the WebLogic Server to retrieve data for an Excel spreadsheet.
Add the poi-2.5.1-final-20040804.jar file to the CLASSPATH variable in the <weblogic81>\samples\domains\examples\startExamplesServer script. <weblogic81> is the directory in which the WebLogic Server is installed.
Next, create a JDBC connection with the MySQL database in WebLogic Server. Start the examples server with the script startExamplesServer. Access the administration console with the URL http://localhost:7001/console or with the Administration Console link in the WebLogic Server Examples index. In the administration console, right-click on the examples>Services>JDBC>Connection Pools node and select Configure a new JDBCConnectionPool. Specify the following connection properties to configure a JDBC connection pool:
- The database type: MySQL
- The JDBC driver: MySQL's Driver (Type 4)
- Database name: test
- Host name: localhost
- Port number: 3306
- Driver class name: com.mysql.jdbc.Driver
- Connection URL: jdbc:mysql://localhost:3306/test
Generating an Excel Document with Apache POI
In this section we will generate an Excel spreadsheet from the example database table. First, create a JSP application to generate an Excel spreadsheet.
In the JSP an Excel spreadsheet will be created from a MySQL database table. The Apache POI HSSF API is used to generate an Excel spreadsheet. The Apache POI HSSF package has classes for the different components of an Excel spreadsheet. Some of the commonly used classes of the Apache POI HSSF package are listed in Table 1.
First, import the Apache POI HSSF package:
<%@ page import="org.apache.poi.hssf.usermodel.*, java.sql.*,
java.io.*,javax.naming.InitialContext"%>
Create an Excel stylesheet workbook:
HSSFWorkbook wb=new HSSFWorkbook();
Next, create an Excel spreadsheet:
HSSFSheet sheet1=wb.createSheet("sheet1");
The data for the stylesheet is retrieved from a MySQL database table. Obtain a JDBC connection from the database. The JDBC connection is obtained with the datasource JNDI MySqlDS.
InitialContext initialContext = new InitialContext();
javax.sql.DataSource ds = (javax.sql.DataSource)
initialContext.lookup("MySqlDS");
java.sql.Connection conn = ds.getConnection();
Create a java.sql.Statement and get a result set from the example table Catalog:
Statement stmt=conn.createStatement();
ResultSet resultSet=stmt.executeQuery("Select * from Catalog");
Create a header row for the Excel spreadsheet. The rows in an Excel spreadsheet are "0" based.
HSSFRow row=sheet1.createRow(0);
Set the header row cell values corresponding to the table columns. The row cells are also "0" based. For example, the value for the first cell in the row is set with the setCellValue method to CatalogId.
row.createCell((short)0).setCellValue("CatalogId");
To add rows to the spreadsheet, iterate over the result set and add a row for each of the table rows. Retrieve the column values from the ResultSet and set the values in the row cells.
Published February 19, 2006 Reads 46,857
Copyright © 2006 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Deepak Vohra
Deepak Vohra is a Sun Certified Java 1.4 Programmer and a Web developer.
More Stories By Ajay Vohra
Ajay Vohra is a senior solutions architect with DataSynapse Inc.
![]() |
bob kennelly 08/19/06 05:49:54 PM EDT | |||
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 02/19/06 03:33:36 PM EST | |||
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. |
||||
- Cloud People: A Who's Who of Cloud Computing
- Cloud Expo NY: Best Practices for Delivering Oracle Database as a Service
- BEA Updates WebLogic SOA Portal for Web 2.0 Era
- Cloud Business Solutions, Social Media, and Platform Systems of Engagement Market Shares, Strategies, and Forecasts, Worldwide, 2013 to 2019
- Engine Yard Announces General Availability of PHP on Engine Yard Cloud and Support for Riak Distributed Database
- Research and Markets: Global Platform-As-A-Service Market Expected To Post Revenue of US$6.45 Billion in 2016 According To Latest Report
- Part 3 | Component Models in Java
- Component Models in Java | Part 2
- Tech Trends To Watch In May 2013
- Kevin Benedict’s What’s New in HTML5 – Week of May 19, 2013
- Services Orinted Architecture (SOA) Market
- BrightScope Releases Top 25 Technology Companies With the Best 401k Plans
- Cloud People: A Who's Who of Cloud Computing
- Cloud Expo NY: Best Practices for Delivering Oracle Database as a Service
- BEA Updates WebLogic SOA Portal for Web 2.0 Era
- Cloud Business Solutions, Social Media, and Platform Systems of Engagement Market Shares, Strategies, and Forecasts, Worldwide, 2013 to 2019
- Velocity Technology Solutions Introduces IBM Power Systems Universal Cloud Services at COMMON 2013
- Engine Yard Announces General Availability of PHP on Engine Yard Cloud and Support for Riak Distributed Database
- Research and Markets: Global Platform-As-A-Service Market Expected To Post Revenue of US$6.45 Billion in 2016 According To Latest Report
- Component Models in Java | Part 1
- Part 3 | Component Models in Java
- Component Models in Java | Part 2
- Lessons to Learn from the Hibernate Core Implementation
- Tech Trends To Watch In May 2013
- Java vs C++ "Shootout" Revisited
- Where Are RIA Technologies Headed in 2008?
- Configuring Eclipse for Remote Debugging a WebLogic Java Application
- XA Transactions
- Migrating a JBoss EJB Application to WebLogic
- An Introduction to Abbot
- Cloud People: A Who's Who of Cloud Computing
- 'HTTP Session Replication Failure' Issues
- WebLogic Tutorial: "Integrating Apache Poi in WebLogic Server"
- Eclipse "Pollinate" Project to Integrate with Apache Beehive
- Monitoring and Controlling WebLogic Servers with WLST
- Failover and Recovery of Enterprise Applications - Part 1

























