excel.Reader.java Source code

Java tutorial

Introduction

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

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
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.ss.usermodel.WorkbookFactory;

/**
 *
 * @author zach
 */

// TODO: Create Stats Generator for each column
public class Reader {

    private Workbook wb;

    public Reader(File excel) {
        System.out.println("CONSTRUCTOR");
        wb = null;
        try {
            wb = WorkbookFactory.create(excel);
        } catch (IOException e) {
            System.out.println("IO Exception");
            System.out.println(e.getMessage());
        } catch (InvalidFormatException e) {
            System.out.println("Invalid Format");
            System.out.println(e.getMessage());
        }
    }

    public boolean exists() {
        return (wb != null);
    }

    public void print() {
        System.out.println("START PRINT");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd");
        int columnWidth = 15;
        FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
        Sheet sheet = wb.getSheetAt(0);
        for (Row row : sheet) {
            //System.out.print("r");
            for (Cell cell : row) {
                //CellReference cellRef = new CellReference(row.getRowNum(), cell.getColumnIndex());
                //System.out.print(cellRef.formatAsString());
                //System.out.print(" - ");
                // System.out.print("c");
                switch (cell.getCellType()) {
                case Cell.CELL_TYPE_STRING:
                    //System.out.print("s");
                    System.out.printf("%-" + columnWidth + "s", cell.getRichStringCellValue().getString());
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    //System.out.print("d");
                    if (DateUtil.isCellDateFormatted(cell)) {
                        System.out.printf("%-" + columnWidth + "s", df.format(cell.getDateCellValue()));
                    } else {
                        if ((cell.getNumericCellValue() % 1.0) != 0.0)
                            System.out.printf("%-" + columnWidth + ".2f", cell.getNumericCellValue());
                        else
                            System.out.printf("%-" + columnWidth + ".0f", cell.getNumericCellValue());
                    }
                    break;
                case Cell.CELL_TYPE_BOOLEAN:
                    //System.out.print("b");
                    System.out.printf("%-" + columnWidth + "s", cell.getBooleanCellValue());
                    break;
                case Cell.CELL_TYPE_FORMULA:
                    CellValue val = evaluator.evaluate(cell);
                    //System.out.print("f");
                    switch (val.getCellType()) {
                    case Cell.CELL_TYPE_STRING:
                        System.out.printf("%-" + columnWidth + "s", val.getStringValue());
                        break;
                    case Cell.CELL_TYPE_NUMERIC:
                        System.out.printf("%-" + columnWidth + ".2f", val.getNumberValue());
                        break;
                    case Cell.CELL_TYPE_BOOLEAN:
                        System.out.printf("%-" + columnWidth + "s", val.getBooleanValue());
                        break;
                    default:
                        System.out.printf("%-" + columnWidth + "s", "");
                    }
                    break;
                default:
                    System.out.print("");
                }
            }
            System.out.println();
        }
    }

    public void load() {

    }

    public static void main(String[] args) {
        System.out.println("START PRG");
        //File f = new File("testData/SampleData2007.xls");
        File f = new File("testData/Efacility_dump_File_140320_basebridge.xlsx");
        System.out.println("PATH:" + f.getAbsoluteFile());
        if (!f.exists()) {
            System.out.println("File does not exist.");
            System.exit(0);
        }
        System.out.println("FILE");
        Reader r = new Reader(f);
        System.out.println("Reader");
        r.print();
        System.out.println("PRG DONE");
    }
}