Spring Boot

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

공대생안씨 2024. 1. 8. 00:05

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 실행결과