[Spring Boot] 스프링으로 엑셀 파일 읽기

2024. 1. 8. 00:05· Spring Boot
목차
  1. 1. build.gradle
  2. 2. ReadExcel.java
  3. 3. 실행결과
  4. 3-1. student.xlsx 에 저장된 데이터
  5. 3-2. ReadExcel 실행결과

1. build.gradle

plugins {
	id 'java'
	id 'org.springframework.boot' version '3.1.5'
	id 'io.spring.dependency-management' version '1.1.3'
}

group = 'practice'
version = '0.0.1-SNAPSHOT'

java {
	sourceCompatibility = '21'
}

configurations {
	compileOnly {
		extendsFrom annotationProcessor
	}
}

repositories {
	mavenCentral()
}

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
	implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
	implementation 'org.springframework.boot:spring-boot-starter-web'

	// https://mvnrepository.com/artifact/org.apache.poi/poi
	implementation group: 'org.apache.poi', name: 'poi', version: '4.1.2'
	implementation group: 'org.apache.poi', name: 'poi-ooxml', version: '4.1.2'
	// https://mvnrepository.com/artifact/org.apache.tika/tika-core
	implementation group: 'org.apache.tika', name: 'tika-core', version: '2.3.0'


	compileOnly 'org.projectlombok:lombok'
	developmentOnly 'org.springframework.boot:spring-boot-devtools'
	annotationProcessor 'org.projectlombok:lombok'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.named('bootBuildImage') {
	builder = 'paketobuildpacks/builder-jammy-base:latest'
}

tasks.named('test') {
	useJUnitPlatform()
}

 

2. ReadExcel.java

public class ReadExcel {
    public static void main(String[] args) {
        try{
        	// 프로젝트의 루트 경로 바로 아래에 student.xlsx 저장 (상대경로)
            FileInputStream file = new FileInputStream("student.xlsx");
            IOUtils.setByteArrayMaxOverride(Integer.MAX_VALUE);

            XSSFWorkbook workbook = new XSSFWorkbook(file);

            XSSFSheet sheet = workbook.getSheetAt(0);

            Iterator<Row> rowIterator = sheet.iterator();
            while (rowIterator.hasNext()) {
                Row row = rowIterator.next();

                Iterator<Cell> cellIterator = row.cellIterator();

                while(cellIterator.hasNext()){
                    Cell cell = cellIterator.next();

                    switch (cell.getCellType()) {
                        // 숫자 타입
                        case NUMERIC:
                            System.out.println("cell.getNumericCellValue() = " + cell.getNumericCellValue() + "\t");
                            break;
                        // 문자 타입
                        case STRING:
                            System.out.println("cell.getStringCellValue() = " + cell.getStringCellValue() + "\t");
                            break;
                        // 숫자나 문자가 아닌 다른 타입인 경우 에러 출력
                        default:
                            throw new IllegalStateException("Unexpected value:" + cell.getCellType());
                    }
                }
                System.out.println("");
            }
            file.close();
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}

 

3. 실행결과

3-1. student.xlsx 에 저장된 데이터

 

3-2. ReadExcel 실행결과

 

저작자표시 변경금지 (새창열림)

'Spring Boot' 카테고리의 다른 글

[Spring Boot] 로그인 기능 구현 (5) - 구글 로그인 (OAuth 2.0)  (0) 2024.01.14
[Spring Boot] 로그인 기능 구현 (4) - 스프링 시큐리티 사용 JWT 로그인  (1) 2024.01.13
[Spring Boot] 로그인 기능 구현 (3) - 스프링 시큐리티 로그인  (4) 2024.01.07
[Spring Boot] 로그인 기능 구현 (2) - 세션 로그인  (0) 2024.01.07
[Spring Boot] 로그인 기능 구현 (1) - 쿠키 로그인  (0) 2024.01.07
  1. 1. build.gradle
  2. 2. ReadExcel.java
  3. 3. 실행결과
  4. 3-1. student.xlsx 에 저장된 데이터
  5. 3-2. ReadExcel 실행결과
'Spring Boot' 카테고리의 다른 글
  • [Spring Boot] 로그인 기능 구현 (5) - 구글 로그인 (OAuth 2.0)
  • [Spring Boot] 로그인 기능 구현 (4) - 스프링 시큐리티 사용 JWT 로그인
  • [Spring Boot] 로그인 기능 구현 (3) - 스프링 시큐리티 로그인
  • [Spring Boot] 로그인 기능 구현 (2) - 세션 로그인
공대생안씨
공대생안씨
전자공학과 학부생의 코딩 일기
공대생의 코딩 일기전자공학과 학부생의 코딩 일기
티스토리
|
로그인
공대생안씨
공대생의 코딩 일기
공대생안씨
글쓰기
|
관리
전체
오늘
어제
  • All Categories (153)
    • Spring Boot (46)
      • JPA (7)
      • Lombok (2)
    • Java (21)
    • DevOps (3)
      • CI,CD (8)
      • Monitoring (2)
    • Database (7)
      • MySQL (5)
      • MongoDB (1)
      • H2 (1)
    • Trouble Shooting (5)
    • FE (4)
    • IntelliJ (3)
    • Git (3)
    • Algorithm (41)

블로그 메뉴

  • 홈
  • 태그
  • Github

공지사항

인기 글

hELLO · Designed By 정상우.v4.2.2
공대생안씨
[Spring Boot] 스프링으로 엑셀 파일 읽기
상단으로

티스토리툴바

개인정보

  • 티스토리 홈
  • 포럼
  • 로그인

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.