Java怎樣從Excel檔案中讀取資料

來源:互聯網
上載者:User
用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());
     }
}

} 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.