中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > Java > 第三方类库
Excelling in Excel with Java -Learn how to use the&nbs…
作者:未知 时间:2005-07-24 21:26 出处:JR 责编:chinaitpower
              摘要:Excelling in Excel with Java -Learn how to use the Jakarta POI
Elango Sundaram

Summary
Many organizations use Microsoft Excel as a mode of information exchange, as most non-programmers, business analysts, and project/program managers are comfortable with that technology. The ability to use Java to parse, gather, and consolidate data from Excel documents proves useful. And the Jakarta POI (Poor Obfuscation Implementation) allows programmers to quickly complete those tasks. POI can be used to complete pure Java ports of file formats based on Microsoft's OLE (Object Linking and Embedding) 2 compound document format (used by Microsoft Excel, Microsoft Word, and so on). This article provides insight into POI and shows how to read/write Microsoft Excel documents using Java. It also presents an interesting example using the POI API. (600 words; March 22, 2004) 


Advertisement 
 
 


hether you have balance sheets, account information downloads, tax calculations, or pay slips, they all tend to come in Microsoft Excel. Non-IT professionals feel comfortable using Microsoft Excel as a data exchange technology. The Jakarta POI (Poor Obfuscation Implementation) API is a fantastic way for Java programmers to access Microsoft document formats. The most mature API from Jakarta POI is the HSSF (Horrible Spreadsheet Format) API, which accesses Microsoft Excel documents. 

In this article, I walk you through the steps for creating and reading Excel documents, and for using fonts and cell styling?all using Java. 

Note: You can download the source code for all the examples in this article from Resources. 

POI terminology 
The key terms associated with Jakarta POI are as follows: 


POIFS (Poor Obfuscation Implementation File System): Java APIs for reading and writing OLE (Object Linking and Embedding) 2 compound document formats 

HSSF (Horrible Spreadsheet Format): Java API to read Microsoft Excel 

HDF (Horrible Document Format): Java API to read and write Microsoft Word 97 

HPSF (Horrible Property Set Format): Java API for reading property sets using (only) Java 
Create an Excel document 
The Jakarta POI API can be used to create an Excel document programmatically. The important steps involved are: 


Create a workbook: HSSFWorkbook workbook = new HSSFWorkbook(); 

Create a new worksheet in the workbook and name the worksheet "Java Excels": HSSFSheet sheet = workbook.createSheet("Java Excels"); 

Create a new row in the sheet: HSSFRow row = sheet.createRow((short)0); 

Create a cell in the row: HSSFCell cell = row.createCell((short) 0); 

Put some content in the cell: cell.setCellValue("Have a Cup of XL"); 

Write the workbook into the filesystem: workbook.write(fileOutputStream); 
Read data from the Excel document 
In this example, you'll see how to read values from an Excel document. 

Let's assume this is our Excel sheet: 

Employee Name Specialization Designation 
Anbu Programming Senior Programmer 
Jason Banking Industry Business Analyst 
Ramesh Databases DBA 
MackyB Accounting Delivery Head 
 


The key steps in reading the Excel sheet are as follows: 


Create a new Excel document reference: HSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(fileToBeRead));. 

Refer to the sheet: By default, the first sheet in the Excel document is at reference 0: HSSFSheet sheet = workbook.getSheetAt(0);. A sheet can also be referred to by name. Let's assume that the Excel sheet has the default name "Sheet1". It can be referred to as follows: HSSFSheet sheet = workbook.getSheet("Sheet1");. 

Refer to a row: HSSFRow row = sheet.getRow(0);. 

Refer to a cell in the row: HSSFCell cell = row.getCell((short)0);. 

Get the values in that cell: cell.getStringCellValue();. 
A practical example 
Now let's assume that we want to see the list of all declared methods and member variables in a jar file. It would be ideal to have a consolidated list of all information in one single file. We would like to view the information so that the class names are in the first column, declared fields in the second column, and declared methods in the third column, with the column headings appearing in red. 

The program will have to complete the following activities: 


Unzip the jar file 
Read all classfiles in the jar file 
Load the classes in the jar file 
Using reflection, get the declared methods and fields 
Write the class information into an Excel sheet using Jakarta POI
Let's concentrate on just the interesting steps of Jakarta POI usage: 


Create a new Excel document: workbook = new HSSFWorkbook(); 

Make a worksheet in that document and give the worksheet a name: sheet = workbook.createSheet("Java Class Info"); 

Set the first three columns' widths: sheet.setColumnWidth((short)0,(short)10000 ); 

Create the header line: HSSFRow row = sheet.createRow((short)0); 

Create and set font and cell style: 
  1.    HSSFFont font = workbook.createFont();
  2.    font.setColor(HSSFFont.COLOR_RED);
  3.    font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
  4.    // Create the style
  5.       HSSFCellStyle cellStyle= workbook.createCellStyle();
  6.       cellStyle.setFont(font);


Use the cell style: 
  1.       HSSFCell cell = row.createCell((short) 0);
  2.       cell.setCellStyle(cellStyle);
  3.       cell.setCellType(HSSFCell.CELL_TYPE_STRING);
  4.       cell.setCellValue("Class Name ");


Write the output file: 
  1.       FileOutputStream fOut = new FileOutputStream(outputFile);
  2.       // Write the Excel sheet
  3.       workbook.write(fOut);
  4.       fOut.flush();
  5.       // Done deal. Close it.
  6.       fOut.close();


Summary 
As demonstrated in this article, Java developers no longer need to wince at data in Excel sheets. We can programmatically access Excel documents. Have a cup of Java, and excel in Excel! 


 
关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有