From c1ec207911889f23ed34afafb4f0bd273f88cd48 Mon Sep 17 00:00:00 2001 From: kric_jspark Date: Tue, 10 Mar 2026 08:30:46 +0900 Subject: [PATCH] =?UTF-8?q?260310=5F01=5FENH:=20=EC=97=91=EC=85=80=20?= =?UTF-8?q?=EB=A7=A4=ED=8D=BC=EC=97=90=20=EB=94=94=EC=8A=A4=ED=81=AC?= =?UTF-8?q?=EC=97=90=EC=84=9C=20=ED=8C=8C=EC=9D=BC=20stream=20read=20write?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 +- .../com/kri/common/excel/ExcelMapper.java | 85 +++++++++++++++++++ 2 files changed, 86 insertions(+), 1 deletion(-) 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();