excel 文件读取

一、基础说明

支持库:Apache POI 库,地址:http://poi.apache.org/download.html

分析:

如上图所示: 
每一个Excel文件都将被解析成一个WorkBook对象; 
Excel的每一页都将被解析成一个Sheet对象; 
然后,Excel中的每一行都是一个Row对象; 
每一个单元格都是一个Cell对象。

二、示例

1、excel 表格样例

2、需要的 jars

3、代码

public class Student {

	private String no;
	private String name;
	private String age;
	private Float score;
	public String getNo() {
		return no;
	}
	public void setNo(String no) {
		this.no = no;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	public float getScore() {
		return score;
	}
	public void setScore(Float score) {
		this.score = score;
	}
	@Override
	public String toString() {
		return "Student [no=" + no + ", name=" + name + ", age=" + age
				+ ", score=" + score + "]";
	}
}
public class ExcelReaderTest {

	public static void main(String[] args) {
		 String path = "C:\\Users\\Administrator\\Desktop\\Test.xlsx";
		 ExcelReaderTest test = new ExcelReaderTest();
		 String ss = test.readExcel(path);
		 System.out.println(ss);
	}
	
	
	public String readExcel(String path){
		InputStream is = null;
		try {
			is = new FileInputStream(path);
			XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is); // 整个 excel
			List<Student> list = new ArrayList<Student>();
			for(int numSheet=0; numSheet<xssfWorkbook.getNumberOfSheets(); numSheet++){
				XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet); // excel 中的每个 sheet
				if (xssfSheet == null) {
					continue;
				}
				// 从第一行开始,第0行为 标识
				for(int numRow=1; numRow<xssfSheet.getLastRowNum(); numRow++){
					XSSFRow row = xssfSheet.getRow(numRow);
					if(row == null){
						continue;
					}
					XSSFCell noCell = row.getCell(0);
					XSSFCell nameCell = row.getCell(1);
					XSSFCell ageCell = row.getCell(2);
					XSSFCell scoreCell = row.getCell(3);
					
					Student student = new Student();
					student.setNo(getValue(noCell));
					student.setName(getValue(nameCell));
					student.setAge(getValue(ageCell));
					student.setScore(Float.valueOf(getValue(scoreCell)));
					
					list.add(student);
				}
			}
			
			if(list.size() == 0){
				return "excel 无数据。";
			}else{
			    StringBuilder sb = new StringBuilder();
			    for(Student s : list){
			    	sb.append(s.toString()).append("\r\n");
			    }
			    return sb.toString();
			}
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		} finally{
			if(is != null){
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	// 获取 单元格 里的 内容
	public String getValue(XSSFCell cell){
	     if(cell == null){
	    	 return null;
	     }
	     if(cell.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN){
	    	 return String.valueOf(cell.getBooleanCellValue());
	     }else if(cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC){
	    	 return String.valueOf(cell.getNumericCellValue());
	     }else{
	    	 return String.valueOf(cell.getStringCellValue());
	     }
	}
	
}

猜你喜欢

转载自my.oschina.net/u/1387400/blog/1609538