-
-
Save EvgenJin/fccc700dcfe62c3518f4b78f98c4e893 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
@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.*; | |
def csvToExelNotExists() { | |
try { | |
String csvFileAddress = "./test.csv"; | |
String xlsxFileAddress = "./autos.xlsx"; | |
SXSSFWorkbook workBook = new SXSSFWorkbook(); | |
// Set whether temp files should be compressed. | |
workBook.setCompressTempFiles(true); | |
SXSSFSheet sheet = workBook.createSheet("sheet2"); | |
// Specifies how many rows can be accessed at most via getRow(). | |
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); | |
str.eachWithIndex{col,index -> | |
currentRow.createCell(index).setCellValue(str[index]); | |
} | |
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(); | |
} | |
} | |
def csvToExelExists() { | |
try { | |
String csvFileAddress = "./test.csv"; | |
FileInputStream myxls = new FileInputStream("./autos.xlsx"); | |
XSSFWorkbook studentsSheet = new XSSFWorkbook(myxls); | |
XSSFSheet worksheet = studentsSheet.getSheetAt(0); | |
int lastRow=worksheet.getLastRowNum(); | |
System.out.println(lastRow); | |
String currentLine = null; | |
int RowNum = lastRow+1; | |
BufferedReader br = new BufferedReader(new FileReader(csvFileAddress)); | |
br.readLine() // skip 1 line | |
while ((currentLine = br.readLine()) != null) { | |
String[] str = currentLine.split(","); | |
// Row row = worksheet.createRow(++lastRow); | |
Row row = worksheet.createRow(RowNum); | |
str.eachWithIndex{col,index -> | |
row.createCell(index).setCellValue(str[index]); | |
} | |
RowNum++; | |
} | |
// Row row = worksheet.createRow(++lastRow); | |
// row.createCell(1).setCellValue("Dr.Hola"); | |
myxls.close(); | |
FileOutputStream output_file =new FileOutputStream(new File("./autos2.xlsx")); | |
studentsSheet.write(output_file); | |
output_file.close(); | |
System.out.println(" is successfully written"); | |
} | |
catch(Exception ex) { | |
ex.printStackTrace(); | |
} | |
} | |
csvToExelExists(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment