用Java怎樣從Excel檔案中讀取資料
package cn.com.spaceware.lixun;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
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 cn.com.spaceware.sxd.SC_BANK_DATABASE.DATABEAN.RepairInfoBean;
/**
* 從Excel檔案中讀取ATM機維護資訊
*
* Author: Shune LEE
*
* Date: November 22nd,2005
*/
public class ReadRepairInfo{
private boolean isCancel=false; //控制讀取是否繼續進行的參數
private String filePath=null; //Excel檔案的存放全路徑
/*控制格式的變數*/
private int headNum=2; //控制從第headNum行開始讀取,因為行的索引是從0開始的
private int leftNum=0; //控制從第leftNum列開始讀取,因為列的索引是從0開始的
ReadRepairInfo(String file){
this.filePath=file;
}
/**
*
* @author Shune Lee
*
* TODO 從Excel檔案中讀取維護資訊,並添加到RepairInfoBean中去
*
*/
public ArrayList readExcel4RepairInfo(){
ArrayList table=new ArrayList();
ArrayList record=new ArrayList();
try {
HSSFWorkbook workbook=new HSSFWorkbook(new FileInputStream(filePath));
HSSFSheet sheet = workbook.getSheetAt(0); //得到一個Sheet
/*獲得匯入的記錄數,也就是資料的行數*/
int rowNum=(sheet.getLastRowNum()-sheet.getFirstRowNum()+1)-headNum; //行的索引是從0開始的
/*遍曆所有資料,並將得到的資料傳給rInfo*/
for(int i=headNum;i
/*如果使用者將isCancel設為true,則停止讀取*/
if(isCancel)
break;
HSSFRow row = sheet.getRow(i);
for(short j=row.getFirstCellNum();j
String cellContent1=null;
Double cellContent2=null;
HSSFCell cell=row.getCell(j);
/*判斷儲存格內容的類型,並賦給相應的變數*/
if(cell.getCellType()==1){
cellContent1=cell.getStringCellValue();
record.add(cellContent1);
}
else if(cell.getCellType()==0){
cellContent2=new Double(cell.getNumericCellValue());
record.add(cellContent2);
}
else
System.out.println("Excel中的資料有問題,請檢查。");
}
table.add(getRepairInfoBean(record));
}
} catch (FileNotFoundException e) {
System.out.println("找不到檔案 " + filePath);
e.printStackTrace();
} catch (IOException e) {
System.out.println("訪問檔案出錯 " + filePath);
e.printStackTrace();
}
return table;
}
/**
*
* @author Shune Lee
*
* TODO 用從Excel讀取的記錄產生RepairInfoBean
*
* @param ArrayList 從Excel讀取的一條記錄,即一行
*/
public RepairInfoBean getRepairInfoBean(ArrayList content){
RepairInfoBean rpInfo=new RepairInfoBean();
if(content!=null&&!(content.isEmpty())){
/*給RepairInfoBean的對象傳入資料*/
rpInfo.setRepairID(((Double)content.get(0)).intValue());
rpInfo.setStrATMEndCode(((Double)content.get(1)).intValue());
rpInfo.setDEPLOYID(((Double)content.get(2)).intValue());
rpInfo.setStrRepairType(((Double)content.get(3)).intValue());
rpInfo.setStrContent((String)content.get(4));
rpInfo.setStrPersonName((String)content.get(5));
rpInfo.setStrDate(new String().valueOf(content.get(6)));
System.out.println("The size is="+content.size());
}
return rpInfo;
}
/*測試函數*/
public static void main(String[] args){
ReadRepairInfo readRepairInfo=new ReadRepairInfo("E://test.xls");
ArrayList list=readRepairInfo.readExcel4RepairInfo();
Iterator it=list.iterator();
while(it.hasNext()){
RepairInfoBean rpInfo=(RepairInfoBean)it.next();
System.out.println("RepairID="+rpInfo.getRepairID()+"; "+"strContent="+rpInfo.getStrContent());
}
}
}