tools.parsing.ExcelReader.java Source code

Java tutorial

Introduction

Here is the source code for tools.parsing.ExcelReader.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 tools.parsing;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
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 smyrgeorge
 */
public class ExcelReader {

    private final String filePath;
    private final ArrayList<Comment> comments;

    public ExcelReader(String filepath) {
        this.filePath = filepath;
        this.comments = new ArrayList<>();
    }

    public void readExcel() {
        try {
            FileInputStream file = new FileInputStream(new File(this.filePath));
            XSSFWorkbook workbook = new XSSFWorkbook(file);
            XSSFSheet sheet = workbook.getSheetAt(0);
            Iterator<Row> rowIterator = sheet.iterator();
            while (rowIterator.hasNext()) {
                Row row = rowIterator.next();
                Iterator<Cell> cellIterator = row.cellIterator();
                Comment comment = new Comment();
                while (cellIterator.hasNext()) {
                    Cell cell = cellIterator.next();
                    switch (cell.getCellType()) {
                    case Cell.CELL_TYPE_NUMERIC:
                        if (cell.getColumnIndex() == 0)
                            comment.setTopic(cell.getNumericCellValue() + "");
                        else if (cell.getColumnIndex() == 1)
                            comment.setCode(cell.getNumericCellValue() + "");
                        else if (cell.getColumnIndex() == 2)
                            comment.setAuthor(cell.getNumericCellValue() + "");
                        else if (cell.getColumnIndex() == 3)
                            comment.setDate(cell.getNumericCellValue() + "");
                        else
                            comment.setComment(cell.getNumericCellValue() + "");
                        break;
                    case Cell.CELL_TYPE_STRING:
                        if (cell.getColumnIndex() == 0)
                            comment.setTopic(cell.getStringCellValue());
                        else if (cell.getColumnIndex() == 1)
                            comment.setCode(cell.getStringCellValue());
                        else if (cell.getColumnIndex() == 2)
                            comment.setAuthor(cell.getStringCellValue());
                        else if (cell.getColumnIndex() == 3)
                            comment.setDate(cell.getStringCellValue());
                        else
                            comment.setComment(cell.getStringCellValue());
                        break;
                    }
                }
                this.comments.add(comment);
            }
            file.close();
        } catch (IOException e) {
            Logger.getLogger(ExcelReader.class.getName()).log(Level.SEVERE, null, e);
        }
    }

    public ArrayList<Comment> getComments() {
        return this.comments;
    }

}