(5) POI 에서 Excel 내용 읽 기

12208 단어 자바poiExcel
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import writeFileContent.ExcelUtil;

public class ExcelReader {

    /** *   excel03   * @param file excel03   * @return ArrayList<ArrayList<String>> */
    public static ArrayList<ArrayList<String>> readXLS(String file){
        ArrayList<ArrayList<String>> data=new ArrayList<ArrayList<String>>();
        try{
            InputStream is=new FileInputStream(file);
            HSSFWorkbook workbook=new HSSFWorkbook(is);//   Excel         
            //    sheet 
            for (int numSheet = 0; numSheet < workbook.getNumberOfSheets(); numSheet++) {
                if (null != workbook.getSheetAt(numSheet)) {
                    HSSFSheet sheet = workbook.getSheetAt(numSheet); //    sheet
                    //  sheet        
                    for (int rowNumOfSheet = 0; rowNumOfSheet <= sheet.getLastRowNum(); rowNumOfSheet++) {
                        if (null != sheet.getRow(rowNumOfSheet)) {
                            HSSFRow row = sheet.getRow(rowNumOfSheet); //    
                            ArrayList<String> rowList=new ArrayList<String>();
                            //            
                            for (int cellNumOfRow = 0; cellNumOfRow <= row.getLastCellNum(); cellNumOfRow++) {
                                if (null != row.getCell(cellNumOfRow)) {
                                    HSSFCell cell = row.getCell(cellNumOfRow); //    
                                    if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){//    
                                        if(HSSFDateUtil.isCellDateFormatted(cell)){
                                            DateFormat df=new SimpleDateFormat("yyyy/MM/dd");
                                            Date date=cell.getDateCellValue();
                                            rowList.add(df.format(date));
                                        }else{
                                            double value=cell.getNumericCellValue();
                                            rowList.add(String.valueOf(value));
                                        }
                                    }else if(cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN){//    
                                        boolean bool=cell.getBooleanCellValue();
                                        rowList.add(String.valueOf(bool));
                                    }else {
                                        rowList.add(cell.getStringCellValue());
                                    }
                                }
                            }
                            data.add(rowList);
                        }
                    }
                }        
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(ExcelUtil.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ExcelUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
        return data;
    }

    /** *   excel07   * @param file excel07   * @return ArrayList<ArrayList<String>> */
    public static ArrayList<ArrayList<String>> readXLSX(String file){
        ArrayList<ArrayList<String>> data=new ArrayList<ArrayList<String>>();
        try{
            //   XSSFWorkbook   ,file        
            XSSFWorkbook xwb = new XSSFWorkbook(file);
            //     Sheet
            for(int numSheet = 0; numSheet < xwb.getNumberOfSheets(); numSheet++){
                XSSFSheet sheet = xwb.getSheetAt(numSheet); 
                if(sheet == null){
                    continue;
                }
                //   Row
                for(int rowNum = 0; rowNum <= sheet.getLastRowNum(); rowNum++){
                    XSSFRow row = sheet.getRow(rowNum);
                    if(row == null){
                        continue;
                    }
                    ArrayList<String> rowList=new ArrayList<String>();
                    //   Cell
                    for(int cellNum = 0; cellNum <= row.getLastCellNum(); cellNum++){
                        XSSFCell cell = row.getCell(cellNum);
                        if(cell == null){
                            continue;
                        }
                        if(cell.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN){
                            boolean bool=cell.getBooleanCellValue();
                            rowList.add(String.valueOf(bool));
                        }else if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){//    
                            if(HSSFDateUtil.isCellDateFormatted(cell)){
                                DateFormat df=new SimpleDateFormat("yyyy/MM/dd");
                                Date date=cell.getDateCellValue();
                                rowList.add(df.format(date));
                            }else{
                                double value=cell.getNumericCellValue();
                                rowList.add(String.valueOf(value));
                            }
                        }else{
                            rowList.add(cell.getStringCellValue());
                        }
                    }
                    data.add(rowList);
                }
            } 
        } catch (IOException ex) {
            Logger.getLogger(ExcelUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
        return data;
    }

    /** *            xls  xlsx,           excel   * @param file    * @return    */
    public static ArrayList<ArrayList<String>> read(String file){
        ArrayList<ArrayList<String>> data=new ArrayList<ArrayList<String>>();
        String fileType=file.substring(file.lastIndexOf(".")).toLowerCase();
        switch(fileType){
            case ".xls":
                data=readXLS(file);
                break;
            case ".xlsx":
                data=readXLSX(file);
                break;
            default:
                return null;             
        }
        return data;
    }

    public static void main(String args[]){
        String file="";
        ArrayList<ArrayList<String>> data=read(file);
        if(data!=null){
            for(ArrayList<String> list:data){
                System.out.println(list);
            }
        }
    }
}

좋은 웹페이지 즐겨찾기