【0】写在前面
- 0.1) these codes are from 基于Apache POI 的从xlsx读出数据
- 0.2) this idea is from http://cwind.iteye.com/blog/2187670 , adding some comments for easy understanding proves to be my work.
package com.cwind.poi;
import java.io.File;
import java.io.FileInputStream;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class SimpleDatasheetReader {
public static void main(String[] args){
try {
File excel = new File("E:/bench-cluster/temp-resource/RunningMan.xlsx");
FileInputStream fis = new FileInputStream(excel);
//创建工作簿
XSSFWorkbook book = new XSSFWorkbook(fis);
//创建工作簿下的第一页纸张
XSSFSheet sheet = book.getSheetAt(0);
// 基于行的迭代器
Iterator<Row> itr = sheet.iterator();
System.out.println(itr.hasNext());
// Iterating over Excel file in Java
while (itr.hasNext()) {
//得到行
Row row = itr.next(); System.out.println(row.getLastCellNum());
// Iterating over each column of Excel file
// 基于行创建单元格 迭代器
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
//依次 获取某行的单元格
Cell cell = cellIterator.next();
switch (cell.getCellType()) {
//下面是依据不同数据类型 打印出单元格的 数据
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue() + "\t");
break;
case Cell.CELL_TYPE_NUMERIC:
if(DateUtil.isCellDateFormatted(cell)){
System.out.print(cell.getDateCellValue() + "\t");
}else{
System.out.print(cell.getNumericCellValue() + "\t");
}
break;
case Cell.CELL_TYPE_BOOLEAN:
System.out.print(cell.getBooleanCellValue() + "\t");
break;
default:
}
}
System.out.println("");
}
}catch(Exception ex){
ex.printStackTrace();
}
}
}