Created
August 1, 2021 02:21
-
-
Save ajaysjournal/5b578f49e91f5323c7e7696b0822870f to your computer and use it in GitHub Desktop.
excel to csv sample https://stackoverflow.com/questions/18077264/convert-csv-to-xls-xlsx-using-apache-poi
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 org.apache.poi.ss.usermodel.Row; | |
import org.apache.poi.xssf.streaming.SXSSFSheet; | |
import org.apache.poi.xssf.streaming.SXSSFWorkbook; | |
import org.apache.poi.xssf.usermodel.XSSFRow; | |
import org.apache.poi.xssf.usermodel.XSSFSheet; | |
import org.apache.poi.xssf.usermodel.XSSFWorkbook; | |
import java.io.BufferedReader; | |
import java.io.FileOutputStream; | |
import java.io.FileReader; | |
import java.nio.file.Files; | |
import java.nio.file.Paths; | |
import java.util.concurrent.atomic.AtomicReference; | |
import java.util.stream.Stream; | |
public class Main { | |
public static void convertCsvToXlsx(String xlsLocation, String csvLocation, String sheetName) throws Exception { | |
SXSSFWorkbook workbook = new SXSSFWorkbook(); | |
SXSSFSheet sheet = (SXSSFSheet) workbook.createSheet(sheetName); | |
AtomicReference<Integer> row = new AtomicReference<>(0); | |
Files.readAllLines(Paths.get(csvLocation)).forEach(line -> { | |
Row currentRow = sheet.createRow(row.getAndSet(row.get() + 1)); | |
String[] nextLine = line.split(","); | |
Stream.iterate(0, i -> i + 1).limit(nextLine.length).forEach(i -> { | |
currentRow.createCell(i).setCellValue(nextLine[i]); | |
}); | |
}); | |
FileOutputStream fos = new FileOutputStream(xlsLocation); | |
workbook.write(fos); | |
fos.flush(); | |
} | |
public static void csvToXLSX() { | |
try { | |
String csvFileAddress = "test.csv"; //csv file address | |
String xlsxFileAddress = "test.xlsx"; //xlsx file address | |
XSSFWorkbook workBook = new XSSFWorkbook(); | |
XSSFSheet sheet = workBook.createSheet("sheet1"); | |
String currentLine = null; | |
int RowNum=0; | |
BufferedReader br = new BufferedReader(new FileReader(csvFileAddress)); | |
while ((currentLine = br.readLine()) != null) { | |
String str[] = currentLine.split(","); | |
RowNum++; | |
XSSFRow currentRow=sheet.createRow(RowNum); | |
for(int i=0;i<str.length;i++){ | |
currentRow.createCell(i).setCellValue(str[i]); | |
} | |
} | |
FileOutputStream fileOutputStream = new FileOutputStream(xlsxFileAddress); | |
workBook.write(fileOutputStream); | |
fileOutputStream.close(); | |
System.out.println("Done"); | |
} catch (Exception ex) { | |
System.out.println(ex.getMessage()+"Exception in try"); | |
} | |
} | |
public static void main(String[] args) throws Exception { | |
csvToXLSX(); | |
convertCsvToXlsx("test1.xlsx", "test.csv", "test"); | |
convertCsvToXlsx("test1.xlsx", "test.csv", "test2"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment