poi读取excel

poi读取excel文件
 
 方法1:(转自:http://www.blogjava.net/hwpok/archive/2010/01/20/310263.html)


使用的jar包:poi-3.8-20120326.jar,poi-excelant-3.8-20120326.jar, poi-ooxml-3.8-20120326.jar ,poi-ooxml-schemas-3.8-20120326.jar,poi-scratchpad-3.8-20120326.jar,protobuf-java-2.2.0.jar

Java代码 

import java.io.File;   
import java.io.FileInputStream;   
import java.io.IOException;   
import java.io.InputStream;   
import java.text.DecimalFormat;   
import java.util.ArrayList;   
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;   
  
/** *//**  
 * <ul>  
 * <li>Title:[POI基础上的Excel数据读取工具]</li>  
 * <li>Description: [支持Excell2003,Excell2007,自动格式化数值型数据,自动格式化日期型数据]</li>  
 * <li>Copyright 2009 RoadWay Co., Ltd.</li>  
 * <li>All right reserved.</li>  
 * <li>Created by [惠万鹏] [Jan 20, 2010]</li>  
 * <li>Midified by [modifier] [modified time]</li>  
 *   
 * <li>所需Jar包列表</li>  
 * <li>poi-3.6-20091214.jar</li>  
 * <li>poi-contrib-3.6-20091214.jar</li>  
 * <li>poi-examples-3.6-20091214.jar</li>  
 * <li>poi-ooxml-3.6-20091214.jar</li>  
 * <li>poi-ooxml-schemas-3.6-20091214.jar</li>  
 * <li>poi-scratchpad-3.6-20091214.jar</li>  
 * <li>xmlbeans-2.3.0.jar</li>  
 * <ul>  
 *   
 * @version 1.0  
 */  
public class POIExcelUtil   
{   
    /** *//** 总行数 */  
    private int totalRows = 0;   
       
    /** *//** 总列数 */  
    private int totalCells = 0;   
       
    /** *//** 构造方法 */  
    public POIExcelUtil()   
    {}   
       
    /** *//**  
     * <ul>  
     * <li>Description:[根据文件名读取excel文件]</li>  
     * <li>Created by [Huyvanpull] [Jan 20, 2010]</li>  
     * <li>Midified by [modifier] [modified time]</li>  
     * <ul>  
     *   
     * @param fileName  
     * @return  
     * @throws Exception  
     */  
    public List<ArrayList<String>> read(String fileName)   
    {   
        List<ArrayList<String>> dataLst = new ArrayList<ArrayList<String>>();   
           
        /** *//** 检查文件名是否为空或者是否是Excel格式的文件 */  
        if (fileName == null || !fileName.matches("^.+\\.(?i)((xls)|(xlsx))$"))   
        {   
            return dataLst;   
        }   
           
        boolean isExcel2003 = true;   
        /** *//** 对文件的合法性进行验证 */  
        if (fileName.matches("^.+\\.(?i)(xlsx)$"))   
        {   
            isExcel2003 = false;   
        }   
           
        /** *//** 检查文件是否存在 */  
        File file = new File(fileName);   
        if (file == null || !file.exists())   
        {   
            return dataLst;   
        }   
           
        try  
        {   
            /** *//** 调用本类提供的根据流读取的方法 */  
            dataLst = read(new FileInputStream(file), isExcel2003);   
        }   
        catch (Exception ex)   
        {   
            ex.printStackTrace();   
        }   
           
        /** *//** 返回最后读取的结果 */  
        return dataLst;   
    }   
       
    /** *//**  
     * <ul>  
     * <li>Description:[根据流读取Excel文件]</li>  
     * <li>Created by [Huyvanpull] [Jan 20, 2010]</li>  
     * <li>Midified by [modifier] [modified time]</li>  
     * <ul>  
     *   
     * @param inputStream  
     * @param isExcel2003  
     * @return  
     */  
    public List<ArrayList<String>> read(InputStream inputStream,   
            boolean isExcel2003)   
    {   
        List<ArrayList<String>> dataLst = null;   
        try  
        {   
            /** *//** 根据版本选择创建Workbook的方式 */  
            Workbook wb = isExcel2003 ? new HSSFWorkbook(inputStream)   
                    : new XSSFWorkbook(inputStream);   
            dataLst = read(wb);   
        }   
        catch (IOException e)   
        {   
            e.printStackTrace();   
        }   
        return dataLst;   
    }   
       
    /** *//**  
     * <ul>  
     * <li>Description:[得到总行数]</li>  
     * <li>Created by [Huyvanpull] [Jan 20, 2010]</li>  
     * <li>Midified by [modifier] [modified time]</li>  
     * <ul>  
     *   
     * @return  
     */  
    public int getTotalRows()   
    {   
        return totalRows;   
    }   
       
    /** *//**  
     * <ul>  
     * <li>Description:[得到总列数]</li>  
     * <li>Created by [Huyvanpull] [Jan 20, 2010]</li>  
     * <li>Midified by [modifier] [modified time]</li>  
     * <ul>  
     *   
     * @return  
     */  
    public int getTotalCells()   
    {   
        return totalCells;   
    }   
       
    /** *//**  
     * <ul>  
     * <li>Description:[读取数据]</li>  
     * <li>Created by [Huyvanpull] [Jan 20, 2010]</li>  
     * <li>Midified by [modifier] [modified time]</li>  
     * <ul>  
     *   
     * @param wb  
     * @return  
     */  
    private List<ArrayList<String>> read(Workbook wb)   
    {   
        List<ArrayList<String>> dataLst = new ArrayList<ArrayList<String>>();   
           
        /** *//** 得到第一个shell */  
        Sheet sheet = wb.getSheetAt(0);   
        this.totalRows = sheet.getPhysicalNumberOfRows();   
        if (this.totalRows >= 1 && sheet.getRow(0) != null)   
        {   
            this.totalCells = sheet.getRow(0).getPhysicalNumberOfCells();   
        }   
           
        /** *//** 循环Excel的行 */  
        for (int r = 0; r < this.totalRows; r++)   
        {   
            Row row = sheet.getRow(r);   
            if (row == null)   
            {   
                continue;   
            }   
               
            ArrayList<String> rowLst = new ArrayList<String>();   
            /** *//** 循环Excel的列 */  
            for (short c = 0; c < this.getTotalCells(); c++)   
            {   
                Cell cell = row.getCell(c);   
                String cellValue = "";   
                if (cell == null)   
                {   
                    rowLst.add(cellValue);   
                    continue;   
                }   
                   
                /** *//** 处理数字型的,自动去零 */  
                if (Cell.CELL_TYPE_NUMERIC == cell.getCellType())   
                {   
                    /** *//** 在excel里,日期也是数字,在此要进行判断 */  
                    if (HSSFDateUtil.isCellDateFormatted(cell))   
                    {   
                        cellValue = DateUtil.get4yMdHms(cell.getDateCellValue());   
                    }   
                    else  
                    {   
                        cellValue = getRightStr(cell.getNumericCellValue() + "");   
                    }   
                }   
                /** *//** 处理字符串型 */  
                else if (Cell.CELL_TYPE_STRING == cell.getCellType())   
                {   
                    cellValue = cell.getStringCellValue();   
                }   
                /** *//** 处理布尔型 */  
                else if (Cell.CELL_TYPE_BOOLEAN == cell.getCellType())   
                {   
                    cellValue = cell.getBooleanCellValue() + "";   
                }   
                /** *//** 其它的,非以上几种数据类型 */  
                else  
                {   
                    cellValue = cell.toString() + "";   
                }   
                   
                rowLst.add(cellValue);   
            }   
            dataLst.add(rowLst);   
        }   
        return dataLst;   
    }   
       
    /** *//**  
     * <ul>  
     * <li>Description:[正确地处理整数后自动加零的情况]</li>  
     * <li>Created by [Huyvanpull] [Jan 20, 2010]</li>  
     * <li>Midified by [modifier] [modified time]</li>  
     * <ul>  
     *   
     * @param sNum  
     * @return  
     */  
    private String getRightStr(String sNum)   
    {   
        DecimalFormat decimalFormat = new DecimalFormat("#.000000");   
        String resultStr = decimalFormat.format(new Double(sNum));   
        if (resultStr.matches("^[-+]?\\d+\\.[0]+$"))   
        {   
            resultStr = resultStr.substring(0, resultStr.indexOf("."));   
        }   
        return resultStr;   
    }   
       
    /** *//**  
     * <ul>  
     * <li>Description:[测试main方法]</li>  
     * <li>Created by [Huyvanpull] [Jan 20, 2010]</li>  
     * <li>Midified by [modifier] [modified time]</li>  
     * <ul>  
     *   
     * @param args  
     * @throws Exception  
     */  
    public static void main(String[] args) throws Exception   
    {   
        List<ArrayList<String>> dataLst = new POIExcelUtil()   
                .read("e:/Book1_shao.xls");   
        for (ArrayList<String> innerLst : dataLst)   
        {   
            StringBuffer rowData = new StringBuffer();   
            for (String dataStr : innerLst)   
            {   
                rowData.append(",").append(dataStr);   
            }   
            if (rowData.length() > 0)   
            {   
                System.out.println(rowData.deleteCharAt(0).toString());   
            }   
        }   
    }   
}  

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
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;

/** *//**
 * <ul>
 * <li>Title:[POI基础上的Excel数据读取工具]</li>
 * <li>Description: [支持Excell2003,Excell2007,自动格式化数值型数据,自动格式化日期型数据]</li>
 * <li>Copyright 2009 RoadWay Co., Ltd.</li>
 * <li>All right reserved.</li>
 * <li>Created by [惠万鹏] [Jan 20, 2010]</li>
 * <li>Midified by [modifier] [modified time]</li>
 * 
 * <li>所需Jar包列表</li>
 * <li>poi-3.6-20091214.jar</li>
 * <li>poi-contrib-3.6-20091214.jar</li>
 * <li>poi-examples-3.6-20091214.jar</li>
 * <li>poi-ooxml-3.6-20091214.jar</li>
 * <li>poi-ooxml-schemas-3.6-20091214.jar</li>
 * <li>poi-scratchpad-3.6-20091214.jar</li>
 * <li>xmlbeans-2.3.0.jar</li>
 * <ul>
 * 
 * @version 1.0
 */
public class POIExcelUtil
{
    /** *//** 总行数 */
    private int totalRows = 0;
    
    /** *//** 总列数 */
    private int totalCells = 0;
    
    /** *//** 构造方法 */
    public POIExcelUtil()
    {}
    
    /** *//**
     * <ul>
     * <li>Description:[根据文件名读取excel文件]</li>
     * <li>Created by [Huyvanpull] [Jan 20, 2010]</li>
     * <li>Midified by [modifier] [modified time]</li>
     * <ul>
     * 
     * @param fileName
     * @return
     * @throws Exception
     */
    public List<ArrayList<String>> read(String fileName)
    {
        List<ArrayList<String>> dataLst = new ArrayList<ArrayList<String>>();
        
        /** *//** 检查文件名是否为空或者是否是Excel格式的文件 */
        if (fileName == null || !fileName.matches("^.+\\.(?i)((xls)|(xlsx))$"))
        {
            return dataLst;
        }
        
        boolean isExcel2003 = true;
        /** *//** 对文件的合法性进行验证 */
        if (fileName.matches("^.+\\.(?i)(xlsx)$"))
        {
            isExcel2003 = false;
        }
        
        /** *//** 检查文件是否存在 */
        File file = new File(fileName);
        if (file == null || !file.exists())
        {
            return dataLst;
        }
        
        try
        {
            /** *//** 调用本类提供的根据流读取的方法 */
            dataLst = read(new FileInputStream(file), isExcel2003);
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
        
        /** *//** 返回最后读取的结果 */
        return dataLst;
    }
    
    /** *//**
     * <ul>
     * <li>Description:[根据流读取Excel文件]</li>
     * <li>Created by [Huyvanpull] [Jan 20, 2010]</li>
     * <li>Midified by [modifier] [modified time]</li>
     * <ul>
     * 
     * @param inputStream
     * @param isExcel2003
     * @return
     */
    public List<ArrayList<String>> read(InputStream inputStream,
            boolean isExcel2003)
    {
        List<ArrayList<String>> dataLst = null;
        try
        {
            /** *//** 根据版本选择创建Workbook的方式 */
            Workbook wb = isExcel2003 ? new HSSFWorkbook(inputStream)
                    : new XSSFWorkbook(inputStream);
            dataLst = read(wb);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return dataLst;
    }
    
    /** *//**
     * <ul>
     * <li>Description:[得到总行数]</li>
     * <li>Created by [Huyvanpull] [Jan 20, 2010]</li>
     * <li>Midified by [modifier] [modified time]</li>
     * <ul>
     * 
     * @return
     */
    public int getTotalRows()
    {
        return totalRows;
    }
    
    /** *//**
     * <ul>
     * <li>Description:[得到总列数]</li>
     * <li>Created by [Huyvanpull] [Jan 20, 2010]</li>
     * <li>Midified by [modifier] [modified time]</li>
     * <ul>
     * 
     * @return
     */
    public int getTotalCells()
    {
        return totalCells;
    }
    
    /** *//**
     * <ul>
     * <li>Description:[读取数据]</li>
     * <li>Created by [Huyvanpull] [Jan 20, 2010]</li>
     * <li>Midified by [modifier] [modified time]</li>
     * <ul>
     * 
     * @param wb
     * @return
     */
    private List<ArrayList<String>> read(Workbook wb)
    {
        List<ArrayList<String>> dataLst = new ArrayList<ArrayList<String>>();
        
        /** *//** 得到第一个shell */
        Sheet sheet = wb.getSheetAt(0);
        this.totalRows = sheet.getPhysicalNumberOfRows();
        if (this.totalRows >= 1 && sheet.getRow(0) != null)
        {
            this.totalCells = sheet.getRow(0).getPhysicalNumberOfCells();
        }
        
        /** *//** 循环Excel的行 */
        for (int r = 0; r < this.totalRows; r++)
        {
            Row row = sheet.getRow(r);
            if (row == null)
            {
                continue;
            }
            
            ArrayList<String> rowLst = new ArrayList<String>();
            /** *//** 循环Excel的列 */
            for (short c = 0; c < this.getTotalCells(); c++)
            {
                Cell cell = row.getCell(c);
                String cellValue = "";
                if (cell == null)
                {
                    rowLst.add(cellValue);
                    continue;
                }
                
                /** *//** 处理数字型的,自动去零 */
                if (Cell.CELL_TYPE_NUMERIC == cell.getCellType())
                {
                    /** *//** 在excel里,日期也是数字,在此要进行判断 */
                    if (HSSFDateUtil.isCellDateFormatted(cell))
                    {
                        cellValue = DateUtil.get4yMdHms(cell.getDateCellValue());
                    }
                    else
                    {
                        cellValue = getRightStr(cell.getNumericCellValue() + "");
                    }
                }
                /** *//** 处理字符串型 */
                else if (Cell.CELL_TYPE_STRING == cell.getCellType())
                {
                    cellValue = cell.getStringCellValue();
                }
                /** *//** 处理布尔型 */
                else if (Cell.CELL_TYPE_BOOLEAN == cell.getCellType())
                {
                    cellValue = cell.getBooleanCellValue() + "";
                }
                /** *//** 其它的,非以上几种数据类型 */
                else
                {
                    cellValue = cell.toString() + "";
                }
                
                rowLst.add(cellValue);
            }
            dataLst.add(rowLst);
        }
        return dataLst;
    }
    
    /** *//**
     * <ul>
     * <li>Description:[正确地处理整数后自动加零的情况]</li>
     * <li>Created by [Huyvanpull] [Jan 20, 2010]</li>
     * <li>Midified by [modifier] [modified time]</li>
     * <ul>
     * 
     * @param sNum
     * @return
     */
    private String getRightStr(String sNum)
    {
        DecimalFormat decimalFormat = new DecimalFormat("#.000000");
        String resultStr = decimalFormat.format(new Double(sNum));
        if (resultStr.matches("^[-+]?\\d+\\.[0]+$"))
        {
            resultStr = resultStr.substring(0, resultStr.indexOf("."));
        }
        return resultStr;
    }
    
    /** *//**
     * <ul>
     * <li>Description:[测试main方法]</li>
     * <li>Created by [Huyvanpull] [Jan 20, 2010]</li>
     * <li>Midified by [modifier] [modified time]</li>
     * <ul>
     * 
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception
    {
        List<ArrayList<String>> dataLst = new POIExcelUtil()
                .read("e:/Book1_shao.xls");
        for (ArrayList<String> innerLst : dataLst)
        {
            StringBuffer rowData = new StringBuffer();
            for (String dataStr : innerLst)
            {
                rowData.append(",").append(dataStr);
            }
            if (rowData.length() > 0)
            {
                System.out.println(rowData.deleteCharAt(0).toString());
            }
        }
    }
}
 

 

方法2:已通过测试

Java代码 
// 构造 XSSFWorkbook 对象,strPath 传入文件路径   
XSSFWorkbook xwb = new XSSFWorkbook(fileName);   
  
XSSFSheet sheet = null;   
sheet = xwb.getSheet(sheetName);   
//int num = xwb.getSheetIndex(sheetName);                      
//sheet = xwb.getSheetAt(num);   
}   
// 定义 row、cell   
XSSFRow row;   
String cell;   
  
// 循环输出表格中的内容   
for (int i = sheet.getFirstRowNum(); i < sheet   
        .getPhysicalNumberOfRows(); i++) {   
    row = sheet.getRow(i);//获取行数据   
    for (int j = row.getFirstCellNum(); j < row   
            .getPhysicalNumberOfCells(); j++) {   
        // 通过 row.getCell(j).toString() 获取单元格内容,   
        cell = row.getCell(j).toString();//获取列数据   
        System.out.print(cell + "\t");         
    }   
    System.out.println("");   
}  

// 构造 XSSFWorkbook 对象,strPath 传入文件路径
XSSFWorkbook xwb = new XSSFWorkbook(fileName);

XSSFSheet sheet = null;
sheet = xwb.getSheet(sheetName);
//int num = xwb.getSheetIndex(sheetName);					
//sheet = xwb.getSheetAt(num);
}
// 定义 row、cell
XSSFRow row;
String cell;

// 循环输出表格中的内容
for (int i = sheet.getFirstRowNum(); i < sheet
		.getPhysicalNumberOfRows(); i++) {
	row = sheet.getRow(i);//获取行数据
	for (int j = row.getFirstCellNum(); j < row
			.getPhysicalNumberOfCells(); j++) {
		// 通过 row.getCell(j).toString() 获取单元格内容,
		cell = row.getCell(j).toString();//获取列数据
		System.out.print(cell + "\t");		
	}
	System.out.println("");
} 

方法3:(若读取excel97---excel2003可试用以下方法,该方法已测试通过)

Java代码 
//该方法根据参数返回该页指定单元格中的内容  

//该方法根据参数返回该页指定单元格中的内容Java代码 
public String getExcelCellValueByParams(String fileName, String sheetName, String cellNum){//传入的参数依次为excel文件名、sheet页名称(或sheet页下标)、单元格位置(例如A1、B1)   
    String cellValue = "";   
    try {   
        String n = fileName.substring(fileName.lastIndexOf(".")+1, fileName   
                .length());   
        if (n.equalsIgnoreCase("xls")) {   
            jxl.Workbook wb = Workbook.getWorkbook(new File(fileName));   
            Sheet rs = null;   
            if (isNumeric(sheetName)) {//判断该参数是否为数字   
                rs = wb.getSheet(Integer.valueOf(sheetName));// 根据下标读取sheet页   
            } else {   
                rs = wb.getSheet(sheetName);// 根据sheet名称读取该页   
            }   
            Cell cell = rs.getCell(cellNum);   
            cellValue = cell.getContents();   
        }    
    } catch (BiffException e) {   
        e.printStackTrace();   
    } catch (IOException e) {   
        e.printStackTrace();   
    }   
    return cellValue;   
}  


使用的jar包:poi-3.8-20120326.jar,poi-excelant-3.8-20120326.jar, poi-ooxml-3.8-20120326.jar ,poi-ooxml-schemas-3.8-20120326.jar,poi-scratchpad-3.8-20120326.jar,protobuf-java-2.2.0.jar,


                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值