I am trying to convert .xlsx file to .csv, convertion is happening but the data is not formatted properly. Please find code below and suggest changes to the code.
Here I am trying to read an .xlsx file and write it to a csv file i.e. converting xlsx to csv but I am not getting the .csv file in proper format all the data is displayed in a single but it must displayed like rows in Excel.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class XlsxtoCSV {
static void xlsx(File inputFile, File outputFile) {
// For storing data into CSV files
StringBuffer data = new StringBuffer();
try {
FileOutputStream fos = new FileOutputStream(outputFile);
// Get the workbook object for XLSX file
XSSFWorkbook wBook = new XSSFWorkbook(new FileInputStream(inputFile));
// Get first sheet from the workbook
XSSFSheet sheet = wBook.getSheetAt(0);
Row row;
Cell cell;
// Iterate through each rows from first sheet
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
row = rowIterator.next();
// For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
cell = cellIterator.next();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
data.append(cell.getBooleanCellValue() + ",");
break;
case Cell.CELL_TYPE_NUMERIC:
data.append(cell.getNumericCellValue() + ",");
break;
case Cell.CELL_TYPE_STRING:
data.append(cell.getStringCellValue() + ",");
break;
case Cell.CELL_TYPE_BLANK:
data.append("" + ",");
break;
default:
data.append(cell + ",");
}
}
}
fos.write(data.toString().getBytes());
fos.close();
} catch (Exception ioe) {
ioe.printStackTrace();
}
}
//testing the application
public static void main(String[] args) {
//reading file from desktop
File inputFile = new File("C:\\Users\\user69\\Desktop\\test.xlsx");
//writing excel data to csv
File outputFile = new File("C:\\Users\\user69\\Desktop\\test1.csv");
xlsx(inputFile, outputFile);
}
}
Apache POI SS - HSSF & XSSF - Convert Worksheet to CSV Below code shows how worksheet can be converted to CSV using Apache POI HSSF and XSSF API as compared to Aspose.
Java developers can easily convert XLS file to CSV in just a few lines of code.
This is thanks @Swapnil!
data.append("\r\n"); // After the columns have been appended.
The following was edited (added) by @Abdullah My original answer above does not has that much impact, but Abdullah's edit shows much effort, so I leave it for those that encounter this question&answer.
public class App {
public void convertExcelToCSV(Sheet sheet, String sheetName) {
StringBuilder data = new StringBuilder();
try {
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
CellType type = cell.getCellTypeEnum();
if (type == CellType.BOOLEAN) {
data.append(cell.getBooleanCellValue());
} else if (type == CellType.NUMERIC) {
data.append(cell.getNumericCellValue());
} else if (type == CellType.STRING) {
String cellValue = cell.getStringCellValue();
if(!cellValue.isEmpty()) {
cellValue = cellValue.replaceAll("\"", "\"\"");
data.append("\"").append(cellValue).append("\"");
}
} else if (type == CellType.BLANK) {
} else {
data.append(cell + "");
}
if(cell.getColumnIndex() != row.getLastCellNum()-1) {
data.append(",");
}
}
data.append('\n');
}
Files.write(Paths.get("C:\\Users\\" + sheetName + ".csv"),
data.toString().getBytes("UTF-8"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String [] args)
{
App app = new App();
String path = "C:\\Users\\myFile.xlsx";
try (InputStream inp = new FileInputStream(path)) {
Workbook wb = WorkbookFactory.create(inp);
for (int i = 0; i < wb.getNumberOfSheets(); i++) {
System.out.println(wb.getSheetAt(i).getSheetName());
app.convertExcelToCSV(wb.getSheetAt(i), wb.getSheetAt(i).getSheetName());
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
Use Commons CSV to encode cell values, that's much more robust. Unfortunately, some extra code is still needed to iterate over the Sheet by Row/Cell and call Commons CSV on each cell (XSSF doesn't provide that), but at least the actual cell value being written is guaranteed to be standard CSV (i.e. you don't need to worry about escaping chars or adding commas yourself).
Maven add for Commons CSV:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.5</version>
</dependency>
Once Commons CSV is available, this is the code to export a Workbook to CSV. This examples writes to an OutputStream, but File output is also easy.
// Convert an XSSFWorkbook to CSV and write to provided OutputStream
private void writeWorkbookAsCSVToOutputStream(XSSFWorkbook workbook, OutputStream out) {
CSVPrinter csvPrinter = null;
try {
csvPrinter = new CSVPrinter(new OutputStreamWriter(out), CSVFormat.DEFAULT);
if (workbook != null) {
XSSFSheet sheet = workbook.getSheetAt(0); // Sheet #0 in this example
Iterator<Row> rowIterator = sheet.rowIterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
csvPrinter.print(cell.getStringCellValue());
}
csvPrinter.println(); // Newline after each row
}
}
}
catch (Exception e) {
log.error("Failed to write CSV file to output stream", e);
}
finally {
try {
if (csvPrinter != null) {
csvPrinter.flush(); // Flush and close CSVPrinter
csvPrinter.close();
}
}
catch (IOException ioe) {
log.error("Error when closing CSV Printer", ioe);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With