poi解析Excel 支持2003、2007

package com.fh.controller.information.pictures;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

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;


public class ExcelIn {

	 /**
	   * 读取出filePath中的所有数据信息
	   * @param filePath excel文件的绝对路径
	   * 
	   */
	  public static  void getDataFromExcel()
	  {
	    String filePath = "E:\\123.xlsx";
	    //判断是否为excel类型文件
	   
	    FileInputStream fis =null;
	    FileInputStream fis2 =null;
	    Workbook wookbook = null;
	    try
	    {
	      //获取一个绝对地址的流
	        fis = new FileInputStream(filePath);
	        fis2 =new FileInputStream(filePath);
	    }
	    catch(Exception e)
	    {
	      e.printStackTrace();
	    }
	    try 
	    {
	      //2003版本的excel,用.xls结尾
	     wookbook = new HSSFWorkbook(fis2);//得到工作簿
	       
	    } 
	    catch (Exception ex) 
	    {
	      //ex.printStackTrace();
	      try
	      {
	        //2007版本的excel,用.xlsx结尾
	        
	        wookbook = new XSSFWorkbook(fis);//得到工作簿
	      } catch (IOException e)
	      {
	        //  Auto-generated catch block
	        e.printStackTrace();
	      }
	    }
	    //得到一个工作表
	    Sheet sheet = wookbook.getSheetAt(0);
	    //获得表头
	    Row rowHead = sheet.getRow(0);
	    //获得数据的总行数
	    int totalRowNum = sheet.getLastRowNum();
	     //获得所有数据
	    List<String> cus=new ArrayList<String>();
	   
	    for(int i = 0 ; i <= totalRowNum ; i++)
	    {
	      //获得第i行对象
	      Row row = sheet.getRow(i);
	      for(int j=0;j<=row.getLastCellNum();j++){
	    	  String cu=new String();
	      //获得获得第i行第0列的 String类型对象
	      Cell cell = row.getCell((short)j);
	      if(cell!=null)
	       System.out.println( cell.toString());
	      //获得一个数字类型的数据
	     
	    
	      }
	    }
	  }
	  public static void main(String[] args) {
		  ExcelIn.getDataFromExcel();
	}
}

需要导入三个jar包

poi-ooxml-3.5-FINAL.jar

poi-3.5-FINAL.jar

xmlbeans-2.5.0.jar

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值