一.POI简介
Jakarta POI 是apache的子项目,目标是处理ole2对象。
项目站点:http://poi.apache.org/
它提供了一组操纵Windows文档的Java API
目前比较成熟的是HSSF接口,处理MS Excel(97-2002)对象。
它不象我们仅仅是用csv生成的没有格式的可以由Excel转换的东西,
而是真正的Excel对象,你可以控制一些属性如sheet,cell等等。
二、版本
JDK:1.5
POI:3.5
更多实例到项目站点查看:http://poi.apache.org/
package com.vefan.excel;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.util.CellReference;
public class POIForXLS
{
public static void main(String[] args){
}
public void createXLS(){
Workbook wb = new HSSFWorkbook();
try
{
FileOutputStream fileOut = new FileOutputStream("E:\\workspace\\JavaApp\\csv\\workbook.xls");
wb.write(fileOut);
fileOut.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
public void readXLS(String filePath){
/*
Getting the cell contents
To get the contents of a cell, you first need to know what kind of cell it is (asking a string cell for its numeric contents will get you a NumberFormatException for example). So, you will want to switch on the cell's type, and then call the appropriate getter for that cell.
In the code below, we loop over every cell in one sheet, print out the cell's reference (eg A3), and then the cell's contents.
*/
// import org.apache.poi.ss.usermodel.*;
FileInputStream fileIn = null;
try
{
fileIn = new FileInputStream("E:\\workspace\\JavaApp\\csv\\workbook.xls");
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
Workbook wb = null;
try
{
wb = new HSSFWorkbook(fileIn);
} catch (IOException e)
{
e.printStackTrace();
}
Sheet sheet1 = wb.getSheetAt(0);
for (Row row : sheet1) {
for (Cell cell : row) {
CellReference cellRef = new CellReference(row.getRowNum(), cell.getColumnIndex());
System.out.print(cellRef.formatAsString());
System.out.print(" - ");
switch(cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
System.out.println(cell.getRichStringCellValue().getString());
break;
case Cell.CELL_TYPE_NUMERIC:
if(DateUtil.isCellDateFormatted(cell)) {
System.out.println(cell.getDateCellValue());
} else {
System.out.println(cell.getNumericCellValue());
}
break;
case Cell.CELL_TYPE_BOOLEAN:
System.out.println(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_FORMULA:
System.out.println(cell.getCellFormula());
break;
default:
System.out.println();
}
}
}
}
}
分享到:
相关推荐
POI操作Excel文档,帮助文档
Apache poi 操作 excel 文件压缩包,亲测可用
java的POI操作Excel文件,java的POI操作Excel文件,java的POI操作Excel文件
使用POI操作Excel文件(写)可参考:http://blog.csdn.net/flyever581549/archive/2008/11/11/3275850.aspx
java的POI操作Excel文件.pdf
包括POI库文件,API,5篇POI操作EXCEL的教程
可以运行的POI导出Excel文件实例,里面有两种方法,一个是Servlet,一个是main
POI读写excel文件+poi简单文档; POI读写excel文件+poi简单文档。
java的POI操作Excel文件,POI提供了Java操作office的组件,通过它可以从数据库中的表导入到Excel中,也可以进行相反的操作
poi操作Excel详细帮助文档poi操作Excel详细帮助文档
java的POI操作Excel文件2.pdf
poi操作excel文件的源代码 主要包括4个函数 1、poiWrite 向excel文件写入“姓名”两个字 2、poiWriteCopy 读取一个指定文件内容,写入另一个文件; 3、poiCopy2 读取指定路径文件,指定sheet,指定行、指定单元格...
poi 操作excel模板,读取数据后,讲数据填充到新生成的文件中且提供下载