POI读取Excel2003、Excel2007简单示例

网上示例很多,参考过几个,可能是版本或环境的问题,照着做居然报错,今天抽了一点时间自己写了个小例子(存在一些不足之处)简单熟悉了一下。

首先说一下开发环境:Windows 7 、JDK 1.6.25、MyEclipse 6.5、POI 3.8(下载地址:http://www.apache.org/dyn/closer.cgi/poi/release/bin/poi-bin-3.8-20120326.zip)

具体用到的几个Jar包:

POI读取Excel2003、Excel2007简单示例

Excel内容:

具体代码如下:

 

package com.project.excel;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ReadExcel {

	public static void main(String[] args){
		List<UserBean> list = parseExcel("d:\\user_2007.xlsx");
		for(int i=0;i<list.size();i++){
			System.out.println("姓名:"+list.get(i).getUsername()+"  性别:"+list.get(i).getSex()+"  年龄:"+list.get(i).getAge());
		}
	}
	
	/**
	 * 解析Excel,读取内容
	 * @param path Excel路径
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List parseExcel(String path){
		List<UserBean> list = new ArrayList();
		File file = null;
		InputStream input = null;
		if(path!=null&&path.length()>7){
			//判断文件是否是Excel(2003、2007)
			String suffix = path.substring(path.lastIndexOf("."),path.length());
			file = new File(path);
			try {
				input = new FileInputStream(file);
			} catch (FileNotFoundException e) {
				System.out.println("未找到指定的文件!");
			}
			if (".xls".equals(suffix)) {// 2003
				System.out.println("Excel为2003版本");
				POIFSFileSystem fileSystem = null;
				HSSFWorkbook workBook = null;//工作簿
				try {
					fileSystem = new POIFSFileSystem(input);
					workBook = new HSSFWorkbook(fileSystem);
				} catch (IOException e) {
					e.printStackTrace();
				}
				HSSFSheet sheet = workBook.getSheetAt(0);//获取第一个工作簿
				list = getContent((Sheet)sheet);
			} else if (".xlsx".equals(suffix)) {// 2007
				System.out.println("Excel为2007版本");
				XSSFWorkbook workBook = null;
				try {
					workBook = new XSSFWorkbook(input);
				} catch (IOException e) {
					e.printStackTrace();
				}
				XSSFSheet sheet = workBook.getSheetAt(0);//获取第一个工作簿
				list = getContent(sheet);
			}
		}else{
			System.out.println("非法的文件路径!");
		}
		return list;
	}
	
	/**
	 * 获取Excel内容
	 * @param sheet
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List getContent(Sheet sheet){
		List<UserBean> list = new ArrayList();
		int rowCount = sheet.getPhysicalNumberOfRows();//行数
		for(int i=1;i<rowCount;i++){//遍历行,略过标题行,从第二行开始
			UserBean entity = new UserBean();
			Row row = sheet.getRow(i);
			int cellCount = row.getPhysicalNumberOfCells();
			for(int j=0;j<cellCount;j++){//遍历行单元格
				Cell cell = row.getCell(j);
				switch(j){
				    case 0:
				    	if(cell.getCellType()==Cell.CELL_TYPE_STRING){
							entity.setUsername(cell.getStringCellValue());
						}
						break;
				    case 1:
				    	if(cell.getCellType()==Cell.CELL_TYPE_STRING){
							entity.setSex(cell.getStringCellValue());
						}
						break;
				    case 2:
				    	if(cell.getCellType()==Cell.CELL_TYPE_NUMERIC){
							entity.setAge((int)cell.getNumericCellValue());
						}
						break;
				}
			}
			list.add(entity);
		}
		return list;
	}
}

class UserBean{
	
	private String username;//姓名
	private String sex;//性别
	private int age;//年龄
	
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}

你可能感兴趣的:(poi,Excel,Excel,2003,2007)