메뉴 건너뛰기

조회 수 4391 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
자바의 JXL 라이브러리를 활용하여 엑셀파일을 읽은 후 ArrayList 리턴받기

※ 참고사항
공개된 JXL 라이브러리는 자바 1.4 버전에 컴파일된 듯 하다. 버전이 맞지않아 오류가 발생할 수 있으니 주의하자.
서버에 업로드된 엑셀파일을 열어 HashMap 에 Cell을 담고 List 에 Row를 담아 리턴하는 소스이다.

 ExcelUtils.java


/**
 * @class ExcelUtils
 * @brief 
 *
 * registered date 20100908
 * programmed by Seok Kyun. Choi. 최석균
 * http://syaku.tistory.com
 */

package com.syaku.util;

import java.util.*;
import java.io.File;
import jxl.*;

import org.apache.log4j.Logger;

public class ExcelUtils {
  private static Logger log = Logger.getLogger(ExcelUtils.class);

  public List xlsForList(String file_path) throws Exception {
    File file = new File(file_path);

    if (!file.exists()) {
      throw new Exception("파일이 존재하지 않습니다.");
    }

    Workbook workbook = null;
    Sheet sheet = null;
    List list = new ArrayList();

    try {

      workbook = Workbook.getWorkbook(file);
      sheet = workbook.getSheet(0);

      int row = sheet.getRows();
      int col = sheet.getColumns();

      if(row <= 0) { throw new Exception("내용이 없습니다."); }

      for(int i = 0; i < row ; i++) {
        HashMap hm = new HashMap();
        for(int o = 0; o < col ; o++) {
          hm.put("COL"+o,sheet.getCell(o,i).getContents());
        }

        list.add(i,hm);
      }

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

    return list;
  }

}



 예제소스

String excel_file = "/excel.jsp"; // 절대경로의 엑셀파일

ExcelUtils excel = new ExcelUtils();
List result = excel.xlsForList(excel_file);

for (int i =1; i < result.size(); i++) {
  HashMap hm2 = (HashMap)result.get(i);

  out.println(hm2.get("COL0"));
  out.println(hm2.get("COL1"));
  out.println(hm2.get("COL2"));
  out.println(hm2.get("COL3"));
  out.println(hm2.get("COL4"));
  out.println(hm2.get("COL5"));
  out.println(hm2.get("COL6"));
  out.println(hm2.get("COL7"));
  out.println(hm2.get("COL8"));
  out.println(hm2.get("COL9"));
  out.println(hm2.get("COL10"));
  out.println(hm2.get("COL11"));
  out.println(hm2.get("COL12"));
}




List of Articles
번호 제목 날짜 조회 수
111 public static void main(String [] args) 2016.09.13 3143
110 Reflection을 활용한 메서드, 필드 값 불러오기. 2021.03.31 124
109 request header 로부터 접속 정보 확인 file 2023.02.15 75
108 Select statements cannot be executed as an update. 에러 해결방법 file 2016.08.29 3853
107 set get 파라미터 2016.08.18 3658
106 Singleton Pattern 과 DeadLock file 2023.02.15 74
105 Spring Boot 프로젝트 생성 file 2016.09.02 4160
104 static 2016.09.13 3354
103 static멤버와 관련된 예제 2016.09.21 3768
102 System.out.println(); 이클립스에서 자동화기능 사용 file 2016.09.19 5158
101 TCP 소켓 프로그래밍 01 - Server/Client 일대일 연결 file 2021.03.31 119
100 war로 묶지 않아도 컴파일된 소스 위치 확인하기 file 2016.08.29 4116
99 XML to JSON , JSON to Map 2020.06.29 262
98 [객체 지향 언어의 이해] 업캐스팅과 다운캐스팅 file 2021.03.31 157
97 [자바(스프링&mybatis&jsp) 프로젝트 & 아파치 &톰켓 연동 ] 이클립스 프로젝트 생성 순서04.jdbc 드라이버 설치 file 2016.08.18 4209
96 [자바(스프링&mybatis&jsp) 프로젝트 & 아파치 &톰켓 연동 ] 이클립스 프로젝트 생성 순서07.commons-collection 설치 file 2016.08.18 3845
95 [자바(스프링&mybatis&jsp) 프로젝트 & 아파치 &톰켓 연동 ] 이클립스 프로젝트 생성 순서08.commons-logging 설치 file 2016.08.18 4458
94 [자바(스프링&mybatis&jsp) 프로젝트 & 아파치 &톰켓 연동 ]11. 이클립스 프로젝트 생성 file 2016.08.18 3972
93 [자바(스프링&mybatis&jsp) 프로젝트 & 아파치 &톰켓 연동 ]9. 이클립스 압타나 플러그인 설치 file 2016.08.18 4012
92 [자바(스프링&mybatis&jsp) 프로젝트 & 아파치 &톰켓 연동]3. 이클립스 설치 file 2016.08.18 3756
Board Pagination Prev 1 2 3 4 5 6 7 8 Next
/ 8

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved