|
|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SYS-CON.TV |
TOP THREE LINKS YOU MUST CLICK ON Integration
WebLogic Tutorial: "Integrating Apache Poi in WebLogic Server"
The Apache Jakarta POI project provides components for the access and generation of Excel documents
By: Deepak Vohra; Ajay Vohra
Feb. 19, 2006 02:30 PM
Digg This!
Page 1 of 2
next page »
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.
Preliminary Setup 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:
Generating an Excel Document with Apache POI 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.*, 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(); Create a java.sql.Statement and get a result set from the example table Catalog:
Statement stmt=conn.createStatement(); 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. Page 1 of 2 next page »
BEA WEBLOGIC LATEST STORIES
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK BREAKING NEWS FROM THE WIRES
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||