Teste.importarExcel.java Source code

Java tutorial

Introduction

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

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 *
 * @author Alessandro
 */
public class importarExcel {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        try {
            //get arquivo
            File file = new File("C:\\Users\\Alessandro\\Desktop\\TCC2\\IMPORTAR\\Alunos.xls");

            //get extensao do arquivo
            String name = file.toString();
            int pos = name.lastIndexOf('.');
            String ext = name.substring(pos + 1);

            FileInputStream fileIn = new FileInputStream(file);
            Workbook obj = null;

            if (ext.equals("xlsx")) {
                try {
                    //Metodo aceita o path do arquivo  
                    obj = new XSSFWorkbook(fileIn);
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            } else if (ext.equals("xls")) {
                try {
                    //Metodo nao aceita string do path do arquivo  
                    obj = new HSSFWorkbook(fileIn);
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            } else {
                throw new IllegalArgumentException("Received file does not have a standard excel extension.");
            }

            int o = 0;
            Sheet worksheet = obj.getSheet("Plan1");
            Row row;
            Cell cell;

            for (int i = 0; i <= worksheet.getLastRowNum(); i++) {
                row = worksheet.getRow(i);
                String linha = "";
                for (int j = 0; j < 2; j++) {
                    cell = row.getCell(j);

                    if (cell.getCellType() == 1) {
                        linha += " | " + cell.getStringCellValue();
                    } else {
                        double aux = 0;
                        int aux2 = 0;
                        aux = cell.getNumericCellValue();
                        aux2 = (int) aux;
                        linha += " | " + aux2;
                    }
                }

                System.out.println(linha);
            }

        } catch (FileNotFoundException ex) {
            System.out.println("Arquivo no encontrado");
        }
    }

}