diff --git a/build.gradle b/build.gradle index 0e38304..2645aa5 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ plugins { } //버전 변경은 여기서 -def commonVer = "beta.1.0.67" +def commonVer = "beta.1.0.68" group = 'com.kri.common' version = commonVer diff --git a/src/main/java/com/kri/common/excel/ExcelMapper.java b/src/main/java/com/kri/common/excel/ExcelMapper.java index be7df67..8d3ed37 100644 --- a/src/main/java/com/kri/common/excel/ExcelMapper.java +++ b/src/main/java/com/kri/common/excel/ExcelMapper.java @@ -4,6 +4,8 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.lang.reflect.Field; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Collection; import java.util.Comparator; @@ -296,6 +298,59 @@ public final class ExcelMapper { * @param clazz DTO 타입 * @param rowAccessWindowSize SXSSFWorkbook row window (예: 100, 500 등) */ + + public static void writeStreaming( + Path path, + List data, + Class clazz, + int rowAccessWindowSize + ) { + writeStreaming(path, data, clazz, rowAccessWindowSize, 1); + } + + public static void writeStreaming( + Path path, + List data, + Class clazz, + int rowAccessWindowSize, + int headerRowIndex + ) { + validateHeaderRowIndex(headerRowIndex); + + try (SXSSFWorkbook workbook = writeStreaming(data, clazz, rowAccessWindowSize, headerRowIndex); + OutputStream out = Files.newOutputStream(path)) { + workbook.write(out); + out.flush(); + } catch (IOException e) { + throw new RuntimeException("Streaming 엑셀 쓰기 중 오류", e); + } + } + + public static void writeStreamingFromMap( + Path path, + List> rows, + int rowAccessWindowSize + ) { + writeStreamingFromMap(path, rows, rowAccessWindowSize, 1); + } + + public static void writeStreamingFromMap( + Path path, + List> rows, + int rowAccessWindowSize, + int headerRowIndex + ) { + validateHeaderRowIndex(headerRowIndex); + + try (SXSSFWorkbook workbook = writeStreamingFromMap(rows, rowAccessWindowSize, headerRowIndex); + OutputStream out = Files.newOutputStream(path)) { + workbook.write(out); + out.flush(); + } catch (IOException e) { + throw new RuntimeException("Streaming Map 엑셀 쓰기 중 오류", e); + } + } + public static SXSSFWorkbook writeStreaming(List data, Class clazz, int rowAccessWindowSize) { @@ -441,6 +496,30 @@ public final class ExcelMapper { * - 첫 번째 시트만 읽음. * - headerRowIndex 행을 헤더로 간주하고, 그 다음 행부터 데이터를 읽음. */ + public static void readStreaming(Path path, RowMapHandler handler) { + readStreaming(path, 1, handler); + } + + public static void readStreaming(Path path, int headerRowIndex, RowMapHandler handler) { + try (OPCPackage pkg = OPCPackage.open(path.toFile())) { + XSSFReader reader = new XSSFReader(pkg); + SharedStringsTable sst = (SharedStringsTable) reader.getSharedStringsTable(); + + XMLReader parser = XMLReaderFactory.createXMLReader(); + SheetToMapHandler sheetHandler = new SheetToMapHandler(sst, handler, headerRowIndex); + parser.setContentHandler(sheetHandler); + + Iterator sheets = reader.getSheetsData(); + if (sheets.hasNext()) { + try (InputStream sheetIS = sheets.next()) { + parser.parse(new InputSource(sheetIS)); + } + } + } catch (Exception e) { + throw new RuntimeException("Streaming 엑셀 읽기 중 오류", e); + } + } + public static void readStreaming(InputStream inputStream, RowMapHandler handler) { readStreaming(inputStream, 1, handler); } @@ -607,6 +686,12 @@ public final class ExcelMapper { out.flush(); } + public void writeTo(Path path) throws IOException { + try (OutputStream out = Files.newOutputStream(path)) { + writeTo(out); + } + } + @Override public void close() throws IOException { workbook.dispose();