POI读写EXCEL

最近一个项目需要生成和读写一堆繁杂的EXCEL表格,处理EXCEL数据。综合网上各大JAVA读写EXCEL的组件,个人觉得Apache的POI用起来算是比较方便的。在此与大家分享一下。
Aapache POI的下载地址(最新发布的3.5Final版):
http://poi.apache.org/download.html#POI-3.5-FINAL

解压ZIP文件,将根目录下的poi-3.5-FINAL-20090928.jar文件引入classpath。

操作EXCEL的几个主要类放在org.apache.poi.hssf.usermodel包下面,以HSSF打头的一些类,如下说明:
一、HSSFWork类,对应于一个EXCEL文件对象。
二、HSSFSheet类,对应于一个EXCEL中的sheet对象。
三、HSSFRow类,对应于一个sheet中的某一行。
四、HSSFCell类,对应于具体的某行某列。
五、POIFSFileSystem类,对应于一个文件对象,该类位于org.apache.poi.poifs.filesystem包下面。在写入单个sheet的文件时我们用不到这个类,但在读取EXCEL和写入多个sheet的文件时,该类是必须的。
还有其它诸如HSSFHeader,HSSFFooter,HSSFFont等很多类可参见官方文档。就以上的五个类我们可以完成简单的读写EXCEL操作。
可能这样说的还是很抽象,没关系,我们以下面的两个简单的读写例子来看一下POI是多么的好用(其实POI的功能远大于读写EXCEL,也不止像以下代码中展现的一些微弱功能,更大的功能可参见附包下载的文档)。

// XLSTest.java
package db.export;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class XLSTest {

/**
* read all elements from a excel and print them
*
* @param filepath
* @throws Exception
* zhanxiao
*/
public static void readXLS(String filepath) throws Exception {
// get the excel file inputstream
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(filepath));

// get excel object
HSSFWorkbook book = new HSSFWorkbook(fs);

// get sheet by index,you can use getSheet(String name) to get a desired
// name sheet
HSSFSheet sheet = book.getSheetAt(0);
HSSFRow row;
HSSFCell cell;

// iterator the sheet to get elements
// use getLastRowNum() to get the row count of the sheet
for (int i = 0; i <= sheet.getLastRowNum(); i++) {
// get row by index
row = sheet.getRow(i);

// use getLastCellNum() to get the total column number
// suppose all the elements in the sheet are type of String,so I
// user getStringCellValue()
for (int j = 0; j < row.getLastCellNum(); j++) {
// get cell by index
cell = row.getCell(j);

// if the cell is a null value,the method will throw a
// NullPointerException
System.out.print(cell.getStringCellValue() + "\t");
}
System.out.println("");
}
}

/**
* write xls
*
* @param filepath
* @throws Exception
* zhanxiao
*/
public static void writeXLS(String filepath) throws Exception {
/*
* the following 8 lines check whether the file is already exist if
* yes,create sheet base the file if no, create a new xls file
*/
File file = new File(filepath);
HSSFWorkbook book;
if (file.exists()) {
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file));
book = new HSSFWorkbook(fs);
} else {
book = new HSSFWorkbook();
}

// create a sheet with desired name
HSSFSheet sheet = book.createSheet("sheetName");
HSSFRow row;

// write a 10 row,2 column file
for (int i = 0; i < 10; i++) {
row = sheet.createRow(i);
for (int j = 0; j < 2; j++) {
row.createCell(j).setCellValue(
"Row " + i + " ," + " Column " + j);
}
}

// create a outputstream for file writing
FileOutputStream out = new FileOutputStream(filepath);
// write file
book.write(out);

// close outputstream
out.close();
}

public static void main(String[] args) throws Exception {
writeXLS("e:\\test.xls");
readXLS("e:\\test.xls");

}
}

总结:POI的功能完不止这些,具体参考文档。程序也有待提高,请大家指点。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值