update2viva.ConvertXLSX.java Source code

Java tutorial

Introduction

Here is the source code for update2viva.ConvertXLSX.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package update2viva;

/**
 *
 * @author jungoliver
 */
import java.io.*;
import java.util.Iterator;

import org.apache.poi.hssf.usermodel.HSSFSheet;
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.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ConvertXLSX {
    static void readXlsx(File inputFile, String outputfile)
            throws FileNotFoundException, UnsupportedEncodingException, IOException {
        String[] args;
        //File to store data in form of CSV
        File f = new File(outputfile + "\\out_.csv");

        OutputStream os = (OutputStream) new FileOutputStream(f);
        String encoding = "ISO-8859-1";
        OutputStreamWriter osw = new OutputStreamWriter(os, encoding);
        BufferedWriter bw = new BufferedWriter(osw);

        // Get the workbook instance for XLSX file
        XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(inputFile));

        // Get first sheet from the workbook
        XSSFSheet sheet = wb.getSheetAt(0);

        Row row;
        Cell cell;

        String acrow = "";
        for (int i = 0; i <= sheet.getLastRowNum(); i++) {
            row = sheet.getRow(i);

            for (int j = 0; j < row.getLastCellNum(); j++) {
                if (!(row.getCell(j) == null)) {
                    switch (row.getCell(j).getCellType()) {

                    case Cell.CELL_TYPE_BOOLEAN:
                        acrow = "" + row.getCell(j).getBooleanCellValue();
                        break;

                    case Cell.CELL_TYPE_NUMERIC:
                        acrow = "" + row.getCell(j).getNumericCellValue();
                        break;

                    case Cell.CELL_TYPE_STRING:
                        acrow = row.getCell(j).getStringCellValue();
                        break;

                    case Cell.CELL_TYPE_BLANK:
                        System.out.println(" ");
                        break;
                    }
                    // acrow=row.getCell(j).getStringCellValue();
                    if (acrow.contains("\n"))
                        acrow = acrow.replaceAll("\n", "");

                    if (!(acrow.contains("\n")))

                    {
                        bw.write(acrow + ";");
                    }
                }
                if (row.getCell(j) == null) {
                    bw.write(';');
                }
            }

            bw.newLine();
        }

        bw.flush();
        bw.close();
        inputFile.delete();
    }

    static void readXls(File inputFile) {
        try {
            // Get the workbook instance for XLS file
            HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(inputFile));
            // Get first sheet from the workbook
            HSSFSheet sheet = workbook.getSheetAt(0);
            Cell cell;
            Row row;

            // 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:
                        System.out.println(cell.getBooleanCellValue());
                        break;

                    case Cell.CELL_TYPE_NUMERIC:
                        System.out.println(cell.getNumericCellValue());
                        break;

                    case Cell.CELL_TYPE_STRING:
                        System.out.println(cell.getStringCellValue());
                        break;

                    case Cell.CELL_TYPE_BLANK:
                        System.out.println(" ");
                        break;

                    default:
                        System.out.println(cell);
                    }
                }
            }

        }

        catch (FileNotFoundException e) {
            System.err.println("Exception" + e.getMessage());
        } catch (IOException e) {
            System.err.println("Exception" + e.getMessage());
        }
    }

    public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException, IOException {
        File input = new File(args[0]);
        readXlsx(input, args[1]);
    }
}