com.ddt.driver.EXCELDriver.java Source code

Java tutorial

Introduction

Here is the source code for com.ddt.driver.EXCELDriver.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 com.ddt.driver;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
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;

/**
 *
 * @author leonardo
 */
public class EXCELDriver extends Driver {

    public EXCELDriver(String path) {
        super();
        getDriver(path, null);
    }

    public EXCELDriver(String path, String sheetName) {
        super();
        getDriver(path, sheetName);
    }

    private void getDriver(String path, String sheetName) {
        if (path.split("\\.")[1].equals("xlsx")) {
            readNewerVersionExcelFile(path, sheetName);
        } else if (path.split("\\.")[1].equals("xls")) {
            readOlderVersionExcelFile(path, sheetName);
        }
    }

    private void readOlderVersionExcelFile(String path, String sheetName) {
        try {

            HSSFWorkbook workbook;

            try (FileInputStream file = new FileInputStream(new File(path))) {

                workbook = new HSSFWorkbook(file);
                HSSFSheet sheet = workbook.getSheetAt(0);
                Iterator<Row> rowIterator = sheet.iterator();

                while (rowIterator.hasNext()) {
                    String line = "";
                    Row row = rowIterator.next();
                    Iterator<Cell> cellIterator = row.cellIterator();

                    while (cellIterator.hasNext()) {
                        Cell cell = cellIterator.next();

                        switch (cell.getCellType()) {
                        case Cell.CELL_TYPE_BOOLEAN:
                            line += cell.getBooleanCellValue() + ";";
                            break;
                        case Cell.CELL_TYPE_NUMERIC:
                            line += String.valueOf(cell.getNumericCellValue()).split("\\.")[0] + ";";
                            break;
                        case Cell.CELL_TYPE_STRING:
                            line += cell.getStringCellValue() + ";";
                            break;
                        }
                    }
                    linesOfDriver.add(line);
                }
            }
        } catch (FileNotFoundException e) {
            Logger.getLogger(EXCELDriver.class.getName()).log(Level.SEVERE, null, e);
        } catch (IOException e) {
            Logger.getLogger(EXCELDriver.class.getName()).log(Level.SEVERE, null, e);
        }
    }

    private void readNewerVersionExcelFile(String path, String sheetName) {
        try {
            try (final FileInputStream file = new FileInputStream(new File(path))) {
                XSSFWorkbook workbook = new XSSFWorkbook(file);
                XSSFSheet sheet = getSheetName(sheetName, workbook);
                Iterator<Row> rowIterator = sheet.iterator();

                while (rowIterator.hasNext()) {
                    String line = "";
                    Row row = rowIterator.next();
                    Iterator<Cell> cellIterator = row.cellIterator();

                    while (cellIterator.hasNext()) {
                        Cell cell = cellIterator.next();

                        switch (cell.getCellType()) {
                        case Cell.CELL_TYPE_BOOLEAN:
                            line += cell.getBooleanCellValue() + ";";
                            break;
                        case Cell.CELL_TYPE_NUMERIC:
                            line += String.valueOf(cell.getNumericCellValue()).split("\\.")[0] + ";";
                            break;
                        case Cell.CELL_TYPE_STRING:
                            line += cell.getStringCellValue() + ";";
                            break;
                        }
                    }
                    linesOfDriver.add(line);
                }
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(EXCELDriver.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(EXCELDriver.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private XSSFSheet getSheetName(String sheetName, XSSFWorkbook workbook) {
        if (sheetName == null) {
            return workbook.getSheetAt(0);
        } else {
            return workbook.getSheet(sheetName);
        }
    }
}