jxl解析Excel文件

jopen 9年前發布 | 1K 次閱讀 Java

    package util;

import java.io.File;  
import java.io.IOException;  
import java.text.SimpleDateFormat;  
import java.util.ArrayList;  
import java.util.Date;  
import java.util.List;  

import pojo.UserInfo;  
import jxl.Cell;  
import jxl.CellType;  
import jxl.DateCell;  
import jxl.Sheet;  
import jxl.Workbook;  
import jxl.read.biff.BiffException;  

public class ExcelUtil {  
    /** 
     * 獲取excel表格的數據 
     * @param path 文件路徑 
     * @return 
     * @throws IOException  
     * @throws BiffException  
     */  
    public static List<UserInfo> getExcelDate() throws BiffException, IOException{  
        String[] info=null;  
        List<UserInfo> list=new ArrayList<UserInfo>();  

        File file = new File("F:\\userInfo.xls");  
        Workbook book = Workbook.getWorkbook(file);  
        // 獲得第一個工作表對象  
        Sheet sheet = book.getSheet(0);  
        Cell cell=null;  
        int a=sheet.getRows();  
        int b=sheet.getColumns();  
        for (int i = 1; i < a ; i++){  
            info=new String[b];  
            for(int j = 0;j < b ; j++){  
                cell = sheet.getCell(j, i);  

                if(cell.getType()==CellType.DATE){//時間的處理  
                    DateCell dc = (DateCell)cell;  
                    Date date = dc.getDate();  
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
                    String sDate = sdf.format(date);  
                    info[j] = sDate;  
                }else{  
                    info[j] = cell.getContents();  
                }  

            }  
            UserInfo ui=arrayToObject(info);  
            list.add(ui);  
        }  

        return list;  
    }  
    /** 
     * 數組轉對象 
     * @param info 
     * @return 
     */  
    private static UserInfo arrayToObject(String[] info){  
        UserInfo ui=new UserInfo();  

        ui.setAccountNum(info[0]);  
        ui.setCustomerName(info[1]);  
        ui.setMobile(info[2]);  
        ui.setServiceSales(info[3]);  
        ui.setTerminal(info[4]);  
        ui.setRecommend(info[5]);  
        ui.setSource(info[6]);  
        ui.setRegDate(info[7]);  

        return ui;  
    }  

}  </pre> 


 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!