Created
February 10, 2018 12:51
-
-
Save VadimKirilchuk/f92cc54f7cbfc2071e6f598aebacc819 to your computer and use it in GitHub Desktop.
Convert CSV to XLSX (Streaming)
This file contains hidden or 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
#! /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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
спасибо от души то, что нужно