package cmm.util;

import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ExcelUtil {

	/** LOGGER */
	private static final Logger LOGGER = LoggerFactory.getLogger(ExcelUtil.class);
	
    /** 다운로드 버퍼 크기 */
    // private static final int BUFFER_SIZE = 8192; // 8kb

    /** 문자 인코딩 */
    public final static String CHARSET = "utf-8";
    
    public final static String CELL_STYLE_TITLE = "1";
    public final static String CELL_STYLE_TITLE2 = "11";
    public final static String CELL_STYLE_TITLE3 = "12";
    public final static String CELL_STYLE_HEADER = "2";
    
    public final static String CELL_STYLE_DATA_DEFAULT_CENTER = "3";
    public final static String CELL_STYLE_DATA_DEFAULT_LEFT = "4";
    public final static String CELL_STYLE_DATA_DEFAULT_RIGHT = "5";
    public final static String CELL_STYLE_DATA_DATE_HYPHEN = "6";
    public final static String CELL_STYLE_DATA_DATE_SLASH = "7";
    public final static String CELL_STYLE_DATA_NUMBER = "8";
    public final static String CELL_STYLE_DATA_NUMBER_DECIMAL_1 = "9";
    public final static String CELL_STYLE_DATA_NUMBER_DECIMAL_2 = "10";
    
    public final static int CELL_SIZE_DEFAULT = 4500;
    
    public final static String DATALIST_MAP = "map";
    public final static String DATALIST_VO = "vo";
  
    
    private String filePath;
    private List<String> outputColumns;  
    private int startRow;
   
    public static void xlsxFileDownByEgovMap(
        HttpServletResponse response,
        String fileName,
        String sheetName,
        String headerTitle,
        String[] headerNames,
        String[] dataKeys,
        String[] dataType,
        int[] cellSizes,
        List<DataMap> dataList) throws Exception
    {
        // 워크북 생성
        XSSFWorkbook wb = new XSSFWorkbook();

        // 시트 생성
        XSSFSheet sheet = null;
        if (StringUtils.isNotEmpty(sheetName))
        {
            sheet = wb.createSheet(sheetName);
        }
        else
        {
            sheet = wb.createSheet();
        }
        
        // 기본 Cell Style
        Map<String, CellStyle> styles = createStyles(wb);
        
        int headerNamesLength = 1;
        if (headerNames != null && headerNames.length > 0) headerNamesLength = headerNames.length;
        
        // 셀크기지정
        if (cellSizes == null || cellSizes.length == 0)
        {
            for (int i = 0; i < headerNamesLength; i++)
            {
                sheet.setColumnWidth(i, CELL_SIZE_DEFAULT);
            }
        }
        else
        {
            for (int i = 0; i < headerNamesLength; i++)
            {
                sheet.setColumnWidth(i, cellSizes[i] * 256);
            }
        }

        Row titleRow = sheet.createRow(0);
        titleRow.setHeightInPoints(45);
        Cell titleCell = titleRow.createCell(0);
        titleCell.setCellValue(headerTitle);
        titleCell.setCellStyle(styles.get(CELL_STYLE_TITLE));
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, headerNamesLength-1));
        
        Row headerRow = sheet.createRow(1);
        headerRow.setHeightInPoints(40);
        Cell headerCell;
        for (int i = 0; i < headerNamesLength; i++) {
            headerCell = headerRow.createCell(i);
            headerCell.setCellValue(headerNames[i]);
            headerCell.setCellStyle(styles.get(CELL_STYLE_HEADER));
        }
        
        Row dataRow = null;
        
        Cell dataCell = null;
        
        DataMap dataMap = null;
        if (dataList != null && dataList.size() > 0)
        {
            for (int i = 0; i < dataList.size(); i++)
            {
                dataMap = (DataMap) dataList.get(i);
                dataRow = sheet.createRow(i + 2);
                dataCell = null;
                if (dataKeys != null && dataKeys.length > 0)
                {
                    for (int j = 0; j < dataKeys.length; j++)
                    {
                        Object dataObj = (Object)(dataMap.get(dataKeys[j]));
                        if (dataObj != null) {
                            String dataTypeNm = dataObj.getClass().getName();
                            
                            dataCell = dataRow.createCell(j);
                            dataCell.setCellStyle(styles.get(dataType[j]));
                            if (dataTypeNm.contains("BigDecimal"))
                            {
                                dataCell.setCellValue(((BigDecimal)(dataMap.get(dataKeys[j]))).doubleValue());
                            }
                            else if (dataTypeNm.contains("Integer"))
                            {
                                dataCell.setCellValue((Integer)(dataMap.get(dataKeys[j])));
                            }
                            else if (dataTypeNm.contains("Long"))
                            {
                                dataCell.setCellValue((Long)(dataMap.get(dataKeys[j])));
                            }
                            else if (dataTypeNm.contains("Date"))
                            {
                                dataCell.setCellValue((Date)(dataMap.get(dataKeys[j])));
                            }
                            else
                            {
                                dataCell.setCellValue((String)(dataMap.get(dataKeys[j])));
                            }
                        }
                        else
                        {
                            dataCell = dataRow.createCell(j);
                            dataCell.setCellStyle(styles.get(CELL_STYLE_DATA_DEFAULT_CENTER));
                            dataCell.setCellValue("");
                        }

                    }
                }
            }
        }
        
        BufferedOutputStream outs = null;
        OutputStream out = null;
        try
        {
        	response.reset();
            response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8") + ";");
            out = response.getOutputStream();
            outs = new BufferedOutputStream(out);
            wb.write(outs);
            out.flush();
        }
        catch (IOException e){
            LOGGER.error("fail in xlsxFileDownByEgovMap()", e);
        } catch (Exception e){
            LOGGER.error("fail in xlsxFileDownByEgovMap()", e);
        } finally {
        	if(out != null){
        		out.close();
        	}
 			if(outs != null){
 				outs.close();
 			}
        }
    }
    
    public static void xlsxFileDownByEgovMap2(
        HttpServletResponse response,
        String fileName,
        String sheetName,
        String headerTitle,
        String[] headerNames1,
        String[] headerNames2,
        int [][] headerMerged,
        String[] dataKeys,
        String[] dataType,
        int[] cellSizes,
        List<DataMap> dataList) throws Exception
    {
        XSSFWorkbook wb = new XSSFWorkbook();

        XSSFSheet sheet = null;
        if (StringUtils.isNotEmpty(sheetName))
        {
            sheet = wb.createSheet(sheetName);
        }
        else
        {
            sheet = wb.createSheet();
        }
        
        Map<String, CellStyle> styles = createStyles(wb);
        
        int headerNamesLength = 1;
        if (headerNames1 != null && headerNames1.length > 0) headerNamesLength = headerNames1.length;
        
        int headerMergedLength = 0;
        if (headerMerged != null && headerMerged.length > 0) headerMergedLength = headerMerged.length;
        
        if (cellSizes == null || cellSizes.length == 0)
        {
            for (int i = 0; i < headerNamesLength; i++)
            {
                sheet.setColumnWidth(i, CELL_SIZE_DEFAULT);
            }
        }
        else
        {
            for (int i = 0; i < headerNamesLength; i++)
            {
                sheet.setColumnWidth(i, cellSizes[i] * 256);
            }
        }

        Row titleRow = sheet.createRow(0);
        titleRow.setHeightInPoints(45);
        Cell titleCell = titleRow.createCell(0);
        titleCell.setCellValue(headerTitle);
        titleCell.setCellStyle(styles.get(CELL_STYLE_TITLE));
        
        
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, headerNamesLength-1));
        
        Row headerRow1 = sheet.createRow(1);
        headerRow1.setHeightInPoints(40);
        Cell headerCell1;
        for (int i = 0; i < headerNamesLength; i++)
        {
            //if (AtEduStringUtils.isNotEmpty(headerNames1[i]))
            //{
                headerCell1 = headerRow1.createCell(i);
                headerCell1.setCellValue(headerNames1[i]);
                headerCell1.setCellStyle(styles.get(CELL_STYLE_HEADER));
            //}
        }
        
        Row headerRow2 = sheet.createRow(2);
        headerRow2.setHeightInPoints(40);
        Cell headerCell2;
        for (int i = 0; i < headerNamesLength; i++)
        {
            //if (AtEduStringUtils.isNotEmpty(headerNames2[i]))
            //{
                headerCell2 = headerRow2.createCell(i);
                headerCell2.setCellValue(headerNames2[i]);
                headerCell2.setCellStyle(styles.get(CELL_STYLE_HEADER));
            //}
        }
        
        for (int i = 0; i < headerMergedLength; i++)
        {
            sheet.addMergedRegion(new CellRangeAddress(headerMerged[i][0], headerMerged[i][1], headerMerged[i][2], headerMerged[i][3]));
        }
        
        Row dataRow = null;
        
        Cell dataCell = null;
        
        DataMap dataMap = null;
        if (dataList != null && dataList.size() > 0)
        {
            for (int i = 0; i < dataList.size(); i++)
            {
                dataMap = (DataMap) dataList.get(i);
                dataRow = sheet.createRow(i + 3);
                dataCell = null;
                if (dataKeys != null && dataKeys.length > 0)
                {
                    for (int j = 0; j < dataKeys.length; j++)
                    {
                        Object dataObj = (Object)(dataMap.get(dataKeys[j]));
                        if (dataObj != null) {
                            String dataTypeNm = dataObj.getClass().getName();
                            
                            dataCell = dataRow.createCell(j);
                            dataCell.setCellStyle(styles.get(dataType[j]));
                            if (dataTypeNm.contains("BigDecimal"))
                            {
                                dataCell.setCellValue(((BigDecimal)(dataMap.get(dataKeys[j]))).doubleValue());
                            }
                            else if (dataTypeNm.contains("Integer"))
                            {
                                dataCell.setCellValue((Integer)(dataMap.get(dataKeys[j])));
                            }
                            else if (dataTypeNm.contains("Long"))
                            {
                                dataCell.setCellValue((Long)(dataMap.get(dataKeys[j])));
                            }
                            else if (dataTypeNm.contains("Date"))
                            {
                                dataCell.setCellValue((Date)(dataMap.get(dataKeys[j])));
                            }
                            else
                            {
                                dataCell.setCellValue((String)(dataMap.get(dataKeys[j])));
                            }
                        }
                        else
                        {
                            dataCell = dataRow.createCell(j);
                            dataCell.setCellStyle(styles.get(CELL_STYLE_DATA_DEFAULT_CENTER));
                            dataCell.setCellValue("");
                        }

                    }
                }
            }
        }
        
        BufferedOutputStream outs = null;
        try
        {
        	response.reset();
            response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8") + ";");
            outs = new BufferedOutputStream(response.getOutputStream());
            wb.write(outs);
            outs.close();
        }
        catch (IOException e){
        	LOGGER.error("fail in xlsxFileDownByEgovMap2()", e);
        } catch (Exception e){
        	LOGGER.error("fail in xlsxFileDownByEgovMap2()", e);
        }
        
        response.getOutputStream().flush();
        response.getOutputStream().close();
    }
    
    /**
     * Create a library of cell styles
     */
    public static Map<String, CellStyle> createStyles(Workbook wb)
    {
        Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
        CellStyle style;
        
        // DDDDDDDDDDDDDDDDDDDDDD 2021.08.13 임시 막음 버전차이로 인한 상수값 부재 
        /*
        Font titleFont = wb.createFont();
        titleFont.setFontHeightInPoints((short) 18);
        titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
        style = wb.createCellStyle();
        style.setAlignment(CellStyle.ALIGN_CENTER);
        style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
        style.setFont(titleFont);
        styles.put(CELL_STYLE_TITLE, style);
        
        Font title2Font = wb.createFont();
        title2Font.setFontHeightInPoints((short) 13);
        title2Font.setBoldweight(Font.BOLDWEIGHT_BOLD);
        style = wb.createCellStyle();
        style.setAlignment(CellStyle.ALIGN_LEFT);
        style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
        style.setFont(title2Font);
        styles.put(CELL_STYLE_TITLE2, style);
        
        Font title3Font = wb.createFont();
        title3Font.setFontHeightInPoints((short) 10);
        title3Font.setBoldweight(Font.BOLDWEIGHT_BOLD);
        style = wb.createCellStyle();
        style.setAlignment(CellStyle.ALIGN_LEFT);
        style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
        style.setFont(title3Font);
        styles.put(CELL_STYLE_TITLE3, style);

        Font headerFont = wb.createFont();
        headerFont.setFontHeightInPoints((short) 11);
        headerFont.setColor(IndexedColors.WHITE.getIndex());
        style = wb.createCellStyle();
        style.setAlignment(CellStyle.ALIGN_CENTER);
        style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
        style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
        style.setFillPattern(CellStyle.SOLID_FOREGROUND);
        style.setFont(headerFont);
        style.setWrapText(true);
        style.setBorderRight(CellStyle.BORDER_THIN);
        style.setRightBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderLeft(CellStyle.BORDER_THIN);
        style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderTop(CellStyle.BORDER_THIN);
        style.setTopBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderBottom(CellStyle.BORDER_THIN);
        style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
        styles.put(CELL_STYLE_HEADER, style);

        style = wb.createCellStyle();
        style.setAlignment(CellStyle.ALIGN_CENTER);
        style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
        style.setWrapText(true);
        style.setBorderRight(CellStyle.BORDER_THIN);
        style.setRightBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderLeft(CellStyle.BORDER_THIN);
        style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderTop(CellStyle.BORDER_THIN);
        style.setTopBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderBottom(CellStyle.BORDER_THIN);
        style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
        styles.put(CELL_STYLE_DATA_DEFAULT_CENTER, style);
        
        style = wb.createCellStyle();
        style.setAlignment(CellStyle.ALIGN_RIGHT);
        style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
        style.setWrapText(true);
        style.setBorderRight(CellStyle.BORDER_THIN);
        style.setRightBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderLeft(CellStyle.BORDER_THIN);
        style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderTop(CellStyle.BORDER_THIN);
        style.setTopBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderBottom(CellStyle.BORDER_THIN);
        style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
        styles.put(CELL_STYLE_DATA_DEFAULT_RIGHT, style);
        
        style = wb.createCellStyle();
        style.setAlignment(CellStyle.ALIGN_LEFT);
        style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
        style.setWrapText(true);
        style.setBorderRight(CellStyle.BORDER_THIN);
        style.setRightBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderLeft(CellStyle.BORDER_THIN);
        style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderTop(CellStyle.BORDER_THIN);
        style.setTopBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderBottom(CellStyle.BORDER_THIN);
        style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
        styles.put(CELL_STYLE_DATA_DEFAULT_LEFT, style);

        style = wb.createCellStyle();
        style.setAlignment(CellStyle.ALIGN_CENTER);
        style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
        style.setDataFormat(wb.createDataFormat().getFormat("yyyy-mm-dd"));
        style.setBorderRight(CellStyle.BORDER_THIN);
        style.setRightBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderLeft(CellStyle.BORDER_THIN);
        style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderTop(CellStyle.BORDER_THIN);
        style.setTopBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderBottom(CellStyle.BORDER_THIN);
        style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
        styles.put(CELL_STYLE_DATA_DATE_HYPHEN, style);

        style = wb.createCellStyle();
        style.setAlignment(CellStyle.ALIGN_CENTER);
        style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
        style.setDataFormat(wb.createDataFormat().getFormat("yyyy/mm/dd"));
        style.setBorderRight(CellStyle.BORDER_THIN);
        style.setRightBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderLeft(CellStyle.BORDER_THIN);
        style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderTop(CellStyle.BORDER_THIN);
        style.setTopBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderBottom(CellStyle.BORDER_THIN);
        style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
        styles.put(CELL_STYLE_DATA_DATE_SLASH, style);
        
        style = wb.createCellStyle();
        style.setAlignment(CellStyle.ALIGN_RIGHT);
        style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
        style.setDataFormat(wb.createDataFormat().getFormat("#,##0"));
        style.setBorderRight(CellStyle.BORDER_THIN);
        style.setRightBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderLeft(CellStyle.BORDER_THIN);
        style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderTop(CellStyle.BORDER_THIN);
        style.setTopBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderBottom(CellStyle.BORDER_THIN);
        style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
        styles.put(CELL_STYLE_DATA_NUMBER, style);
        
        style = wb.createCellStyle();
        style.setAlignment(CellStyle.ALIGN_RIGHT);
        style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
        style.setDataFormat(wb.createDataFormat().getFormat("#,##0.0"));
        style.setBorderRight(CellStyle.BORDER_THIN);
        style.setRightBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderLeft(CellStyle.BORDER_THIN);
        style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderTop(CellStyle.BORDER_THIN);
        style.setTopBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderBottom(CellStyle.BORDER_THIN);
        style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
        styles.put(CELL_STYLE_DATA_NUMBER_DECIMAL_1, style);

        style = wb.createCellStyle();
        style.setAlignment(CellStyle.ALIGN_RIGHT);
        style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
        style.setDataFormat(wb.createDataFormat().getFormat("#,##0.00"));
        style.setBorderRight(CellStyle.BORDER_THIN);
        style.setRightBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderLeft(CellStyle.BORDER_THIN);
        style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderTop(CellStyle.BORDER_THIN);
        style.setTopBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderBottom(CellStyle.BORDER_THIN);
        style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
        styles.put(CELL_STYLE_DATA_NUMBER_DECIMAL_2, style);
        */
        
        return styles;
    }
    
    
    //추가
    public static Workbook getWorkbook(String filePath) {
        
        /*
         * FileInputStream은 파일의 경로에 있는 파일을
         * 읽어서 Byte로 가져온다.
         * 
         * 파일이 존재하지 않는다면은
         * RuntimeException이 발생된다.
         */
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(filePath);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
        
        Workbook wb = null;
        
        /*
         * 파일의 확장자를 체크해서 .XLS 라면 HSSFWorkbook에
         * .XLSX라면 XSSFWorkbook에 각각 초기화 한다.
         */
        if(filePath.toUpperCase().endsWith(".XLS")) {
            try {
                wb = new HSSFWorkbook(fis);
            } catch (IOException e) {
                throw new RuntimeException(e.getMessage(), e);
            }
        }
        else if(filePath.toUpperCase().endsWith(".XLSX")) {
            try {
                wb = new XSSFWorkbook(fis);
            } catch (IOException e) {
                throw new RuntimeException(e.getMessage(), e);
            }
        }
        
        return wb;
    }
    
    

    
    public static String getName(Cell cell, int cellIndex) {
        int cellNum = 0;
        if(cell != null) {
            cellNum = cell.getColumnIndex();
        }
        else {
            cellNum = cellIndex;
        }
        
        return CellReference.convertNumToColString(cellNum);
    }
    
    public static String getValue(Cell cell) {
        String value = "";
        
        if(cell == null) {
            value = "";
        }
        else {
        	// DDDDDDDDDDDDDDDDDDDDDD 2021.08.13 임시 막음 버전차이로 인한 상수값 부재 
            /*
            if( cell.getCellType() == Cell.CELL_TYPE_FORMULA ) {
                value = cell.getCellFormula();
            }
            else if( cell.getCellType() == Cell.CELL_TYPE_NUMERIC ) {
                value = cell.getNumericCellValue() + "";
            }
            else if( cell.getCellType() == Cell.CELL_TYPE_STRING ) {
                value = cell.getStringCellValue();
            }
            else if( cell.getCellType() == Cell.CELL_TYPE_BOOLEAN ) {
                value = cell.getBooleanCellValue() + "";
            }
            else if( cell.getCellType() == Cell.CELL_TYPE_ERROR ) {
                value = cell.getErrorCellValue() + "";
            }
            else if( cell.getCellType() == Cell.CELL_TYPE_BLANK ) {
                value = "";
            }
            else {
                value = cell.getStringCellValue();
            }
            */
        }
        
        return value;
    }

    public static List<Map<String, String>> read(ExcelUtil excelReadOption) {
        //엑셀 파일 자체
        //엑셀파일을 읽어 들인다.
        //FileType.getWorkbook() <-- 파일의 확장자에 따라서 적절하게 가져온다.
        Workbook wb = getWorkbook(excelReadOption.getFilePath());
        /**
         * 엑셀 파일에서 첫번째 시트를 가지고 온다.
         */
        Sheet sheet = wb.getSheetAt(0);
        
        //System.out.println("Sheet 이름: "+ wb.getSheetName(0)); 
        //System.out.println("데이터가 있는 Sheet의 수 :" + wb.getNumberOfSheets());
        /**
         * sheet에서 유효한(데이터가 있는) 행의 개수를 가져온다.
         */
        int numOfRows = sheet.getPhysicalNumberOfRows();
        int numOfCells = 0;
        
        Row row = null;
        Cell cell = null;
        
        String cellName = "";
        /**
         * 각 row마다의 값을 저장할 맵 객체
         * 저장되는 형식은 다음과 같다.
         * put("A", "이름");
         * put("B", "게임명");
         */
        Map<String, String> map = null;
        /*
         * 각 Row를 리스트에 담는다.
         * 하나의 Row를 하나의 Map으로 표현되며
         * List에는 모든 Row가 포함될 것이다.
         */
        List<Map<String, String>> result = new ArrayList<Map<String, String>>(); 
        /**
         * 각 Row만큼 반복을 한다.
         */
        for(int rowIndex = excelReadOption.getStartRow() - 1; rowIndex < numOfRows; rowIndex++) {
            /*
             * 워크북에서 가져온 시트에서 rowIndex에 해당하는 Row를 가져온다.
             * 하나의 Row는 여러개의 Cell을 가진다.
             */
            row = sheet.getRow(rowIndex);
            
            if(row != null) {
                /*
                 * 가져온 Row의 Cell의 개수를 구한다.
                 */
                numOfCells = row.getPhysicalNumberOfCells();
                /*
                 * 데이터를 담을 맵 객체 초기화
                 */
                map = new HashMap<String, String>();
                /*
                 * cell의 수 만큼 반복한다.
                 */
                for(int cellIndex = 0; cellIndex < numOfCells; cellIndex++) {
                    /*
                     * Row에서 CellIndex에 해당하는 Cell을 가져온다.
                     */
                    cell = row.getCell(cellIndex);
                    /*
                     * 현재 Cell의 이름을 가져온다
                     * 이름의 예 : A,B,C,D,......
                     */
                    cellName = getName(cell, cellIndex);
                    /*
                     * 추출 대상 컬럼인지 확인한다
                     * 추출 대상 컬럼이 아니라면, 
                     * for로 다시 올라간다
                     */
                    if( !excelReadOption.getOutputColumns().contains(cellName) ) {
                        continue;
                    }
                    /*
                     * map객체의 Cell의 이름을 키(Key)로 데이터를 담는다.
                     */
                    map.put(cellName, getValue(cell));
                }
                /*
                 * 만들어진 Map객체를 List로 넣는다.
                 */
                result.add(map);
                
            }
            
        }
        
        return result;
        
    }
    
    
    
    public String getFilePath() {
        return filePath;
    }
    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }
    public List<String> getOutputColumns() {
        
        List<String> temp = new ArrayList<String>();
        temp.addAll(outputColumns);
        
        return temp;
    }
    public void setOutputColumns(List<String> outputColumns) {
        
        List<String> temp = new ArrayList<String>();
        temp.addAll(outputColumns);
        
        this.outputColumns = temp;
    }
    
    public void setOutputColumns(String ... outputColumns) {
        
        if(this.outputColumns == null) {
            this.outputColumns = new ArrayList<String>();
        }
        
        for(String ouputColumn : outputColumns) {
            this.outputColumns.add(ouputColumn);
        }
    }
    
    public int getStartRow() {
        return startRow;
    }
    public void setStartRow(int startRow) {
        this.startRow = startRow;
    }


    
}
