Apache总是让人联想到“优秀”这个词。
前几天因为某个Java系统需要用到操作Excel,于是小研究了一下POI中的HSSF,非常好用。据官网上说,Office2007格式的文档支持也正在开发中,真是期待啊。
Apache POI包括POIFS(OLE2)、HSSF(excel)、HWPF(word)、HSLF(Powerpoint)等组件,使用者几乎可以用它做一个简单的Office了。
使用POI开发包时,需要先下载,目前最新稳定版本是3.1。解压后得到poi-3.1-FINAL-20080629.jar,将其加到应用的库中即可。
下面是一段试验代码,可以实现建立工作薄、创建工作表、将数据填充到单元格中:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package excel;
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package excel;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import java.io.FileInputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import java.io.FileInputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
/**
*
* @author Administrator
*/
public class ExcelUtil {
*
* @author Administrator
*/
public class ExcelUtil {
public String filePath = "e:\\workbook.xls";
public void newWordBook() {
HSSFWorkbook wb = new HSSFWorkbook();
try {
FileOutputStream fileOut = new FileOutputStream(filePath);
wb.write(fileOut);
fileOut.close();
} catch (FileNotFoundException ex) {
System.out.println(ex.getMessage());
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
HSSFWorkbook wb = new HSSFWorkbook();
try {
FileOutputStream fileOut = new FileOutputStream(filePath);
wb.write(fileOut);
fileOut.close();
} catch (FileNotFoundException ex) {
System.out.println(ex.getMessage());
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
/**
* 创建空白文件
*/
public void newSheet() {
HSSFWorkbook wb = new HSSFWorkbook();
wb.createSheet("第一页");
wb.createSheet("第二页");
try {
FileOutputStream fileOut = new FileOutputStream(filePath);
wb.write(fileOut);
fileOut.close();
} catch (FileNotFoundException ex) {
System.out.println(ex.getMessage());
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
* 创建空白文件
*/
public void newSheet() {
HSSFWorkbook wb = new HSSFWorkbook();
wb.createSheet("第一页");
wb.createSheet("第二页");
try {
FileOutputStream fileOut = new FileOutputStream(filePath);
wb.write(fileOut);
fileOut.close();
} catch (FileNotFoundException ex) {
System.out.println(ex.getMessage());
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
private void saveWorkBook(HSSFWorkbook wb) {
try {
FileOutputStream fileOut = new FileOutputStream(filePath);
wb.write(fileOut);
fileOut.close();
} catch (FileNotFoundException ex) {
System.out.println(ex.getMessage());
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
private HSSFWorkbook getWorkBook(String filePath){
try {
FileInputStream fileIn = new FileInputStream(filePath);
HSSFWorkbook wb = new HSSFWorkbook(fileIn);
fileIn.close();
return wb;
} catch (IOException ex) {
System.out.println(ex.getMessage());
return null;
}
}
private HSSFCell getCell(HSSFSheet sheet,int rowIndex,short columnIndex){
HSSFRow row = sheet.getRow(rowIndex);
if (row == null) {
row = sheet.createRow(rowIndex);
}
HSSFCell cell = row.getCell(columnIndex);
if (cell == null) {
cell = row.createCell((short) columnIndex);
}
return cell;
}
/**
* 写数据
* @param file
*/
public void writeData(String file) {
//创建工作薄
HSSFWorkbook wb = getWorkBook(file);
if(wb==null){
return;
}
//获取工作表
HSSFSheet sheet = wb.getSheetAt(0);
if (sheet == null) {
sheet = wb.createSheet("第一页");
}
HSSFCell cell=getCell(sheet,0,(short)0);
//数值
cell.setCellValue(123);
//字符串
HSSFRichTextString str=new HSSFRichTextString("你好");
cell=getCell(sheet,0,(short)1);
cell.setCellValue(str);
//保存
saveWorkBook(wb);
}
try {
FileOutputStream fileOut = new FileOutputStream(filePath);
wb.write(fileOut);
fileOut.close();
} catch (FileNotFoundException ex) {
System.out.println(ex.getMessage());
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
private HSSFWorkbook getWorkBook(String filePath){
try {
FileInputStream fileIn = new FileInputStream(filePath);
HSSFWorkbook wb = new HSSFWorkbook(fileIn);
fileIn.close();
return wb;
} catch (IOException ex) {
System.out.println(ex.getMessage());
return null;
}
}
private HSSFCell getCell(HSSFSheet sheet,int rowIndex,short columnIndex){
HSSFRow row = sheet.getRow(rowIndex);
if (row == null) {
row = sheet.createRow(rowIndex);
}
HSSFCell cell = row.getCell(columnIndex);
if (cell == null) {
cell = row.createCell((short) columnIndex);
}
return cell;
}
/**
* 写数据
* @param file
*/
public void writeData(String file) {
//创建工作薄
HSSFWorkbook wb = getWorkBook(file);
if(wb==null){
return;
}
//获取工作表
HSSFSheet sheet = wb.getSheetAt(0);
if (sheet == null) {
sheet = wb.createSheet("第一页");
}
HSSFCell cell=getCell(sheet,0,(short)0);
//数值
cell.setCellValue(123);
//字符串
HSSFRichTextString str=new HSSFRichTextString("你好");
cell=getCell(sheet,0,(short)1);
cell.setCellValue(str);
//保存
saveWorkBook(wb);
}
public static void main(String[] args) {
ExcelUtil excel = new ExcelUtil();
excel.writeData(excel.filePath);
}
}
ExcelUtil excel = new ExcelUtil();
excel.writeData(excel.filePath);
}
}
在JDK6上运行通过
本文转自 王杰瑞 51CTO博客,原文链接:http://blog.51cto.com/wangjierui/104628,如需转载请自行联系原作者