com.read.main.LeerPDF.java Source code

Java tutorial

Introduction

Here is the source code for com.read.main.LeerPDF.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.read.main;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import org.apache.poi.*;
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 aaron
 */
public class LeerPDF {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        try {

            FileInputStream file = new FileInputStream(new File("/home/aaron/Escritorio/Example.xlsx"));
            XSSFWorkbook workbook2 = new XSSFWorkbook(file);
            XSSFSheet sheet = workbook2.getSheetAt(0);
            Iterator<Row> rowIterator = sheet.iterator();

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

                System.out.println("Numero de Columnas: " + row.getLastCellNum());

                System.out.println(row.getRowNum());

                if (row.getRowNum() == 0) {
                    System.out.println("Fila Cero");
                } else {

                    int numColumna = 0;

                    while (numColumna < row.getLastCellNum()) {

                        Cell cell = row.getCell(numColumna);

                        try {
                            switch (cell.getCellType()) {
                            case Cell.CELL_TYPE_BOOLEAN:
                                System.out.print(numColumna + ".- BOOLEAN: ");
                                System.out.print(cell.getBooleanCellValue() + "\t\t");
                                break;
                            case Cell.CELL_TYPE_NUMERIC:
                                System.out.print(numColumna + ".- NUMERIC: ");
                                System.out.print(cell.getNumericCellValue() + "\t\t");
                                break;
                            case Cell.CELL_TYPE_STRING:
                                System.out.print(numColumna + ".- STRING: ");
                                System.out.print(cell.getStringCellValue() + "\t\t");
                                break;
                            }
                        } catch (Exception e) {
                            System.err.println(e);
                        }
                        ;

                        numColumna++;
                    }
                }

                System.out.println("");
            }
            file.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}