package com.eactive.eai.rms.common.excel;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;

import org.apache.log4j.Logger;


public class ExcelUtil {
	
	private static final Logger logger = Logger.getLogger(ExcelUtil.class);
	
    public static Sheet[] loadExcelFile(String fileName) {
    	//File file = new File(fileName);

        InputStream in = null;

        try {
            /**
             * find sheets
             */
            in = new BufferedInputStream(new FileInputStream(fileName));
            Sheet[] sheets = null;
         
            try {
            	int idx = fileName.lastIndexOf(".");
        		//파일 확장자
        		String fileExtension = "."+fileName.substring(idx+1);
                if(fileExtension.equals(".xls")){
                	sheets = ExcelSheet.getExcelSheets(in);
                }else{
                	sheets = ExcelSheet.getExcelSheetsForXlsx(in);
                }
            } catch (Exception e) {
                throw new Exception("엑셀화일이 유효하지 않습니다 - '" + fileName + "'", e);
            }
            return sheets;

        } catch (Exception e) {
        	//e.printStackTrace();
        	logger.error("loadExcelFile error", e); 
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (Exception e) {
                }
            }
        }
		return null;
    }  
}
