excel 文件讀取

1、基礎說明

支持庫:Apache POI 庫,地址:http://poi.apache.org/download.htmlhtml

分析:java

如上圖所示: 
每個Excel文件都將被解析成一個WorkBook對象; 
Excel的每一頁都將被解析成一個Sheet對象; 
而後,Excel中的每一行都是一個Row對象; 
每個單元格都是一個Cell對象。apache

2、示例

一、excel 表格樣例app

二、須要的 jarsxss

三、代碼ide

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());
	     }
	}
	
}
相關文章
相關標籤/搜索