poi读取Excel(兼容2003和2007)
前段时间做项目时,碰到了要导入Excel文件中的数据的需求,网上查了下,用的比较多的是jxl和apache的poi。刚开始用的是jxl(当时感觉它比较小巧)可是它貌似只能处理Excel2003及其以下版本的文件,而且在处理单元格前后的有全角空格时会出现乱码问题所以在处理时还得去掉首尾空格,最后跟客户沟通说是他们公司用的最多的是2007,果断放弃jxl,改用poi,毕竟是一百多人支持开发了10年的的开源项目,处理文件类型几乎囊括了微软的所有办公软件。。但是在用poi处理数据的时候出现了这样和那样的问题:如它把数字类型的值都按照double类型来处理了,而且如果是数字列有空单元格的话,它是算做是字符串类型的。。我把我写的处理代理拷贝过来,方便以后使用。。。嘿嘿...
?
package com.study.poi;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.study.entity.Emp;
public class PoiExcelTest {
??? public static void main(String[] args) {
??? ??? Class[] clazz=new Class[]{Integer.class,String.class,String.class,Integer.class,Date.class,Double.class,Double.class,Integer.class};
??? ??? List<Emp> list=null;
??? ??? DecimalFormat df=new DecimalFormat("0.00");
??? ??? try {
??? ??? ??? list = readExcel("d:"+File.separator+"test.xls",clazz);
??? ??? } catch (ParseException e) {
??? ??? ??? e.printStackTrace();
??? ??? }
??? ??? for (Iterator<Emp> iter=list.iterator(); iter.hasNext();) {
??? ??? ??? Emp emp=iter.next();
??? ??? ??? System.out.println("Emp:"+emp.getEmpno()+"\t"+emp.getEname()+"\t"+df.format(emp.getSal())+"\t"+df.format(emp.getComm())+"\t"+new SimpleDateFormat("yyyy-MM-dd").format(emp.getHiredate())+"\t"+emp.getDeptno());
??? ??? }
??? }
??? private static int version2003=2003;
??? private static int version2007=2003;
??? private static int version=version2003;
??? private static Workbook wb;
??? private static Sheet sheet;
??? private static Row row;
??? private static Cell cell;
??? public static List<Emp> readExcel(String excelFilePath,Class[] clazz) throws ParseException{
??? ??? List<Emp> list=new ArrayList<Emp>();
??? ??? Emp emp;
??? ??? version=(excelFilePath.endsWith(".xls")?version2003:version2007);
??? ??? if(version==2003){
??? ??? ??? try {
??? ??? ??? ??? InputStream stream=new FileInputStream(new File(excelFilePath));
??? ??? ??? ??? wb=new HSSFWorkbook(stream);
??? ??? ??? } catch (FileNotFoundException e) {
??? ??? ??? ??? e.printStackTrace();
??? ??? ??? } catch (IOException e) {
??? ??? ??? ??? e.printStackTrace();
??? ??? ??? }
??? ??? }else if(version==2007){
??? ??? ??? try {
??? ??? ??? ??? wb=new XSSFWorkbook(excelFilePath);
??? ??? ??? } catch (IOException e) {
??? ??? ??? ??? e.printStackTrace();
??? ??? ??? }
??? ??? }
??? ??? sheet=wb.getSheetAt(0);
??? ??? int rows=sheet.getLastRowNum();
??? ??? int cells=sheet.getRow(0).getPhysicalNumberOfCells();
??? ??? for (int i = 0; i < rows; i++) {
??? ??? ??? row=sheet.getRow(i+1);
??? ??? ??? emp=new Emp();
??? ??? ??? for (int j = 0; j < cells; j++) {
??? ??? ??? ??? cell=row.getCell(j);
??? ??? ??? ??? Object obj=getCellValue(cell, clazz[j]);
??? ??? ??? ??? switch (j) {
??? ??? ??? ??? case 0:
??? ??? ??? ??? ??? emp.setEmpno((Integer)obj);
??? ??? ??? ??? ??? break;
??? ??? ??? ??? case 1:
??? ??? ??? ??? ??? emp.setEname((String)obj);
??? ??? ??? ??? ??? break;
??? ??? ??? ??? case 2:
??? ??? ??? ??? ??? emp.setJob((String)obj);
??? ??? ??? ??? ??? break;
??? ??? ??? ??? case 3:
??? ??? ??? ??? ??? emp.setMgr((Integer)obj);
??? ??? ??? ??? ??? break;
??? ??? ??? ??? case 4:
??? ??? ??? ??? ??? emp.setHiredate(new SimpleDateFormat("yyyy-MM-dd").parse(obj.toString()));
??? ??? ??? ??? ??? break;
??? ??? ??? ??? case 5:
??? ??? ??? ??? ??? emp.setSal((Double)obj);?
??? ??? ??? ??? ??? break;
??? ??? ??? ??? case 6:
??? ??? ??? ??? ??? emp.setComm((Double)obj);
??? ??? ??? ??? ??? break;
??? ??? ??? ??? case 7:
??? ??? ??? ??? ??? emp.setDeptno((Integer)obj);
??? ??? ??? ??? ??? break;
??? ??? ??? ??? default:
??? ??? ??? ??? ??? break;
??? ??? ??? ??? }
??? ??? ??? }
??? ??? ??? list.add(emp);
??? ??? }
??? ??? return list;
??? }
??? public static Object getCellValue(Cell cell,Class clazz){
??? ??? String name=clazz.getSimpleName();
??? ??? if("String".equals(name)){
??? ??? ??? return cell.getStringCellValue();
??? ??? ???
??? ??? }else if("Double".equals(name)){
??? ??? ??? try {
??? ??? ??? ??? double valued=cell.getNumericCellValue();
??? ??? ??? ??? return valued;
??? ??? ??? } catch (Exception e) {
??? ??? ??? ??? return 0.0;
??? ??? ??? }
??? ??? }else if("Integer".equals(name)){
??? ??? ??? try {
??? ??? ??? ??? int valuei=(int)cell.getNumericCellValue();
??? ??? ??? ??? return valuei;
??? ??? ??? } catch (Exception e) {
??? ??? ??? ??? return 0;
??? ??? ??? }
??? ??? ???
??? ??? }else if("Date".equals(name)){
??? ??? ??? if(HSSFDateUtil.isCellDateFormatted(cell)){
??? ??? ??? ??? Date date=cell.getDateCellValue();
??? ??? ??? ??? if(date==null){
??? ??? ??? ??? ??? return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
??? ??? ??? ??? }else{
??? ??? ??? ??? ??? return new SimpleDateFormat("yyyy-MM-dd").format(date);
??? ??? ??? ??? }
??? ??? ??? }
??? ??? }
??? ??? return null;
??? }
}
?
?
下载地址:http://poi.apache.org/download.html
1 楼 tianshi1017 2011-12-03 判断03和07的excel版本这里貌似就不用了吧