import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
public class CsvToXlsxConverter {
public static void main(String[] args) {
String csvFilePath = "input.csv";
String xlsxFilePath = "output.xlsx";
try {
convertCsvToXlsx(csvFilePath, xlsxFilePath);
System.out.println("CSV 파일이 XLSX 파일로 성공적으로 변환되었습니다.");
} catch (IOException e) {
System.err.println("파일 변환 중 오류가 발생했습니다: " + e.getMessage());
}
}
public static void convertCsvToXlsx(String csvFilePath, String xlsxFilePath) throws IOException {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet1");
try (BufferedReader br = Files.newBufferedReader(Paths.get(csvFilePath))) {
String line;
int rowIndex = 0;
while ((line = br.readLine()) != null) {
Row row = sheet.createRow(rowIndex++);
String[] values = line.split(",");
for (int colIndex = 0; colIndex < values.length; colIndex++) {
Cell cell = row.createCell(colIndex);
cell.setCellValue(values[colIndex].trim());
}
}
}
try (FileOutputStream fos = new FileOutputStream(xlsxFilePath)) {
workbook.write(fos);
} finally {
workbook.close();
}
}
}