Skip to content

Instantly share code, notes, and snippets.

@VadimKirilchuk
Created February 10, 2018 12:51
Show Gist options
  • Save VadimKirilchuk/f92cc54f7cbfc2071e6f598aebacc819 to your computer and use it in GitHub Desktop.
Save VadimKirilchuk/f92cc54f7cbfc2071e6f598aebacc819 to your computer and use it in GitHub Desktop.
Convert CSV to XLSX (Streaming)
#! /usr/bin/env groovy
@Grab("org.apache.poi:poi:3.16")
@Grab("org.apache.poi:poi-ooxml:3.16")
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.*;
/** more java than groovy, will make fancier later **/
def csvToXLSX() {
try {
String csvFileAddress = "autos.csv"; //csv file address
String xlsxFileAddress = "autos.xlsx"; //xlsx file address
SXSSFWorkbook workBook = new SXSSFWorkbook();
workBook.setCompressTempFiles(true);
SXSSFSheet sheet = workBook.createSheet("sheet1");
sheet.setRandomAccessWindowSize(1000);
String currentLine = null;
int RowNum = 0;
BufferedReader br = new BufferedReader(new FileReader(csvFileAddress));
while ((currentLine = br.readLine()) != null) {
String[] str = currentLine.split(",");
Row currentRow = sheet.createRow(RowNum);
for(int i=0;i<str.length;i++){
currentRow.createCell(i).setCellValue(str[i]);
}
RowNum++;
if (RowNum % 1000 == 0) {
println RowNum;
}
}
FileOutputStream fileOutputStream = new FileOutputStream(xlsxFileAddress);
workBook.write(fileOutputStream);
fileOutputStream.close();
System.out.println("Done");
} catch (Exception ex) {
ex.printStackTrace();
}
}
println "hello";
csvToXLSX();
@EvgenJin
Copy link

EvgenJin commented Jun 3, 2020

спасибо от души то, что нужно

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment