Sie sind auf Seite 1von 36

Excel with Apache POI and Oracle Database

Dustin Marx
http://marxsoftware.blogspot.com/ 13 February 2007 All photographs of
Denver provided by Denver Metro Convention & Visitors Bureau and used with permission.

The Usual Disclaimer


The opinions expressed here are my own and are not necessarily representative of my employers views, of RMOUGs views, or of the views of any other organization or individual.

Excel with Apache POI

13 February 2007

Trademarks
Several products, specifications, and other trademarked terms appear in this presentation and all trademarks remain the property of their respective trademark owners. Trademark owners include:
Microsoft Corporation (Excel and Office Products) Sun Microsystems (Java) Apache Software Foundation
Excel with Apache POI 13 February 2007

Agenda

Photograph of Maroon Bells (near Aspen) provided by Denver Metro Convention & Visitors Bureau and used with permission.

Apache POI (and POI-HSSF) Overview Creating an Excel Document with POI-HSSF Reading an Excel Document with POI-HSSF Key and Helper POI-HSSF Classes Apache POI Limitations and Gotchas Handling Open Office 2007 XML Formats Additional References and Resources
Excel with Apache POI 13 February 2007

Why Excel and Apache POI?


Microsofts Excel is a widely used spreadsheet application, especially in business and industry End users of database reports will often prefer reports in manager-friendly Excel format Excel can be used to easily and quickly generate charts and graphs based on provided data Apache POI is free (Apache License) and is simple to apply within Java programs
Use Apache POI with Java SE and/or Java EE Use Apache POI with other JVM-hosted programming languages such as Groovy and JRuby (perfect for scripting)
Excel with Apache POI 13 February 2007

How Might I Use POI?


Integrate Excel spreadsheet reading and generation in existing Java SE/EE applications
Example: Generate context-sensitive spreadsheet based on data on current web page

Create simple Java application for Excel file management


Example: Build database reports in Excel

Write scripts with JVM-based scripting languages (JRuby, Groovy, etc.) for reporting in Excel
Example: Database management using POI
Excel with Apache POI 13 February 2007

What is Apache POI?


Open Source Java API for Reading and Writing Microsoft Office Format Files
Applies to Excel and Word pre-Office 2007 formats PowerPoint and Visio to lesser extent

Formerly known as Jakarta POI Focus of this presentation is on reading and writing Excel with Apache POIs POI-HSSF
http://poi.apache.org/hssf/index.html
Excel with Apache POI 13 February 2007

Apache POI Subprojects Support of Various Office Formats


Microsoft Apache POI Office Subproject Application
Excel Word PowerPoint Visio Outlook POI - HSSF POI - HWPF POI - HSLF POI - HDGF POI - HSMF

Status
Most Complete And Thorough Early development phase Mostly read only; less complete Read only; less complete Read only; less complete

Excel with Apache POI

13 February 2007

What Products Use Apache POI?


Spring Framework Microsoft Project Exchange in Java (MPXJ) JTimeTracker Steelray Project Viewer Intellisys Project Desktop Excel Writer ColdFusion MX 7.0 JasperReports . . . many others (mostly unadvertised) . . .
Excel with Apache POI 13 February 2007

Does Apache POI Work with Office 2007 Formats?


No However:
Many existing files predate Office 2007 You can save files in Office 2007 applications to older formats that POI does support Many people are not using Office 2007 format exclusively or even at all Office 2007 formats are XML based (zipped up XML files)
Excel with Apache POI 13 February 2007

Apache POI-HSSF Objects Represent Spreadsheet


HSSFWorkbook

HSSFCell

HSSFRow

HSSFSheet
Excel with Apache POI 13 February 2007

POI HSSF Package Use: Layered Spreadsheet Objects


Creating a Spreadsheet Reading a Spreadsheet HSSFWorkbook createSheet() getSheet(String) or getSheetAt(int) HSSFSheet createRow() HSSFRow createCell() HSSFCell getCell(short) or cellIterator() getRow(int) or rowIterator()

Excel with Apache POI

13 February 2007

Generating Excel Documents with Apache POI: Sequence Diagram

Excel with Apache POI

13 February 2007

Creating an Excel Document in Java with POI: High-level Steps


1.

Instantiate a Workbook:
HSSFWorkbook workbook = new HSSFWorkbook();

2.

Use Workbook to Instantiate a Sheet:


HSSFSheet sheet = workbook.createSheet(<<SheetTitle>>);

3.

Use Sheet to Instantiate a Row:


HSSFRow row = sheet.createRow( (short) <<someShortIndex>>);

4.

Use Row to Instantiate a Cell:


HSSFCell cell = row.createCell( (short) <<someShortIndex>>, <<cellType>> );

Excel with Apache POI

13 February 2007

Creating an Excel Document in Java with POI: High-level Steps


5.

Write workbook out to Excel file:

OutputStream os = null; try { os = new FileOutputStream( <<someFilePathAndName>> ); workbook.write(os); // workbook is an HSSFWorkbook } catch (IOException ioEx) . . . Note that each created sheet (HSSFSheet, step 2), row (HSSFRow, step 3), and cell (HSSFCell, step 4) can be edited for appearance and other attributes.

Excel with Apache POI

13 February 2007

Reading an Excel Document in Java with POI: Sequence Diagram

Excel with Apache POI

13 February 2007

Reading an Excel Document in Java with POI : High-level Steps


1.

Instantiate a POIFSFileSystem with appropriate Excel file to be read:


POIFSFileSystem fs = new POIFSFileSystem( new FileInputStream(myspreadsheet.xls"));

2.

Instantiate a Workbook based on POIFSFileSystem:


HSSFWorkbook wb = new HSSFWorkbook(fs);

3.

Use Workbook to acquire Sheet:


HSSFSheet sheet = wb.getSheetAt(0); (Zero-based index to lookup sheets in a workbook)

Excel with Apache POI

13 February 2007

Reading an Excel Document in Java with POI : High-level Steps


4.

Use Sheet to acquire Row:


HSSFRow row = sheet.getRow(<<someRowIndex>>);

5.

Use Row to acquire Cell:


HSSFCell cell = row.getCell( (short) <<someCellIndex>>);

6.

Read and/or alter contents of Cell using Cell accessor methods


13 February 2007

Excel with Apache POI

POI HSSFCell Manipulation


HSSFCell setCellComment() setCellFormula() setCellStyle() setCellType() setCellValue() HSSFCellTypes Blank, Boolean, Error, Formula, Numeric, String
Excel with Apache POI

HSSFCellStyle setAlignment() setBorderXXXXX() setXXXXXBorderColor() setFillBackgroundColor() setFillForegroundColor() setFont() setWrap()

13 February 2007

Other Key HSSF Classes: HSSFWorkbook and HSSFSheet


HSSFWorkbook createCellStyle() createName() createSheet() getNumberOfSheets() getSelectedTab() getSheet() getSheetAt() write() HSSFSheet addMergedRegion() createFreezePane() createRow() setColumnHidden() setDefaultColumnWidth() setDefaultRowHeight() setGridsPrinted() setMargin() setProtected() setSelected() setZoom()
13 February 2007

Excel with Apache POI

Other Key HSSF Classes: HSSFRow and HSSFComment


HSSFRow createCell() setHeight() HSSFComment setAuthor() setColumn() setRow() setString() setVisible()

Excel with Apache POI

13 February 2007

Other Key HSSF Classes: HSSFHeader and HSSFFooter


HSSFHeader setCenter() setLeft() setRight() date() time() numPages() file() tab()
Excel with Apache POI

HSSFFooter setCenter() setLeft() setRight() date() time() numPages() file() tab()


13 February 2007

HSSF Useful Helper Classes


HSSF
Class used for internal HSSF testing that also provides examples of using HSSF API Font Manipulation Apply Font to String

HSSFName

HSSFPrintSetup Region HSSFRegionUtil

Range Naming

HSSFFont

Represent Region Manipulate Region

HSSFRichTextString HSSFDateUtil
Excel and Java Date Manipulation

HSSFDataFormat
Excel with Apache POI

13 February 2007

Apache POI Limitations


Advertised Major Limitations
http://poi.apache.org/hssf/limitations.html Charts Macros Pivot Tables

Should be able to pre-create Excel spreadsheet with charts, macros, or pivot tables and then use POI to populate data values in those pre-generated sheets
Excel with Apache POI 13 February 2007

Apache POI Gotchas: Setting Cell Style


Acquiring and Setting a CellStyle
HSSFCell.getCellStyle() affects All Cells in Sheet HSSFWorkbook.createCellStyle() allows control over individual cell style http://marxsoftware.blogspot.com/2007/10 /apache-poi-use-hssfworkbook-tocreate.html
Excel with Apache POI 13 February 2007

So What About Microsoft Office 2007 Open XML Format?


Office Documents in zipped XML format
Apache POI does not handle Office 2007 Open XML Format (OOXML)

Overview of Office Open XML format:


http://msdn2.microsoft.com/enus/library/aa338205.aspx

Underlying XML format makes Office files potentially more open and portable
Excel with Apache POI 13 February 2007

How Do I Work with Office Open XML (2007) File Formats?


Several options to work with these new XML-based formats:
1.

Unzip and manipulate underlying XML files directly using XML tools (such as XQuery!)
See my Introduction to XQuery presentation tomorrow at RMOUG Training Days 2008

2.

3.

Use framework like OpenXML4J (http://sourceforge.net/projects/openxml4j/) Convert to older Office format and manipulate with Apache POI
13 February 2007

Excel with Apache POI

Other POI-Based or POI-Related Tools


POI Browser
Simple Java Swing HMI providing peek into Microsoft Office files internal structure http://marxsoftware.blogspot.com/2007/12/using-apachepoi-poibrowser.html

POI Ruby Bindings


Allows Ruby programming language to make use of POI http://poi.apache.org/poi-ruby.html

jXLS
Generates rich XLS templates for POI to write data to http://jxls.sourceforge.net/

Excel with Apache POI

13 February 2007

Dustins Other Presentations: RMOUG 2008 and Collaborate08


Introduction to XQuery: A Multipurpose XML Tool
RMOUG Training Days 2008 Thursday, February 14, 8:30 am

Add Some Flash to Your Oracle DB Applications: Flex and OpenLaszlo


Collaborate08, IOUG Forum Monday, April 14, 9:15 am Co-authors: Bill Jackson and Michael Martin
Excel with Apache POI 13 February 2007

Additional References and Resources: My Blog


Main blog URL:
http://marxsoftware.blogspot.com/

POI entries in this blog:


http://marxsoftware.blogspot.com/search/l abel/POI

Excel with Apache POI

13 February 2007

Additional References and Resources: HSSF Quick Start


Busy Developers Guide to HSSF Features
http://poi.apache.org/hssf/quick-guide.html A first must-read for a gentle but fairly comprehensive introduction to POI-HSSF Many concise and practical code samples Covers 80% of what youll probably ever need!

Download source code (it is open source!)


Examples using POI included in code distribution See also org.apache.poi.hssf.dev package

Apache POI Mailing List


http://news.gmane.org/gmane.comp.jakarta.poi.user
Excel with Apache POI 13 February 2007

Additional References and Resources: OTN Articles


Create an Excel Report from Several Oracle Databases Using Apache Jakarta POI
http://www.oracle.com/technology/pub/art icles/saternos_broadcast.html

Generate External Tables from an Excel Spreadsheet Using Apache Jakarta POI
http://www.oracle.com/technology/pub/art icles/saternos_tables.html
Excel with Apache POI 13 February 2007

Additional References and Resources


Learn to Read and Write Microsoft Excel Documents with Jakartas POI Its POI-fect
http://www.devx.com/Java/Article/17301 http://www.javaworld.com/javaworld/javaqa/2002-05/01qa-0503-excel3.html http://www.javaworld.com/javaworld/jw-03-2004/jw-0322poi.html http://www.onjava.com/pub/a/onjava/2003/04/16/poi_excel .html

Excelling in Excel with Java

Reading and Writing Excel Files with POI

Excel with Apache POI

13 February 2007

Additional References and Resources


Brief Introduction to HSSF Using Apache Jakarta POI (NACFUG)
http://inplain.net/CF/index.html

Busy ColdFusion Developers Guide to HSSF Features


http://www.dross.org/index.cfm?objectid=9C65ECEC-508BE116-6F8A9F878188D7CA

Reading Excel Sheet Using Java

http://www.javabeat.net/articles/2007/10/apachepoi-reading-excel-sheet-using-java/
13 February 2007

Excel with Apache POI

Additional References and Resources: JVM Scripting


POI and JVM Scripting Languages
Groovy
http://www.jroller.com/rmcmahon/entry/excel_ reports_using_groovy_and

JRuby
http://wickedcoolthoughts.blogspot.com/2007/ 12/excel-poi-wrapper-in-jruby.html

Excel with Apache POI

13 February 2007

Additional References and Resources: Office Open XML


ECMA Office Open XML Formats FAQ
http://office.microsoft.com/enus/products/HA101723691033.aspx

Open XML and Java OpenXML4J

http://openxmldeveloper.org/articles/OpenXMLand Java.aspx http://sourceforge.net/projects/openxml4j/ http://www.ddj.com/linux-opensource/202401913

XQuery Your Office Documents

Excel with Apache POI

13 February 2007

Das könnte Ihnen auch gefallen