jar包下载  http://witchlovelearning.iteye.com/blog/1986284
附件为源码
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public 
class DataFromExcel {
	/**
	 * @param filePath excel文件存储名称及路径  例:"E:\\person.xls" 或 "E:\\person.xlsx"
	 */
	
	
	private static List<List<String>> read2003Excel(String filePath) {  
        File excelFile = null;
        InputStream inputs = null;
        String cellStr = null;// 单元格,最终按字符串处理  
        List<List<String>> sList = new ArrayList<List<String>>();// 返回封装数据的List  
       
        try {  
            excelFile = new File(filePath);
            if(!excelFile.exists()){
            	System.out.println("文件不存在") ;
            }
            else{
            	inputs = new FileInputStream(excelFile);
                HSSFWorkbook wb = new HSSFWorkbook(inputs);
                HSSFSheet sheet = wb.getSheetAt(0);
                // 开始
循环遍历行,表头不处理,从1开始  
                for (int i = 1; i <= sheet.getLastRowNum(); i++) {  
                    HSSFRow row = sheet.getRow(i);
                    if (row == null) {
                        
continue;  
                    } 
                    // 循环遍历单元格  
                    List<String> rowStr = new ArrayList<String>(); //每行的值组成的list
                    for (int j = 1; j < row.getLastCellNum(); j++) {  //不读入序号
                        HSSFCell cell = row.getCell(j);// 获取单元格对象  
                        if (cell == null) {
                            cellStr = "";  
                        } else if (cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {// 对布尔值的处理  
                            cellStr = String.valueOf(cell.getBooleanCellValue());  
                        } else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {// 对数字值的处理  
                            cellStr = cell.getNumericCellValue() + "";  
                        } else if (cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA) {// 对公式的处理  
                        	 cellStr = cell.getCellFormula();
                        }else {// 其余按照字符串处理  
                            cellStr = cell.getStringCellValue();  
                        }  
                        rowStr.add(cellStr);
                    }  
                    if("".equals(row.getCell(0)) || row.getCell(0) == null)
                    	
break;
                    else 
                        sList.add(rowStr);// 数据装入List  
                }  }
        } catch (IOException e) {  
        	e.printStackTrace();  
        } finally {// 关闭文件流  
            if (inputs != null) {  
                try {  
                	inputs.close();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
            }  
        }  
        return sList;  
    }  
	
	private static List<List<String>> read2007Excel(String filePath) {  
		File excelFile = null;
        InputStream inputs = null;
        String cellStr = null;
        List<List<String>> sList = new ArrayList<List<String>>();
       
        try {  
            excelFile = new File(filePath);
            if(!excelFile.exists()){
            	System.out.println("文件不存在") ;
            }
            else{
            	inputs = new FileInputStream(excelFile);
            	XSSFWorkbook wb = new XSSFWorkbook(inputs);// 创建Excel2007文件对象  
            	XSSFSheet sheet = wb.getSheetAt(0);
            	
                // 开始循环遍历行,表头不处理,从1开始  
                for (int i = 1; i < sheet.getLastRowNum(); i++) {  
                	XSSFRow row = sheet.getRow(i);
                    if (row == null) {
                        continue;  
                    }  
                    // 循环遍历单元格  
                    List<String> rowStr = new ArrayList<String>(); 
                    for (int j = 1; j < row.getLastCellNum(); j++) {  //不读入序号
                    	XSSFCell cell = row.getCell(j);// 获取单元格对象  
                        if (cell == null) {// 单元格为空设置cellStr为空串  
                            cellStr = "";  
                        } else if (cell.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN) {// 对布尔值的处理  
                            cellStr = String.valueOf(cell.getBooleanCellValue());  
                        } else if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {// 对数字值的处理  
                            cellStr = cell.getNumericCellValue() + "";  
                        } else if (cell.getCellType() == XSSFCell.CELL_TYPE_FORMULA) {// 对公式的处理  
                       	    cellStr = cell.getCellFormula();
                        } else {// 其余按照字符串处理  
                            cellStr = cell.getStringCellValue();  
                        }  
                        rowStr.add(cellStr);
                    } 
                    if("".equals(row.getCell(0)) || row.getCell(0) == null)
                    	break;
                    else 
                        sList.add(rowStr);// 数据装入List  
                }  
            }
        } catch (IOException e) {  
        	e.printStackTrace();  
        } finally {// 关闭文件流  
            if (inputs != null) {  
                try {  
                	inputs.close();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
            }  
        }  
        return sList; 
       }  
	
	public static void main(String[] args) {
		
        List<List<String>> lists = read2007Excel("E:\\readtests.xlsx");  
//        List<List<String>> lists = read2003Excel("E:\\readtest.xls"); 
        for (List<String> ls : lists) {  
        	for (String s : ls) {
        		System.out.print(s+"-");  
        	}
        	System.out.print("\n"); 
        }  
	} 
}
  
  
  
    
      
        
          - DataFromExcel.rar (1.5 KB)
- 下载次数: 0