Jakarta POI 是一套用于访问微软格式文档的Java API。
Jakarta POI有很多组件组成,其中有用于操作Excel格式文件的HSSF和用于早在Word的HWPF,在各种组件中目前只有用于操作Excel的HSSF相对成熟。它的官方首页是:http://jakarta.apache.org/poi/hssf/index.html,这里可以下载到它的最新版本和文档。
下面就来看看如何通过Jakarta POI的HSSF操作Excel文件。
操作Excel文件的步骤同JXL类似(关于如何用JXL操作Excel可以看我的另外两篇文章)。
HSSF对Excel的操作主要是通过下面几个对象实现:
HSSFWorkbook 工作簿对象对应于Excel文件
HSSFSheet Sheet对象对应于Excel中的Sheet
HSSFRow 行对象表示Sheet中的一行(这个对象在JXL中并没有提供)
HSSFCell 单元格对象
操作步骤就是用HSSFWorkbook打开或者创建“Excel文件对象”,用HSSFWorkbook对象返回或者创建Sheet对象,用Sheet对象返回行对象,用行对象得到Cell对象,有了Cell对象就随你读写了。下面来看一个动态生成Excel文件的例子:
//创建HSSFWorkbook对象
HSSFWorkbook wb = new HSSFWorkbook();
//创建HSSFSheet对象
HSSFSheet sheet = wb.createSheet("sheet0");
//创建HSSFRow对象
HSSFRow row = sheet.createRow((short)0);
//创建HSSFCell对象
HSSFCell cell=row.createCell((short)0);
//用来处理中文问题
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
//设置单元格的值
cell.setCellValue("单元格中的中文");
//定义你需要的输出流
OutputStream out = new FileOutputStream("viwo.xls");
//输出Excel
wb.write(out);
out.flush();
HSSF读取文件同样还是使用这几个对象,只是把相应的createXXX方法变成了getXXX方法即可。
只要理解了其中原理,不管是读还是写亦或是特定格式都可以轻松实现,正所谓知其然更要知其所以然。
最后附上HSSF官方的QuickGuide:《Busy Developers' Guide to HSSF Features》,虽然是英文的但还是很容易理解的。
http://jakarta.apache.org/poi/hssf/quick-guide.html
一、示例1:
Java代码
package com.cplatform.contants;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
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.hssf.util.HSSFColor;
public class A {
public static void main(String[] args)
throws IOException { HSSFWorkbook wb =
new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet(
"new sheet"); HSSFRow row = sheet.createRow((
short)
0); HSSFCell cell = row.createCell((
short)
0); cell.setCellValue(
1); row.createCell((
short)
1).setCellValue(
1.2); row.createCell((
short)
2).setCellValue(
"test"); row.createCell((
short)
3).setCellValue(
true); HSSFCellStyle cellStyle = wb.createCellStyle(); cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat(
"m/d/yy h:mm")); HSSFCell dCell = row.createCell((
short)
4); dCell.setCellValue(
new Date()); dCell.setCellStyle(cellStyle); HSSFCell csCell = row.createCell((
short)
5); csCell.setEncoding(HSSFCell.ENCODING_UTF_16); csCell.setCellValue(
"中文测试_Chinese Words Test"); HSSFCellStyle style = wb.createCellStyle(); style .setFillForegroundColor(
new HSSFColor.GREY_25_PERCENT() .getIndex()); style .setFillBackgroundColor(
new HSSFColor.GREY_25_PERCENT() .getIndex()); style.setFillPattern(HSSFCellStyle.SPARSE_DOTS); HSSFCell cell1 = row.createCell((
short)
6); cell1.setCellValue(
"X"); cell1.setCellStyle(style); HSSFCellStyle style1 = wb.createCellStyle(); style1.setFillForegroundColor(
new HSSFColor.GREY_40_PERCENT() .getIndex()); style1.setFillBackgroundColor(
new HSSFColor.GREY_40_PERCENT() .getIndex()); style1.setBorderBottom((
short)
1); style1.setBorderTop((
short)
1); style1.setBorderLeft((
short)
1); style1.setBorderRight((
short)
1); style1.setFillPattern(HSSFCellStyle.SPARSE_DOTS); HSSFCell cell11 = row.createCell((
short)
7); cell11.setCellValue(
"X11"); cell11.setCellStyle(style1); HSSFCellStyle st = wb.createCellStyle(); st.setDataFormat(HSSFDataFormat.getBuiltinFormat(
"#,##0")); HSSFCell cell12 = row.createCell((
short)
8); cell12.setCellValue((
double)
10000000); cell12.setCellStyle(st); cell12 .setEncoding(HSSFCell.ENCODING_UTF_16); cell12 .setCellValue(
"中文测试_Chinese Words Test"); row.createCell((
short)
9).setCellType(HSSFCell.CELL_TYPE_ERROR); FileOutputStream fileOut =
new FileOutputStream(
"D:/workbook.xls"); wb.write(fileOut); fileOut.close(); } } 二、示例
2:
package com.cplatform.contants;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class XLSExport {
private static short XLS_ENCODING = HSSFWorkbook.ENCODING_UTF_16;
private static String DATE_FORMAT =
" m/d/yy ";
private static String NUMBER_FORMAT =
" #,##0.00 ";
private String xlsFileName;
private HSSFWorkbook workbook;
private HSSFSheet sheet;
private HSSFRow row;
public XLSExport(String fileName) {
this.xlsFileName = fileName;
this.workbook =
new HSSFWorkbook();
this.sheet = workbook.createSheet(); }
public void exportXLS()
throws Exception {
try { FileOutputStream fOut =
new FileOutputStream(xlsFileName); workbook.write(fOut); fOut.flush(); fOut.close(); }
catch (FileNotFoundException e) {
throw new Exception(
" 生成导出Excel文件出错! ", e); }
catch (IOException e) {
throw new Exception(
" 写入Excel文件出错! ", e); } }
public void createRow(
int index) {
this.row =
this.sheet.createRow(index); }
public void setCell(
int index, String value) { HSSFCell cell =
this.row.createCell((
short) index); cell.setCellType(HSSFCell.CELL_TYPE_STRING); cell.setEncoding(XLS_ENCODING); cell.setCellValue(value); }
public void setCell(
int index, Calendar value) { HSSFCell cell =
this.row.createCell((
short) index); cell.setEncoding(XLS_ENCODING); cell.setCellValue(value.getTime()); HSSFCellStyle cellStyle = workbook.createCellStyle(); cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat(DATE_FORMAT)); cell.setCellStyle(cellStyle); }
public void setCell(
int index,
int value) { HSSFCell cell =
this.row.createCell((
short) index); cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC); cell.setCellValue(value); }
public void setCell(
int index,
double value) { HSSFCell cell =
this.row.createCell((
short) index); cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC); cell.setCellValue(value); HSSFCellStyle cellStyle = workbook.createCellStyle(); HSSFDataFormat format = workbook.createDataFormat(); cellStyle.setDataFormat(format.getFormat(NUMBER_FORMAT)); cell.setCellStyle(cellStyle); }
public static void main(String[] args) { System.out.println(
" 开始导出Excel文件 "); XLSExport e =
new XLSExport(
"d:/test.xls"); e.createRow(
0); e.setCell(
0,
" 编号 "); e.setCell(
1,
" 名称 "); e.setCell(
2,
" 日期 "); e.setCell(
3,
" 金额 "); e.createRow(
1); e.setCell(
0,
1); e.setCell(
1,
" 工商银行 "); e.setCell(
2, Calendar.getInstance()); e.setCell(
3,
111123.99); e.createRow(
2); e.setCell(
0,
2); e.setCell(
1,
" 招商银行 "); e.setCell(
2, Calendar.getInstance()); e.setCell(
3,
222456.88);
try { e.exportXLS(); System.out.println(
" 导出Excel文件[成功] "); }
catch (Exception e1) { System.out.println(
" 导出Excel文件[失败] "); e1.printStackTrace(); } } }