Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

converting excel to csv in java

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Iterator;

import org.apache.commons.io.FilenameUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
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
            FileInputStream fis = new FileInputStream(inputFile);
            Workbook workbook = null;

            String ext = FilenameUtils.getExtension(inputFile.toString());

            if (ext.equalsIgnoreCase("xlsx")) {
                workbook = new XSSFWorkbook(fis);
            } else if (ext.equalsIgnoreCase("xls")) {
                workbook = new HSSFWorkbook(fis);
            }

            // Get first sheet from the workbook

            int numberOfSheets = workbook.getNumberOfSheets();
            Row row;
            Cell cell;
            // Iterate through each rows from first sheet

            for (int i = 0; i < numberOfSheets; i++) {
                Sheet sheet = workbook.getSheetAt(0);
                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 + ",");

                        }
                    }
                    data.append('
'); // appending new line after each row
                }

            }
            fos.write(data.toString().getBytes());
            fos.close();

        } catch (Exception ioe) {
            ioe.printStackTrace();
        }
    }

    // testing the application

    public static void main(String[] args) {
        // int i=0;
        // reading file from desktop
        File inputFile = new File(".//src//test//resources//yourExcel.xls"); //provide your path
        // writing excel data to csv
        File outputFile = new File(".//src//test//resources//yourCSV.csv");  //provide your path
        xlsx(inputFile, outputFile);
        System.out.println("Conversion of " + inputFile + " to flat file: "
                + outputFile + " is completed");
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: how to add to a file in java 
Java :: java_home should point to a jdk not a jre 
Java :: float vs double java 
Java :: java reverse linked list 
Java :: language of minecraft 
Java :: java calendar hour vs hour of day 
Java :: java how to print a string[] 
Java :: collections.reverseorder() in java 
Java :: how to resize image in android programmatically 
Java :: what it means when create final variable in java 
Java :: java linkedlist print 
Java :: android alertdialog setSingleChoiceItems 
Java :: java loop through object 
Java :: jframe actionlistener 
Java :: java output array lists to file 
Java :: convert string to character array 
Java :: fill an array with random numbers between 1 and 100 java 
Java :: how to check the lines in a file java scanner 
Java :: swing setbounds 
Java :: Android dark theme programmatically 
Java :: map interation in java 
Java :: how to use random bound on doubles java 
Java :: linux command to see all the java version installed 
Java :: android list[index] 
Java :: java list distinct by attribute 
Java :: java 8 map foreach 
Java :: check palindrome in java 
Java :: check if sqlexception is duplicate entry java 
Java :: java create file in folder 
Java :: Java print class type 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =