Last active
August 29, 2015 14:10
-
-
Save yellowsnow/08886a7fa1f037b0cc15 to your computer and use it in GitHub Desktop.
YES SIR : Yet another Excel Sheet Simple Iterator Reader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedWriter | |
import java.io.File | |
import java.io.FileInputStream | |
import java.io.FileNotFoundException | |
import java.io.FileWriter | |
import java.io.FilenameFilter | |
import java.io.IOException | |
import java.util.ArrayList | |
import org.apache.poi.openxml4j.exceptions.InvalidFormatException | |
import org.apache.poi.ss.usermodel.Cell | |
import org.apache.poi.ss.usermodel.DataFormatter | |
import org.apache.poi.ss.usermodel.DateUtil | |
import org.apache.poi.ss.usermodel.FormulaEvaluator | |
import org.apache.poi.ss.usermodel.Row | |
import org.apache.poi.ss.usermodel.Sheet | |
import org.apache.poi.ss.usermodel.Workbook | |
import org.apache.poi.ss.usermodel.WorkbookFactory | |
/* | |
Maven | |
<dependency> | |
<groupId>org.apache.poi</groupId> | |
<artifactId>poi</artifactId> | |
<version>3.10.1</version> | |
</dependency> | |
<dependency> | |
<groupId>org.apache.poi</groupId> | |
<artifactId>poi-ooxml</artifactId> | |
<version>3.10.1</version> | |
</dependency> | |
<dependency> | |
<groupId>org.apache.poi</groupId> | |
<artifactId>ooxml-schemas</artifactId> | |
<version>1.1</version> | |
</dependency> | |
*/ | |
/* | |
@Grapes([ | |
@Grab(group='org.apache.poi', module='poi', version='3.10.1'), | |
@Grab(group='org.apache.poi', module='poi-ooxml', version='3.10.1') | |
@Grab(group='org.apache.poi', module='ooxml-schemas', version='1.1') | |
]) | |
*/ | |
class ExcelSheetReader implements Iterable { | |
def file | |
def sheetNum = 0 | |
int skipLines = 1 | |
int skipColumns = 0 | |
Iterator iterator(){ | |
def formatter,sheet,workbook,evaluator | |
def lastRowNum = 0 | |
def excel = file instanceof String ? new File(file) : file | |
workbook = WorkbookFactory.create(excel) | |
evaluator = workbook.getCreationHelper().createFormulaEvaluator() | |
formatter = new DataFormatter(true) | |
sheet = workbook.getSheetAt(sheetNum ?: 0) | |
lastRowNum = sheet.physicalNumberOfRows | |
int j = skipLines ?: 0 | |
return [hasNext:{ | |
j < lastRowNum | |
}, | |
next:{ | |
def record = [] | |
def row = sheet.getRow(j++) | |
int lastCellNum = row.lastCellNum | |
if (lastCellNum >= 0) { | |
(0..(lastCellNum-1)).each{i-> | |
if (i>=skipColumns) { | |
record << getCellValueByType(row.getCell(i)) | |
} | |
} | |
} | |
record | |
} | |
] as Iterator | |
} | |
private getCellValueByType(cell,_cellType=null) { | |
if (!cell) { | |
return null | |
} | |
def cellType = _cellType ?: cell.cellType | |
def result | |
switch(cellType) { | |
case Cell.CELL_TYPE_STRING: | |
result = cell.richStringCellValue.string; | |
break; | |
case Cell.CELL_TYPE_NUMERIC: | |
if (DateUtil.isCellDateFormatted(cell)) { | |
result = cell.dateCellValue | |
} else { | |
result = cell.numericCellValue | |
} | |
break; | |
case Cell.CELL_TYPE_BOOLEAN: | |
result = cell.booleanCellValue | |
break; | |
case Cell.CELL_TYPE_FORMULA: | |
result = showFormulas ? cell.cellFormula : getCellValueByType(cell, cell.cachedFormulaResultType) | |
break | |
} | |
return result | |
} | |
/* | |
static main(args){ | |
//Example Usage | |
new ExcelSheetReader(file:args[0],sheetNum:1,skipLines:2,skipColumns:4).eachWithIndex{row,i-> | |
println "${++i} : ${row.join('\t')}" | |
} | |
} | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment