Skip to content

Instantly share code, notes, and snippets.

@shawnma
Created October 23, 2018 04:28
Show Gist options
  • Save shawnma/1ef6ffa3156d05a4e25a298d6778fd09 to your computer and use it in GitHub Desktop.
Save shawnma/1ef6ffa3156d05a4e25a298d6778fd09 to your computer and use it in GitHub Desktop.
@Grab("org.apache.poi:poi-ooxml:3.13")
@Grab("org.apache.poi:poi:3.13")
@Grab("com.opencsv:opencsv:3.8")
@GrabExclude('stax:stax-api:')
import com.opencsv.*
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
String file = args[0]
def book = new XSSFWorkbook(file)
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file + ".csv"), "UTF-8"))
CSVWriter writer = new CSVWriter(out)
def sheet = book.getSheetAt(0)
def cols = sheet.getRow(0).getPhysicalNumberOfCells()
(0..(sheet.getPhysicalNumberOfRows())).forEach{
rowid->
def row=sheet.getRow(rowid)
def rowStr = row.cellIterator().collect{ cell-> cell.stringCellValue }
writer.writeNext(rowStr.toArray(new String[rowStr.size()]))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment