깜놀하는 해므찌로

Spring Controller Java 엑셀파일 읽기 메소드 예시 / POI 라이브러리 본문

IT

Spring Controller Java 엑셀파일 읽기 메소드 예시 / POI 라이브러리

agnusdei1207 2022. 9. 18. 09:11
반응형
SMALL
 @SuppressWarnings({ "resource", "deprecation" })
public List<SearchVO> xlsxReadList(String filePath) throws Exception{

    List<SearchVO> outArray = new ArrayList<>();
    FileInputStream fis = null;
    XSSFWorkbook workbook = null;

    try{ 
        fis = new FileInputStream(filePath);
        workbook = new XSSFWorkbook(fis); // 경로를 받아 XLS 파일을 읽는다.

        XSSFSheet curSheet;
        XSSFRow curRow;
        XSSFCell curCell;
        SearchVO outData;
        String tarDir = "c:\\test";
        // 현재 sheet 반환 (첫번째시트 : 0)
        curSheet = workbook.getSheetAt(0);

        // row 길이
        int rowLength = curSheet.getPhysicalNumberOfRows();

        // row(세로데이터) 탐색 for문 (row 0은 헤더정보이기 때문에 1부터 시작)
        for(int rowIndex=1; rowIndex < rowLength; rowIndex++){
            // 현재 row 반환
            curRow = curSheet.getRow(rowIndex);
            outData = new SearchVO();
            String value;

            // cell 길이
            int cellLength = curRow.getPhysicalNumberOfCells();

            // cell 최대 길이 
            int cellEnd = curRow.getLastCellNum();

            System.out.println("cellLength :: "+ cellLength);
            System.out.println("cellEnd :: "+ cellEnd);

            // cell(가로데이터) 탐색 for문
            for(int cellIndex = 0; cellIndex <= cellEnd; cellIndex++){

                curCell = curRow.getCell(cellIndex);
                // cell 스타일이 다르더라도 String으로 변환 받음
                if(curCell != null){
                    switch (curCell.getCellType()){
                        case HSSFCell.CELL_TYPE_FORMULA :
                            value = curCell.getCellFormula(); 
                            break;
                        case HSSFCell.CELL_TYPE_NUMERIC :
                            value = (int)curCell.getNumericCellValue()+"";
                            break;
                        case HSSFCell.CELL_TYPE_STRING :
                            value = curCell.getStringCellValue()+"";
                            break;
                        case HSSFCell.CELL_TYPE_BLANK :
                            //value = curCell.getBooleanCellValue()+"";
                            value = "";
                        break;
                            case HSSFCell.CELL_TYPE_ERROR :
                            value = curCell.getErrorCellValue()+"";
                        break;
                            default :
                            value = new String();
                        break;
                    } 
                }else{
                    value = "-"; 
                } 
                if(cellIndex ==  0){  outData.setSeq(value);}            
                if(cellIndex ==  1){  outData.setWorkDiv(value);}               
                if(cellIndex ==  2){  outData.setPosition(value);}           
                if(cellIndex ==  3){  outData.setId(value);}            
                if(cellIndex ==  4){  outData.setName(value);}           
                if(cellIndex ==  5){  outData.setEmail(value);}             
                if(cellIndex ==  6){  outData.setRegDate(value);}          
            } // for
            outArray.add(outData);
        } // for

        }catch(FileNotFoundException e){

            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }finally{
            try{
                if(workbook != null) {
                    workbook = null;
                }
                if(fis != null){
                    fis.close();
                }
            }catch(IOException e){
                e.printStackTrace();
            } 
        }

    return outArray;
    }
반응형
LIST