QuickExcel.ExcelData.java Source code

Java tutorial

Introduction

Here is the source code for QuickExcel.ExcelData.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 QuickExcel;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

/**
 *
 * @author Angel
 */
public class ExcelData {

    private ArrayList<String> mQuestions;
    private ArrayList<String> mAnswers;

    public ExcelData() {

        mQuestions = new ArrayList<>();
        mAnswers = new ArrayList<>();
    }

    public void loadData() {
        try {
            File file = new File("excel.xls");
            POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file));
            HSSFWorkbook wb = new HSSFWorkbook(fs);
            HSSFSheet sheet = wb.getSheetAt(0);
            HSSFRow row;
            HSSFCell cell;

            int rows; // No of rows
            rows = sheet.getPhysicalNumberOfRows();

            int cols = 0; // No of columns
            int tmp = 0;

            // This trick ensures that we get the data properly even if it doesn't start from first few rows
            for (int i = 0; i < 10 || i < rows; i++) {
                row = sheet.getRow(i);
                if (row != null) {
                    tmp = sheet.getRow(i).getPhysicalNumberOfCells();
                    if (tmp > cols) {
                        cols = tmp;
                    }
                }
            }

            for (int r = 0; r < rows; r++) {
                row = sheet.getRow(r);
                if (row != null) {
                    for (int c = 0; c < 2; c++) {
                        cell = row.getCell(c);
                        if (c == 0) {
                            if (cell != null) {
                                addQuestion(cell.toString());
                            }

                        } else {
                            if (cell != null) {
                                addAnswer(cell.toString());
                            }

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

    public String getAnswer(int aIndex) {
        return mAnswers.get(aIndex);
    }

    public void addQuestion(String aQuestion) {
        mQuestions.add(aQuestion);
    }

    public void addAnswer(String aAnswer) {
        mAnswers.add(aAnswer);
    }

    public int searchQuestion(String aInput) {

        int index = -1;

        for (String question : mQuestions) {

            if (question.contains(aInput)) {
                index = mQuestions.indexOf(question);
            }
        }

        return index;
    }
}