import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
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;
/**
* EXCEL文檔解析工具類
* 該工具能將EXCEL文檔中的表解析為由JAVA基礎類構成的資料集合
* 整個EXCEL表由多個行組成.每行用一個LIST表示.
* EXCEL中的行由一個LIST表示,各列的資料索引從0開始一一對齊存放在這個LIST中;
* 多個行構成整個表,由一個LIST存放多個行.
*
*******************************************
* com.trumptech.common.fileParser.excel
* 2007-6-15
* 16:20:38
* author linfan
*******************************************
*/
public class ExcelSheetParser {
private Logger logger= Logger.getLogger(ExcelSheetParser.class);
private HSSFWorkbook workbook ;
public ExcelSheetParser(File excelFile) throws FileNotFoundException, IOException{
workbook = new HSSFWorkbook(new FileInputStream(excelFile));
}
/**
* 獲得表中的資料
* @param sheetNumber 表格索引(EXCEL 是多表文檔,所以需要輸入表索引號)
* @return 由LIST構成的行和表
* @throws FileNotFoundException
* @throws IOException
*/
public List<List> getDatasInSheet(int sheetNumber) throws FileNotFoundException, IOException{
List<List> result = new ArrayList<List>();
//獲得指定的表
HSSFSheet sheet = workbook.getSheetAt(sheetNumber);
//獲得資料總行數
int rowCount = sheet.getLastRowNum();
logger.info("found excel rows count: " + rowCount);
if (rowCount < 1) {
return result;
}
//逐行讀取資料
for (int rowIndex = 0; rowIndex <= rowCount; rowIndex++) {
//獲得行對象
HSSFRow row = sheet.getRow(rowIndex);
if (row != null) {
List<Object> rowData = new ArrayList<Object>();
//獲得本行中儲存格的個數
int columnCount = row.getLastCellNum();
//獲得本行中各儲存格中的資料
for (short columnIndex = 0; columnIndex < columnCount; columnIndex++) {
HSSFCell cell = row.getCell(columnIndex);
//獲得指定儲存格中資料
Object cellStr = this.getCellString(cell);
rowData.add(cellStr);
}
result.add(rowData);
}
}
return result;
}
/**
* 獲得儲存格中的內容
* @param cell
* @return
*/
protected Object getCellString(HSSFCell cell){
Object result = null;
if (cell != null) {
int cellType = cell.getCellType();
switch(cellType){
case HSSFCell.CELL_TYPE_STRING :
result = cell.getRichStringCellValue().getString();
break;
case HSSFCell.CELL_TYPE_NUMERIC:
result=cell.getNumericCellValue();
break;
case HSSFCell.CELL_TYPE_FORMULA:
result = cell.getNumericCellValue();
break;
case HSSFCell.CELL_TYPE_ERROR:
result=null;
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
result=cell.getBooleanCellValue();
break;
case HSSFCell.CELL_TYPE_BLANK:
result=null;
break;
}
}
return result;
}
public static void main(String[] args) throws Exception {
File file = new File("D://tmp//test.xls");
ExcelSheetParser parser = new ExcelSheetParser(file);
List<List> datas = parser.getDatasInSheet(0);
for(int i=0;i<datas.size();i++){//顯示資料
List row = datas.get(i);
for(short n=0;n<row.size() ;n++){
Object value = row.get(n);
String data = String.valueOf(value);
System.out.print(data +"/t");
}
System.out.println();
}
}
}