본문 바로가기
Java

[Java] Apache POI로 Excel 파일을 .txt로 변환하기

by teamnova 2025. 4. 16.
728x90

 

안녕하세요 오늘은 Apache POI 라이브러리를 사용하여 

엑셀 파일을 텍스트 파일로 변환해보도록 하겠습니다 

 

기존 엑셀 파일입니다 

 

 

 

Main.java

public class Main {
    public static void main(String[] args) throws Exception {
        FileInputStream fis = new FileInputStream("엑셀파일이 저장되어 있는 경로/vocab_sample.xlsx");
        Workbook workbook = new XSSFWorkbook(fis);
        Sheet sheet = workbook.getSheetAt(0);

        BufferedWriter writer = new BufferedWriter(new FileWriter("텍스트 파일을 저장할 경로/output.txt"));

        for (Row row : sheet) {
            Cell wordCell = row.getCell(0); // A열
            Cell meaningCell = row.getCell(1); // B열

            if (wordCell != null && meaningCell != null) {
                String word = wordCell.toString();
                String meaning = meaningCell.toString();
                writer.write(word + " - " + meaning);
                writer.newLine();
            }
        }

        writer.close();
        workbook.close();
        fis.close();

        System.out.println("단어장 생성 완료!");
    }

 

 

 

 

위 코드를 실행 시키면 지정된 경로에 엑셀 파일이 텍스트 파일로 변환되어 저장됩니다 

 

 

감사합니다