* 엑셀 중복 처리 방지 코드 추가 close #5
This commit is contained in:
@@ -18,11 +18,13 @@ public class ExcelRowReader implements ItemStreamReader<Row> {
|
||||
private FileInputStream fileInputStream;
|
||||
private Workbook workbook;
|
||||
private Iterator<Row> rowCursor;
|
||||
|
||||
private int currentRowNumber;
|
||||
private final String CURRENT_ROW_KEY = "current.row.number";
|
||||
|
||||
public ExcelRowReader(String filePath) throws IOException {
|
||||
|
||||
this.filePath = filePath;
|
||||
this.currentRowNumber = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -34,10 +36,16 @@ public class ExcelRowReader implements ItemStreamReader<Row> {
|
||||
Sheet sheet = workbook.getSheetAt(0);
|
||||
this.rowCursor = sheet.iterator();
|
||||
|
||||
// 동일 배치 파라미터에 대해 특정 키 값 "current.row.number"의 값이 존재한다면 초기화
|
||||
if (executionContext.containsKey(CURRENT_ROW_KEY)) {
|
||||
currentRowNumber = executionContext.getInt(CURRENT_ROW_KEY);
|
||||
}
|
||||
|
||||
if (rowCursor.hasNext()) {
|
||||
// 위의 값을 가져와 이미 실행한 부분은 건너 뜀
|
||||
for (int i = 0; i < currentRowNumber && rowCursor.hasNext(); i++) {
|
||||
rowCursor.next();
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
throw new ItemStreamException(e);
|
||||
}
|
||||
@@ -47,12 +55,18 @@ public class ExcelRowReader implements ItemStreamReader<Row> {
|
||||
public Row read() {
|
||||
|
||||
if (rowCursor != null && rowCursor.hasNext()) {
|
||||
currentRowNumber++;
|
||||
return rowCursor.next();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(ExecutionContext executionContext) throws ItemStreamException {
|
||||
executionContext.putInt(CURRENT_ROW_KEY, currentRowNumber);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws ItemStreamException {
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ public class ExcelRowWriter implements ItemStreamWriter<BeforeEntity> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(Chunk<? extends BeforeEntity> chunk) throws Exception {
|
||||
public void write(Chunk<? extends BeforeEntity> chunk) {
|
||||
for (BeforeEntity entity : chunk) {
|
||||
Row row = sheet.createRow(rowIndex++);
|
||||
row.createCell(0).setCellValue(entity.getUsername());
|
||||
|
||||
@@ -83,4 +83,4 @@ public class FifthBatch {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user