260310_01_ENH: 엑셀 매퍼에 디스크에서 파일 stream read write 추가

This commit is contained in:
2026-03-10 08:30:46 +09:00
parent 03a368b120
commit c1ec207911
2 changed files with 86 additions and 1 deletions

View File

@@ -4,7 +4,7 @@ plugins {
}
//버전 변경은 여기서
def commonVer = "beta.1.0.67"
def commonVer = "beta.1.0.68"
group = 'com.kri.common'
version = commonVer

View File

@@ -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 <T> void writeStreaming(
Path path,
List<T> data,
Class<T> clazz,
int rowAccessWindowSize
) {
writeStreaming(path, data, clazz, rowAccessWindowSize, 1);
}
public static <T> void writeStreaming(
Path path,
List<T> data,
Class<T> 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<? extends Map<String, ?>> rows,
int rowAccessWindowSize
) {
writeStreamingFromMap(path, rows, rowAccessWindowSize, 1);
}
public static void writeStreamingFromMap(
Path path,
List<? extends Map<String, ?>> 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 <T> SXSSFWorkbook writeStreaming(List<T> data,
Class<T> 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<InputStream> 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();