+ All Categories
Home > Documents > Apache POI

Apache POI

Date post: 26-Oct-2014
Category:
Upload: sreeni-reddy
View: 38 times
Download: 2 times
Share this document with a friend
Description:
apache poi
Popular Tags:
14
Creating a Spreadsheet with Apache POI Database table data is often required to be presented as an Excel spreadsheet. The Apache POI HSSF Excel 2007 consists of spreadsheets and an Excel spreadsheet consists of rows and cells. The the package. The data in the Excel spreadsheet data from a database. In this chapter we will discuss the procedure to create an Excel spreadsheet from an Oracle database table. The Excel spreadsheet is generated in the Setting the Environment The package classes are required to generate and Install the Oracle database 10g including sample schemas. Create a database instance. Create an example database table from which an Excel spreadsheet will be generated. The SQL script to create example table is listed as follows:
Transcript
Page 1: Apache POI

Creating a Spreadsheet with Apache POI

Database table data is often required to be presented as an Excel spreadsheet. The

Apache POI HSSF

Excel 2007 consists of spreadsheets and an Excel spreadsheet consists of rows and cells. The

the package. The data in the Excel spreadsheet

data from a database. In this chapter we will discuss the procedure to create an Excel spreadsheet from an Oracle database table. The Excel spreadsheet is generated in the

Setting the EnvironmentThe package classes are required to generate

and

Install the Oracle database 10g including sample schemas. Create a database instance. Create an example database table from which an Excel spreadsheet will be generated. The SQL script to create example table is listed as follows:

Page 2: Apache POI

[ 356 ]

connection with the Oracle database for retrieving the data for the Excel spreadsheet.

To create a connection select the Connections tab, select the Database node in the Connections Navigator, right click on the Database node, and select New Database Connection. In the Create Database Connection (Type) window displayed, specify a Connection Name and set Connection Type to Oracle(JDBC).

Page 3: Apache POI

[ 357 ]

Click on Next. In the Authentication window, specify the Username and Password and click on Next. In the Connection window, specify the Driver type, Host Name, JDBC Port, SID and click on Next.

In the Test window, click on Test Connection. If a connection with the database is established a success message is displayed. Click on Finish to complete the

Connections Navigator.

Page 4: Apache POI

[ 358 ]

is available as a data source . The data source is used to retrieve data from

for generating an Excel spreadsheet. Select File | Newthe New Gallery window, select General|Applications in the Categories listed and select Application in the Items listed. Click on the OK button. In the Create Application window, specify an Application Name, select the No Template Application Template and click on the OK button. In the Create Project window, specify a Project Name and click on the OKadded to the Applications-Navigator.

Applications-Navigator. Select Tools | Project Properties. In the Project Properties window, select the Libraries node. Click on the Add Library button to create a new library. In the Add Library window click on the New button to add a new library. In the Create Library window specify a Library Name and click on the Add Entry button.

In the Select Path Entry window select the and click on the Select Class Path node in the Create Library window. Click on the OK button in the Create Library window. The Apache POI library is added to the Add Library window. Click on the OK button in the Add Library window. The Apache POI library is created and added to the Libraries. The

Add Jar/Directory button.

Page 5: Apache POI

[ 359 ]

Creating an Excel SpreadsheetIn this section an Excel spreadsheet will be generated from the example database

Applications Navigator and select New. In the New Gallery window select Web Tier | Java Server Pages. In the Items listed, select JSP Page and click on the OK button. In the Create JSP Wizard select a Web Application version and click on the Next button. In the JSP File window specify a

Next. Click on the Next window in the Tag Libraries window. Select the default options in the HTML Options page and click on the Next button. In the Finish window, click on Finishthe Applications-Navigator.

Page 6: Apache POI

[ 360 ]

,

Add a element to the

In the an Excel spreadsheet will be created from an Oracle database

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 the following table.

Class Name Description

HSSFSheet Represents an Excel Spreadsheet. HSSFHeaderHSSFRowHSSFCellHSSFCellStyleHSSFFontHSSFChart Represents a ChartHSSFDateUtilHSSFPrintSetup

Create an Excel stylesheet workbook using the class:

Next, create an Excel spreadsheet from the method:

Page 7: Apache POI

[ 361 ]

connection from the database. First, create an create a method of the class

using the method:

Create a method of the interface. Run an SQL query on the table using the method of the interface to generate a

Create a header row for the Excel spreadsheet using the method of the

Set the header row cell values corresponding to the table columns. Create a cell with the method of the class and set the cell value using the

as follows:

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 and set the values in the row cells:

Create a directory . Create a to output the Excel

Page 8: Apache POI

[ 362 ]

method of the class.

The used to generate an Excel spreadsheet is listed below.

Page 9: Apache POI

[ 363 ]

To run the Applications-Navigator and select Run.

Page 10: Apache POI

[ 364 ]

is generated:

JDBC 4.0 Version

which may be obtained from: . Use the

Tools | Project Properties. In the Project Properties window, select the Libraries node and

J2SE Version

Connection state tracking

Statement pooling

Page 11: Apache POI

[ 365 ]

Connection state tracking is implemented by the connection pool manager and is

4.0 provides the method in the interface for connection state tracking. If the method returns false the connection pool manager closes the connection.

reduced performance due to excessive CPU usage. Connections are associated with application clients using client info properties. The 4.0 includes two new method and to set and get client info properties. Client info properties supported by a database may be retrieved using the method of the interface as follows:

The standard client info properties , , and may be set on the

the overhead of closing and opening new pooling is implemented using the and methods of the

The

Page 12: Apache POI

[ 366 ]

pattern as it also extends the interface. For example, the interface

and thus extends the interface. The interface provides

interface instead of the interface to run SQL queries. To create an

wrapper for the interface using the method. If a wrapper, unwrap the interface to create an

method in the interface. Subsequently invoke the method of

exceptions and chained causes. Chained exceptions are exceptions that are linked

iterate over chained exceptions as follows:

Page 13: Apache POI

[ 367 ]

Page 14: Apache POI

[ 368 ]

SummaryIn this chapter, an Excel spreadsheet was generated from an Oracle database table using the Apache POI HSSF API. The Apache POI HSSF API may also be used to parse an Excel spreadsheet and subsequently store the spreadsheet in a database.

connection state tracking, setting of client info properties, statement caching, access

exceptions facility.


Recommended