JSPCN主页 | JSP空间 | 网站制作 | JSP下载 | JSP论坛 | JSP教程 | 关于JSPCN | 联系我们
JSP虚拟主机,jsp空间,java空间,java虚拟空间,详细请点击进入
做最专业的JSP中文网站 当前位置首页--技术新闻--缓存区  
文章搜索:
关键字 标题
  
JSP中文网内容管理系统(JCMS)
JSP虚拟主机
网络笔记本
网摘,图片,笔记收藏
虚拟服务器

JSPCN文章目录分类
JSP配置[219]JSP基础[136]
中文问题[69]上传问题[27]
JAVABEAN[46]数据库[212]
文件操作[126]图片声音[17]
JSP其他[57]时间相关[16]
JAVAMAIL[72]STRUTS[144]
开发工具[28]教程系列[157]
JSP实例[89]
JAVA基础[421]APPLET[78]
JAVA网络[179]Applica[115]
Servlet[98]XML[163]
J2ME[257]J2EE[374]
考试相关[63]JAVA线程[90]
EJB[261]Swing[26]
Java API[141]声音图片[28]
异常处理[33]JAVA实例[290]
JAVA类[139]SUN[89]
Hibernate[6]JMX[8]
Spring[34]
本版推荐文章 
本版热点文章 
相关文章链接 
 
Excelling in Excel with Java -Learn how to use the Jakarta POI
作者:     文章来源:
访问次数:1次     加入时间:2005年01月01日
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! 


 
JSP虚拟主机 | JSP空间 | JSP主机 | JSP双线虚拟主机 | JAVA空间 | JAVA虚拟主机 | 虚拟服务器 | JSP 虚拟服务器 | VPS
Virtual Private Server | JAVA虚拟服务器 | VM服务器 | VHOST | 虚拟操作系统 | JSP论坛 | JAVA论坛 | JSP站点论坛
Copyright © 2002-2005 JSPCN.net. All rights reserved.
JSP中文网    备案序号:蜀ICP备05001583号
成都恒海科技发展有限公司    成都市一环路南二段6号新瑞楼三楼8号