Example usage for org.apache.poi.hssf.usermodel HSSFCell toString

List of usage examples for org.apache.poi.hssf.usermodel HSSFCell toString

Introduction

In this page you can find the example usage for org.apache.poi.hssf.usermodel HSSFCell toString.

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the cell This method returns a simple representation, anything more complex should be in user code, with knowledge of the semantics of the sheet being processed.

Usage

From source file:test.gov.nih.nci.cacoresdk.PropertyGenerator.java

License:BSD License

private int countRow(HSSFRow row) {
    int nonEmptyCount = 0;
    if (row == null)
        return 0;
    for (int i = 0; i < row.getLastCellNum(); i++) {
        HSSFCell currentCell = row.getCell(i);
        if (currentCell != null) {
            currentCell.setCellType(currentCell.CELL_TYPE_STRING);
            if (!(currentCell.toString().equals("")))
                nonEmptyCount++;//from w ww .jav a 2s . c o  m
        }
    }
    return nonEmptyCount;
}

From source file:test.gov.nih.nci.cacoresdk.PropertyGenerator.java

License:BSD License

private String getFirstString(HSSFRow row) {
    for (int i = 0; i < row.getLastCellNum(); i++) {
        HSSFCell currentCell = row.getCell(i);
        if (currentCell != null) {
            currentCell.setCellType(currentCell.CELL_TYPE_STRING);
            if (!(currentCell.toString().equals("")))
                return currentCell.toString();
        }/*from ww  w .  j av a  2 s .  co  m*/
    }
    return "";
}

From source file:Teste.importarExcel2.java

/**
 * funo para importar o arquivo de Trabalho
 *
 *///  www .j  av a 2  s. co  m
@SuppressWarnings("unchecked")
private static void importarTrabalho(List dataHolder) {

    String erros = "";
    //camea na linha 1 porque a linha 0  cabealho
    int qtdImportado = 0;
    String totalDeLinhas = String.valueOf(dataHolder.size() - 1);

    for (int i = 1; i < dataHolder.size(); i++) {
        List linha = (List) dataHolder.get(i);
        Aluno aluno = new Aluno();
        Curso curso = new Curso();
        Trabalho trab = new Trabalho();
        Defesa defesa = new Defesa();
        Assunto assunto = new Assunto();
        Perfil perfil = new Perfil();
        Professor orientador = new Professor();
        Empresa empresa = new Empresa();
        Banca banca = new Banca();
        Bancastatus bancaStatus = new Bancastatus();
        Convidado conv1 = new Convidado();
        Convidado conv2 = new Convidado();
        Trabalhostatus trabStatus = new Trabalhostatus();
        Modalidade modalidade = new Modalidade();
        Estagio estagio = new Estagio();

        //----------------------------------------------------
        //tipo de estgio
        HSSFCell myCellEstagioTipo = (HSSFCell) linha.get(18);
        myCellEstagioTipo.setCellType(Cell.CELL_TYPE_STRING);
        String stringCellEstagioTipo = myCellEstagioTipo.toString().trim();
        //----------------------------------------------------

        //----------------------------------------------------
        //data defesa
        HSSFCell myCellDataDefesa = (HSSFCell) linha.get(12);
        myCellDataDefesa.setCellType(Cell.CELL_TYPE_STRING);
        String stringCellDataDefesa = myCellDataDefesa.toString().trim();
        //----------------------------------------------------

        for (int col = 0; col < linha.size(); col++) {
            HSSFCell myCell = (HSSFCell) linha.get(col);
            myCell.setCellType(Cell.CELL_TYPE_STRING);
            String stringCellValue = myCell.toString().trim();

            switch (col) {
            case 0:
                /*matricula aluno*/
                aluno.setMatricula(stringCellValue);
                break;
            case 1:
                /*nome aluno*/
                aluno.setNome(stringCellValue);
                break;
            case 2:
                /*saber o curso*/
                if (stringCellValue.equalsIgnoreCase("BCC")
                        || stringCellValue.equalsIgnoreCase("Cincia da Computao - Curriculo Antigo")) {
                    stringCellValue = "Cincia da Computao - Curriculo Antigo";
                } else if (stringCellValue.equalsIgnoreCase("BCC")
                        || stringCellValue.equalsIgnoreCase("Cincia da Computao - Curriculo Novo")) {
                    stringCellValue = "Cincia da Computao - Curriculo Novo";
                } else if (stringCellValue.equalsIgnoreCase("BCC")
                        || stringCellValue.equalsIgnoreCase("Cincia da Computao - Curriculo Antigo")) {
                    stringCellValue = "Cincia da Computao";
                } else if (stringCellValue.equalsIgnoreCase("BSI")
                        || stringCellValue.equalsIgnoreCase("Sistemas de Informao")) {
                    stringCellValue = "Sistemas de Informao";
                }

                CursoDAO cDAO = new CursoDAO();
                try {
                    curso = cDAO.getCursoByNomeCurso(stringCellValue);
                } catch (Exception ex) {
                    String novoErro = "Linha: " + i + " . Curso no encontrado.";
                    erros = erros + "\n" + novoErro;
                }
                cDAO.closeSession();
                AlunoDAO aDAO = new AlunoDAO();
                if (null != curso.getIdcurso()) {
                    aluno.setCursoidcurso(curso);

                    try {
                        Aluno alunoByMatricula = aDAO.getAlunoByMatricula(aluno.getMatricula());
                        if (alunoByMatricula == null) {
                            //insere porque no existe na base
                            boolean inserido = aDAO.insert(aluno);
                        }
                    } catch (Exception ex) {
                        String novoErro = "Linha: " + i + " . Erro ao encontrar aluno.";
                        erros = erros + "\n" + novoErro;

                    }
                }
                trab.setAlunomatricula(aluno);
                aDAO.closeSession();
                break;
            case 3:
                /*titulo trabalho*/
                trab.setTitulo(stringCellValue);
                break;
            case 4:
                /*data matricula do trabalho*/
                trab.setDatamatricula(CalendarFormat.getDataISO(stringCellValue));
                /*horas por dia*/
                trab.setQtdhorasdia(CalendarFormat.parseTime("06:00:00"));
                break;
            case 5:
                if (!stringCellEstagioTipo.equalsIgnoreCase("estgio no obrigatrio")
                        && !stringCellEstagioTipo.equalsIgnoreCase("ESTAGIO_NAO_OBRIGATORIO")
                        && !stringCellEstagioTipo.equalsIgnoreCase("ESTAGIO NAO OBRIGATORIO")
                        && !stringCellDataDefesa.equalsIgnoreCase("\\0")
                        && !stringCellDataDefesa.equalsIgnoreCase("")) {
                    /*data minima do trabalho*/
                    defesa.setStatusdefesa(true);
                    defesa.setMatriculadatadefesa(trab.getDatamatricula());
                    defesa.setMindatadefesa(CalendarFormat.getDataISO(stringCellValue));
                }
                break;
            case 6:
                if (!stringCellEstagioTipo.equalsIgnoreCase("estgio no obrigatrio")
                        && !stringCellEstagioTipo.equalsIgnoreCase("ESTAGIO_NAO_OBRIGATORIO")
                        && !stringCellEstagioTipo.equalsIgnoreCase("ESTAGIO NAO OBRIGATORIO")
                        && !stringCellDataDefesa.equalsIgnoreCase("\\0")
                        && !stringCellDataDefesa.equalsIgnoreCase("")) {
                    /*data maxima do trabalho*/
                    defesa.setMaxdatadefesa(CalendarFormat.getDataISO(stringCellValue));
                } else {
                    /*insere a data maxima como data de finalizao
                     para estagios no obrigatorios*/
                    trab.setDatafinalizacao(CalendarFormat.getDataISO(stringCellValue));
                }
                break;
            case 7:
                /*assunto/area do trabalho*/
                assunto.setNome(stringCellValue);
                AssuntoDAO asDAO = new AssuntoDAO();
                Assunto assuntoByNome = null;
                try {
                    assuntoByNome = asDAO.getAssuntoByNome(stringCellValue);
                } catch (Exception ex) {
                    String novoErro = "Linha: " + i + " . Erro ao encontrar assunto.";
                    erros = erros + "\n" + novoErro;
                }
                if (assuntoByNome == null) {
                    try {
                        //insere porque no existe na base
                        boolean inserido = asDAO.insert(assunto);
                    } catch (Exception ex) {
                        String novoErro = "Linha: " + i + " . Erro salvar assunto.";
                        erros = erros + "\n" + novoErro;
                    }
                } {
                try {
                    assunto = asDAO.getAssuntoByNome(stringCellValue);
                } catch (Exception ex) {
                    String novoErro = "Linha: " + i + " . Erro ao encontrar assunto.";
                    erros = erros + "\n" + novoErro;
                }
            }
                trab.setAssuntoidassunto(assunto);
                asDAO.closeSession();
                break;
            case 8:
                if (!stringCellValue.equalsIgnoreCase("")) {
                    orientador.setSiape(Integer.parseInt(stringCellValue));
                }
                break;
            case 9:
                PerfilDAO peDAO = new PerfilDAO();
                ProfessorDAO pDAO = new ProfessorDAO();
                Professor professorBySiape = null;

                orientador.setNome(stringCellValue);

                /*1 tentativa de achar o professor*/
                if (orientador.getSiape() != null) {
                    try {
                        professorBySiape = pDAO.getProfessorBySiape(orientador.getSiape());
                    } catch (Exception ex) {
                        String novoErro = "Linha: " + i + " . Erro ao encontrar professor.";
                        erros = erros + "\n" + novoErro;
                    }
                }
                /*2 tentativa de achar o professor*/
                if (null == professorBySiape) {
                    try {
                        professorBySiape = pDAO.getProfessorByNome(orientador.getNome());
                    } catch (Exception ex) {
                        String novoErro = "Linha: " + i + " . Erro ao encontrar professor.";
                        erros = erros + "\n" + novoErro;
                    }
                }

                if (null == professorBySiape && !orientador.getNome().equalsIgnoreCase("")
                        && !orientador.getNome().equalsIgnoreCase("\\0")) {
                    try {
                        perfil = peDAO.getPerfilByNome("Professor");
                    } catch (Exception ex) {
                        String novoErro = "Linha: " + i + " . Erro ao encontrar perfil.";
                        erros = erros + "\n" + novoErro;
                    }

                    orientador.setPerfilidperfil(perfil);

                    /*dar o maior siape ao novo professor*/
                    int siape = 0;
                    try {
                        siape = pDAO.getMaiorSiape();
                        siape++;
                    } catch (Exception ex) {
                        String novoErro = "Linha: " + i + " . Erro ao retornar siape.";
                        erros = erros + "\n" + novoErro;
                    }
                    orientador.setSiape(siape);
                    orientador.setAtivo(true);
                    try {
                        boolean inserido = pDAO.insert(orientador);
                    } catch (Exception ex) {
                        String novoErro = "Linha: " + i + " . Erro ao inserir orientador.";
                        erros = erros + "\n" + novoErro;
                    }
                } else {
                    orientador = professorBySiape;
                }

                trab.setProfessorsiape(orientador);
                peDAO.closeSession();
                pDAO.closeSession();
                break;
            case 10:
                EmpresaDAO eDAO = new EmpresaDAO();
                empresa.setNome(stringCellValue);
                Empresa empresaByNome = null;
                try {
                    empresaByNome = eDAO.getEmpresaByNome(empresa.getNome());
                } catch (Exception ex) {
                    Logger.getLogger(importarController.class.getName()).log(Level.SEVERE, null, ex);
                }

                if (empresaByNome == null) {
                    try {
                        eDAO.insert(empresa);
                    } catch (Exception ex) {
                        String novoErro = "Linha: " + i + " . Erro ao inserir empresa.";
                        erros = erros + "\n" + novoErro;
                    }
                    try {
                        empresa = eDAO.getEmpresaByNome(empresa.getNome());
                    } catch (Exception ex) {
                        String novoErro = "Linha: " + i + " . Erro ao encontrar empresa.";
                        erros = erros + "\n" + novoErro;
                    }
                } else {
                    empresa = empresaByNome;
                }

                trab.setEmpresaidempresa(empresa);
                eDAO.closeSession();
                break;
            case 11:
                if (!stringCellEstagioTipo.equalsIgnoreCase("estgio no obrigatrio")
                        && !stringCellEstagioTipo.equalsIgnoreCase("ESTAGIO_NAO_OBRIGATORIO")
                        && !stringCellEstagioTipo.equalsIgnoreCase("ESTAGIO NAO OBRIGATORIO")
                        && !stringCellDataDefesa.equalsIgnoreCase("\\0")
                        && !stringCellDataDefesa.equalsIgnoreCase("")) {
                    banca.setLocalbanca(stringCellValue);
                }
                break;
            case 12:
                if (!stringCellEstagioTipo.equalsIgnoreCase("estgio no obrigatrio")
                        && !stringCellEstagioTipo.equalsIgnoreCase("ESTAGIO_NAO_OBRIGATORIO")
                        && !stringCellEstagioTipo.equalsIgnoreCase("ESTAGIO NAO OBRIGATORIO")
                        && !stringCellDataDefesa.equalsIgnoreCase("\\0")
                        && !stringCellDataDefesa.equalsIgnoreCase("")) {
                    banca.setDatabanca(CalendarFormat.getDataISO(stringCellValue));
                    banca.setDatafinalizacao(CalendarFormat.getDataISO(stringCellValue));
                    trab.setDatafinalizacao(CalendarFormat.getDataISO(stringCellValue));
                }
                break;
            case 13:
                if (!stringCellEstagioTipo.equalsIgnoreCase("estgio no obrigatrio")
                        && !stringCellEstagioTipo.equalsIgnoreCase("ESTAGIO_NAO_OBRIGATORIO")
                        && !stringCellEstagioTipo.equalsIgnoreCase("ESTAGIO NAO OBRIGATORIO")
                        && !stringCellDataDefesa.equalsIgnoreCase("\\0")
                        && !stringCellDataDefesa.equalsIgnoreCase("")) {
                    banca.setHorario(CalendarFormat.parseTime(stringCellValue));
                }
                break;
            case 14:
                /*status da banca */
                if (!stringCellEstagioTipo.equalsIgnoreCase("estgio no obrigatrio")
                        && !stringCellEstagioTipo.equalsIgnoreCase("ESTAGIO_NAO_OBRIGATORIO")
                        && !stringCellEstagioTipo.equalsIgnoreCase("ESTAGIO NAO OBRIGATORIO")
                        && !stringCellDataDefesa.equalsIgnoreCase("\\0")
                        && !stringCellDataDefesa.equalsIgnoreCase("")) {

                    BancaStatusDAO bsDAO = new BancaStatusDAO();
                    if (stringCellValue.equalsIgnoreCase("Finalizado")
                            || stringCellValue.equalsIgnoreCase("Reprovado")) {
                        try {
                            bancaStatus = bsDAO.getStatusByTipo("Finalizada");
                        } catch (Exception ex) {
                            String novoErro = "Linha: " + i + " . Erro ao encontrar o status da banca";
                            erros = erros + "\n" + novoErro;
                        }
                    } else if (stringCellValue.equalsIgnoreCase("A") || stringCellValue.equalsIgnoreCase("")) {
                        try {
                            bancaStatus = bsDAO.getStatusByTipo("Em andamento");
                        } catch (Exception ex) {
                            String novoErro = "Linha: " + i + " . Erro ao encontrar o status da banca";
                            erros = erros + "\n" + novoErro;
                        }
                    }
                    banca.setStatusidstatus(bancaStatus);
                    bsDAO.closeSession();
                }
                break;
            case 15:
                if (!stringCellEstagioTipo.equalsIgnoreCase("estgio no obrigatrio")
                        && !stringCellEstagioTipo.equalsIgnoreCase("ESTAGIO_NAO_OBRIGATORIO")
                        && !stringCellEstagioTipo.equalsIgnoreCase("ESTAGIO NAO OBRIGATORIO")
                        && !stringCellDataDefesa.equalsIgnoreCase("\\0")
                        && !stringCellDataDefesa.equalsIgnoreCase("")) {
                    /*primeiro convidado */
                    PerfilDAO pe1DAO = new PerfilDAO();
                    ProfessorDAO p1DAO = new ProfessorDAO();

                    Professor professorBySiape1 = null;
                    Professor pConvidado1 = new Professor();
                    String nomeConv1 = stringCellValue;

                    /*procura o professor*/
                    try {
                        professorBySiape1 = p1DAO.getProfessorByNome(nomeConv1);
                    } catch (Exception ex) {
                        String novoErro = "Linha: " + i + " . Erro ao encontrar professor convidado 1.";
                        erros = erros + "\n" + novoErro;
                    }

                    /*se no encontrar insere */
                    if (null == professorBySiape1 && !nomeConv1.equalsIgnoreCase("")
                            && !nomeConv1.equalsIgnoreCase("\\0")) {
                        try {
                            perfil = pe1DAO.getPerfilByNome("Professor");
                        } catch (Exception ex) {
                            String novoErro = "Linha: " + i + " . Erro ao encontrar perfil.";
                            erros = erros + "\n" + novoErro;
                        }

                        pConvidado1.setPerfilidperfil(perfil);

                        /*dar o maior siape ao novo professor*/
                        int siape = 0;
                        try {
                            siape = p1DAO.getMaiorSiape();
                            siape++;
                        } catch (Exception ex) {
                            String novoErro = "Linha: " + i + " . Erro ao retornar siape.";
                            erros = erros + "\n" + novoErro;
                        }
                        pConvidado1.setSiape(siape);
                        pConvidado1.setNome(nomeConv1);
                        pConvidado1.setAtivo(true);
                        try {
                            boolean inserido = p1DAO.insert(pConvidado1);
                        } catch (Exception ex) {
                            String novoErro = "Linha: " + i + " . Erro ao inserir orientador.";
                            erros = erros + "\n" + novoErro;
                        }
                    } else {
                        pConvidado1 = professorBySiape1;
                    }

                    conv1.setProfessorsiape(pConvidado1);
                    conv1.setConfirmado(true);
                    conv1.setEnviadoemail(true);
                    conv1.setDataconfirmacao(defesa.getMatriculadatadefesa());
                    conv1.setDataconvite(defesa.getMatriculadatadefesa());

                    pe1DAO.closeSession();
                    p1DAO.closeSession();
                }
                break;
            case 16:
                if (!stringCellEstagioTipo.equalsIgnoreCase("estgio no obrigatrio")
                        && !stringCellEstagioTipo.equalsIgnoreCase("ESTAGIO_NAO_OBRIGATORIO")
                        && !stringCellEstagioTipo.equalsIgnoreCase("ESTAGIO NAO OBRIGATORIO")
                        && !stringCellDataDefesa.equalsIgnoreCase("\\0")
                        && !stringCellDataDefesa.equalsIgnoreCase("")) {
                    /*segundo convidado */
                    PerfilDAO pe2DAO = new PerfilDAO();
                    ProfessorDAO p2DAO = new ProfessorDAO();
                    Professor professorBySiape2 = null;
                    Professor pConvidado2 = new Professor();
                    String nomeConv2 = stringCellValue;

                    /*procura o professor*/
                    try {
                        professorBySiape2 = p2DAO.getProfessorByNome(nomeConv2);
                    } catch (Exception ex) {
                        String novoErro = "Linha: " + i + " . Erro ao encontrar professor convidado 1.";
                        erros = erros + "\n" + novoErro;
                    }

                    /*se no encontrar insere */
                    if (null == professorBySiape2 && !nomeConv2.equalsIgnoreCase("")
                            && !nomeConv2.equalsIgnoreCase("\\0")) {
                        try {
                            perfil = pe2DAO.getPerfilByNome("Professor");
                        } catch (Exception ex) {
                            String novoErro = "Linha: " + i + " . Erro ao encontrar perfil.";
                            erros = erros + "\n" + novoErro;
                        }

                        pConvidado2.setPerfilidperfil(perfil);

                        /*dar o maior siape ao novo professor*/
                        int siape = 0;
                        try {
                            siape = p2DAO.getMaiorSiape();
                            siape++;
                        } catch (Exception ex) {
                            String novoErro = "Linha: " + i + " . Erro ao retornar siape.";
                            erros = erros + "\n" + novoErro;
                        }
                        pConvidado2.setSiape(siape);
                        pConvidado2.setNome(nomeConv2);
                        pConvidado2.setAtivo(true);
                        try {
                            boolean inserido = p2DAO.insert(pConvidado2);
                        } catch (Exception ex) {
                            String novoErro = "Linha: " + i + " . Erro ao inserir orientador.";
                            erros = erros + "\n" + novoErro;
                        }
                    } else {
                        pConvidado2 = professorBySiape2;
                    }

                    conv2.setProfessorsiape(pConvidado2);
                    conv2.setConfirmado(true);
                    conv2.setEnviadoemail(true);
                    conv2.setDataconfirmacao(defesa.getMatriculadatadefesa());
                    conv2.setDataconvite(defesa.getMatriculadatadefesa());

                    pe2DAO.closeSession();
                    p2DAO.closeSession();

                }
                break;
            case 17:
                /*status do trabalho*/
                TrabalhoStatusDAO tsDAO = new TrabalhoStatusDAO();

                if (stringCellValue.equalsIgnoreCase("") || stringCellValue.equalsIgnoreCase("A")) {
                    try {
                        trabStatus = tsDAO.getStatusByTipo("Em andamento");
                    } catch (Exception ex) {
                        String novoErro = "Linha: " + i + " . Erro ao encontrar o status do trabalho.";
                        erros = erros + "\n" + novoErro;
                    }
                } else if (stringCellValue.equalsIgnoreCase("REPROVADO")) {
                    try {
                        trabStatus = tsDAO.getStatusByTipo("Reprovado");
                    } catch (Exception ex) {
                        String novoErro = "Linha: " + i + " . Erro ao encontrar o status do trabalho.";
                        erros = erros + "\n" + novoErro;
                    }
                } else if (stringCellValue.equalsIgnoreCase("Finalizado")) {
                    try {
                        trabStatus = tsDAO.getStatusByTipo("Finalizado");
                    } catch (Exception ex) {
                        String novoErro = "Linha: " + i + " . Erro ao encontrar o status do trabalho.";
                        erros = erros + "\n" + novoErro;
                    }
                }
                trab.setStatusidstatus(trabStatus);
                tsDAO.closeSession();
                break;
            case 18:
                /*tipo de estgio*/
                EstagioDAO estDAO = new EstagioDAO();
                ModalidadeDAO mDAO = new ModalidadeDAO();

                if (stringCellValue.equalsIgnoreCase("ESTAGIO_OBRIGATORIO")
                        || stringCellValue.equalsIgnoreCase("ESTAGIO OBRIGATORIO")) {
                    try {
                        modalidade = mDAO.getModalidadeByNome("Estgio");
                    } catch (Exception ex) {
                        String novoErro = "Linha: " + i + " . Erro ao encontrar a modalidade.";
                        erros = erros + "\n" + novoErro;
                    }

                    try {
                        estagio = estDAO.getEstagioByTipo("Estgio Obrigatrio");
                    } catch (Exception ex) {
                        String novoErro = "Linha: " + i + " . Erro ao encontrar a modalidade.";
                        erros = erros + "\n" + novoErro;
                    }
                } else if (stringCellValue.equalsIgnoreCase("estgio no obrigatrio")
                        || stringCellValue.equalsIgnoreCase("ESTAGIO_NAO_OBRIGATORIO")
                        || stringCellValue.equalsIgnoreCase("ESTAGIO NAO OBRIGATORIO")) {
                    try {
                        modalidade = mDAO.getModalidadeByNome("Estgio");
                    } catch (Exception ex) {
                        String novoErro = "Linha: " + i + " . Erro ao encontrar o estgio.";
                        erros = erros + "\n" + novoErro;
                    }

                    try {
                        estagio = estDAO.getEstagioByTipo("Estgio No Obrigatrio");
                    } catch (Exception ex) {
                        String novoErro = "Linha: " + i + " . Erro ao encontrar o estgio.";
                        erros = erros + "\n" + novoErro;
                    }
                }
                trab.setEstagioidestagio(estagio);
                trab.setModalidadeidmodalidade(modalidade);

                estDAO.closeSession();
                mDAO.closeSession();
                break;
            }
        }

        //---------------------------------------
        TrabalhoDAO tDAO = new TrabalhoDAO();
        List<Trabalho> trabTemp = new ArrayList<>();
        //procurar trabalho se encontrar no importa novamente
        try {
            //trabTemp = tDAO.getTrabalhoByAlunoAndTitulo(aluno.getMatricula(), trab.getTitulo(), trab.getStatusidstatus().getTipostatus(), trab.getProfessorsiape().getSiape());
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            String novoErro = "Linha: " + i + " . Erro ao encontrar trabalho.";
            erros = erros + "\n" + novoErro;
        }
        if (trabTemp.isEmpty()) {

            //salvar trabalho
            try {
                boolean inserido = tDAO.insert(trab);
                qtdImportado++;
            } catch (Exception ex) {
                String novoErro = "Linha: " + i + " . Erro ao inserir trabalho.";
                erros = erros + "\n" + novoErro;
            }
            tDAO.closeSession();
            //---------------------------------------

            if (!stringCellEstagioTipo.equalsIgnoreCase("estgio no obrigatrio")
                    && !stringCellEstagioTipo.equalsIgnoreCase("ESTAGIO_NAO_OBRIGATORIO")
                    && !stringCellEstagioTipo.equalsIgnoreCase("ESTAGIO NAO OBRIGATORIO")
                    && !stringCellDataDefesa.equalsIgnoreCase("\\0")
                    && !stringCellDataDefesa.equalsIgnoreCase("")) {
                //---------------------------------------
                //salvar banca
                BancaDAO bDAO = new BancaDAO();
                banca.setTrabalhoidtrabalho(trab);
                try {
                    boolean inserido = bDAO.insert(banca);
                } catch (Exception ex) {
                    String novoErro = "Linha: " + i + " . Erro ao inserir banca.";
                    erros = erros + "\n" + novoErro;
                }
                bDAO.closeSession();
                //---------------------------------------
                //salvar o convidado 1
                ConvidadoDAO c1DAO = new ConvidadoDAO();
                conv1.setBancaidbanca(banca);
                try {
                    boolean inserido = c1DAO.insert(conv1);
                } catch (Exception ex) {
                    String novoErro = "Linha: " + i + " . Erro ao inserir convidado 1.";
                    erros = erros + "\n" + novoErro;
                }
                c1DAO.closeSession();
                //---------------------------------------
                //salvar o convidado 2
                ConvidadoDAO c2DAO = new ConvidadoDAO();
                conv2.setBancaidbanca(banca);
                try {
                    boolean inserido = c2DAO.insert(conv2);
                } catch (Exception ex) {
                    String novoErro = "Linha: " + i + " . Erro ao inserir convidado 2.";
                    erros = erros + "\n" + novoErro;
                }
                c2DAO.closeSession();
                //---------------------------------------
                //salvar defesa
                DefesaDAO dDAO = new DefesaDAO();
                defesa.setTrabalhoidtrabalho(trab);
                try {
                    boolean inserido = dDAO.insert(defesa);
                } catch (Exception ex) {
                    String novoErro = "Linha: " + i + " . Erro ao inserir defesa.";
                    erros = erros + "\n" + novoErro;
                }
                dDAO.closeSession();
                //---------------------------------------
            }
        }
    }

    /* this.setImportacaoErrosTrabalho(erros);
     if (qtdImportado > 0) {
     FacesContext.getCurrentInstance().addMessage("growlImportacaoTrabalho",
     new FacesMessage(FacesMessage.SEVERITY_INFO, "Importao Realizada!\r\n Importado " + String.valueOf(qtdImportado) + " de " + totalDeLinhas, ""));
     } else {
     FacesContext.getCurrentInstance().addMessage("growlImportacaoTrabalho",
     new FacesMessage(FacesMessage.SEVERITY_WARN, "Importao no realizada!", ""));
     }
     */
}

From source file:Teste.importarExcel2.java

private static void lerAluno(List dataHolder) {
    //camea na linha 1 porque a linha 0  cabealho
    for (int i = 1; i < dataHolder.size(); i++) {
        List linha = (List) dataHolder.get(i);
        Aluno aluno = new Aluno();
        for (int col = 0; col < linha.size(); col++) {
            HSSFCell myCell = (HSSFCell) linha.get(col);
            String stringCellValue = myCell.toString();

            switch (col) {
            case 0:
                aluno.setMatricula(stringCellValue);
                break;
            case 1:
                aluno.setNome(stringCellValue);
                break;
            case 2:
                aluno.setEmail(stringCellValue);
                break;
            case 3:
                aluno.setTelefone(stringCellValue);
                break;
            case 4:
                CursoDAO cDAO = new CursoDAO();
                Curso curso = new Curso();
                try {
                    curso = cDAO.getCursoByNomeCurso(stringCellValue);
                } catch (Exception ex) {
                    Logger.getLogger(importarExcel2.class.getName()).log(Level.SEVERE, null, ex);
                }/*from  w ww.j  a  v a 2 s . com*/
                if (null != curso.getIdcurso()) {
                    aluno.setCursoidcurso(curso);
                    cDAO.closeSession();

                    AlunoDAO aDAO = new AlunoDAO();
                    try {
                        boolean inserido = aDAO.insert(aluno);

                        if (inserido == false) {
                            System.out.print("Aluno " + aluno.getNome() + " j existe.");
                        }

                    } catch (Exception ex) {
                        Logger.getLogger(importarExcel2.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    aDAO.closeSession();
                }
                break;
            }
        }
        System.out.println();
    }
}

From source file:Teste.importarExcel2.java

private static void read(List dataHolder) {
    for (int i = 0; i < dataHolder.size(); i++) {
        List cellStoreVector = (List) dataHolder.get(i);
        for (int j = 0; j < cellStoreVector.size(); j++) {
            HSSFCell myCell = (HSSFCell) cellStoreVector.get(j);
            String stringCellValue = myCell.toString();
            System.out.print(stringCellValue + "\t");
        }//from  w  ww . ja va 2 s  . c o m
        System.out.println();
    }
}

From source file:transactionController.PurchaseController.java

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    // TODO add your handling code here:
    try {/* w w  w .ja v  a  2s  .  com*/
        JFileChooser fileChooser = new JFileChooser();
        int returnVal = fileChooser.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fileChooser.getSelectedFile();
            List sheetData = new ArrayList();
            FileInputStream fis = null;
            fis = new FileInputStream(file);
            HSSFWorkbook workbook = new HSSFWorkbook(fis);
            HSSFSheet sheet = workbook.getSheetAt(0);
            Iterator rows = sheet.rowIterator();
            while (rows.hasNext()) {
                HSSFRow row = (HSSFRow) rows.next();
                Iterator cells = row.cellIterator();

                List data = new ArrayList();
                while (cells.hasNext()) {
                    HSSFCell cell = (HSSFCell) cells.next();
                    data.add(cell.toString().toUpperCase().replaceAll("!", "").trim());
                }

                sheetData.add(data);
            }
            dtm.setRowCount(0);
            for (int i = 0; i < sheetData.size(); i++) {
                List list = (List) sheetData.get(i);
                String sr_cd = "";
                if (itemCode.get(list.get(1).toString()) == null) {
                    sr_cd = lb.getRetrofit().create(SupportAPI.class)
                            .validateData("SERIESMST", "SR_CD", "SR_NAME", list.get(1).toString()).execute()
                            .body().get("data").getAsString();
                    if (sr_cd.equalsIgnoreCase("")) {
                        dtm.setRowCount(0);
                        return;
                    } else {
                        itemCode.put(list.get(1).toString(), sr_cd);
                    }
                } else {
                    sr_cd = itemCode.get(list.get(1).toString());
                }
                Vector row = new Vector();
                row.add(list.get(0).toString());
                row.add(list.get(1).toString());
                row.add(list.get(2).toString());
                row.add(list.get(3).toString());
                row.add("1");
                row.add(list.get(5).toString());
                row.add("");
                row.add("0");
                row.add(list.get(8).toString());
                row.add(list.get(9).toString());
                row.add(list.get(10).toString());
                row.add(list.get(11).toString());
                row.add("0");
                row.add("0");
                row.add(list.get(5).toString());
                row.add(list.get(15).toString());
                row.add(sr_cd);
                dtm.addRow(row);
            }
            setTotal();
        } else {
            System.out.println("File access cancelled by user.");
        }

    } catch (Exception ex) {
        System.out.println(ex.getMessage());
    }
}

From source file:ucar.unidata.data.DataUtil.java

License:Open Source License

/**
 * Convert excel to csv//from   w w  w . j  a  v a  2s .com
 *
 * @param filename excel file
 * @param skipToFirstNumeric if true, skip to first numeric
 * @param sdf If non null then use this to format any date cells
 *
 * @return csv
 *
 * @throws Exception On badness
 */
public static String xlsToCsv(String filename, boolean skipToFirstNumeric, SimpleDateFormat sdf)
        throws Exception {
    StringBuffer sb = new StringBuffer();
    InputStream myxls = IOUtil.getInputStream(filename, DataUtil.class);
    HSSFWorkbook wb = new HSSFWorkbook(myxls);
    HSSFSheet sheet = wb.getSheetAt(0); // first sheet
    boolean seenNumber = false;
    for (int rowIdx = sheet.getFirstRowNum(); rowIdx <= sheet.getLastRowNum(); rowIdx++) {
        HSSFRow row = sheet.getRow(rowIdx);
        if (row == null) {
            sb.append("\n");
            continue;
        }
        boolean rowOk = true;
        for (short colIdx = row.getFirstCellNum(); colIdx < row.getPhysicalNumberOfCells(); colIdx++) {
            HSSFCell cell = row.getCell(colIdx);
            if (cell == null) {
                continue;
            }
            if (skipToFirstNumeric && !seenNumber) {
                if (cell.getCellType() != HSSFCell.CELL_TYPE_NUMERIC) {
                    rowOk = false;
                    break;
                }
                seenNumber = true;
            }

            String cellValue = null;

            if ((sdf != null) && (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC)) {
                if (HSSFDateUtil.isCellDateFormatted(cell)) {
                    Date date = cell.getDateCellValue();
                    cellValue = sdf.format(date);
                }
            }

            if (cellValue == null) {
                cellValue = cell.toString();
            }

            if (colIdx > 0) {
                sb.append(",");
            }
            sb.append(cellValue);
            /*                if(false && comment!=null) {
            String author = comment.getAuthor();
            String str = comment.getString().getString();
            str = StringUtil.replace(str, author+":","");
            str = StringUtil.replace(str, "\n","");
            sb.append("("+str+")");
            }*/
        }
        if (rowOk) {
            sb.append("\n");
        }
    }
    return sb.toString();
}

From source file:uk.ac.ebi.mnb.parser.ExcelXLSHelper.java

License:Open Source License

@Override
public String[][] getTableHead(Integer sheetIndex, Integer rowCount) {

    HSSFSheet sheet = workbook.getSheetAt(sheetIndex);
    Integer colCount = 0;//from  w w w.  j  a v  a 2 s  .c o  m

    List[] data = new List[rowCount];

    for (Integer i = 0; i < rowCount; i++) {
        HSSFRow row = sheet.getRow(i);
        data[i] = new ArrayList();
        for (Integer j = 0; j < row.getLastCellNum(); j++) {
            HSSFCell cell = row.getCell(j);
            if (cell != null && !cell.toString().trim().isEmpty()) {
                data[i].add(cell.toString());
                colCount = j > colCount ? j : colCount;
            } else {
                data[i].add("");
            }
        }
    }

    String[][] fixedData = new String[rowCount][colCount];
    for (int i = 0; i < rowCount; i++) {
        // make sure we have the correct length
        while (data[i].size() < colCount) {
            data[i].add("");
        }
        fixedData[i] = (String[]) data[i].subList(0, colCount).toArray(new String[0]);
    }

    return fixedData;
}

From source file:usac.centrocalculo.data.LecturaURyS.java

private void printToConsole(List cellDataList) {
    for (int i = 0; i < cellDataList.size(); i++) {
        List cellTempList = (List) cellDataList.get(i);
        for (int j = 0; j < cellTempList.size(); j++) {
            HSSFCell hssfCell = (HSSFCell) cellTempList.get(j);
            String stringCellValue = hssfCell.toString();
            System.out.print(hssfCell.toString());
        }/*from  w w w . j a v a  2 s  .  c  om*/
        System.out.println();
    }
}

From source file:usac.centrocalculo.data.LecturaURyS.java

private void printTo(List cellDataList) {
    for (int i = 0; i < cellDataList.size(); i++) {
        List cellTempList = (List) cellDataList.get(i);
        for (int j = 0; j < cellTempList.size(); j++) {
            HSSFCell hssfCell = (HSSFCell) cellTempList.get(j);
            //String CellValue = (hssfCell.toString()).replaceAll(" ", "");
            String CellValue = hssfCell.toString();
            switch (hssfCell.getCellType()) {

            case Cell.CELL_TYPE_BOOLEAN:

                doc = doc + CellValue + "<>";
                break;

            case Cell.CELL_TYPE_NUMERIC:
                doc = doc + CellValue + "<>";
                break;

            case Cell.CELL_TYPE_STRING:
                doc = doc + CellValue + "<>";
                break;

            case Cell.CELL_TYPE_BLANK:

                break;

            default:

            }/*from w w  w  . j  a  v a  2s.c om*/

        }

    }
    /*this.getDato("F<>","<>Salario",doc);
     this.getDato("<>Salario Q.<>",">       Edad:",doc);
     this.getDato("<>Dependencia:<>",">Ttulo del puesto vacante:<>",doc);
     this.getDato("FORMATO PERFIL DEL PUESTO<>",">       Datos Generales<>",doc);
     //Motivo ....
     this.getDato("NOMBRE:    <>",">       __________________________________<>Registro de Personal:<>",doc);
     this.getDato("Registro de Personal:<>",">       __________________________________<>Vigencia del Puesto",doc);
     //Sexo...
     this.getDato("<>       Edad:        de","        a _________<>Horario de trabajo",doc);
     this.getDato("<>       Edad:        de_________        a","<>Horario de trabajo:<>",doc);
     this.getDato("<>       Lugar de residencia:","<>Motivo de la Vacante",doc);
     this.getDato("Experiencia:  Incluir aspectos tcnicos, equipo que debe<>       manejar, programas de computacin, etc.)<>","NOMBRE:    <>",doc);
     this.getDato("<>       Breve descripcin de funciones del puesto:<>",getDato("NOMBRE:    <>",">       __________________________________<>Registro de Personal:<>",doc),doc);
     this. getDato("<>OBSERVACIONES:<>*","Inga. Roco Carolina Medina ",doc);
     this.getDato("____<>Guatemala,<>","<>       __________________________________<>       __________________________________<>       __________________________________<>          (f) <>Delegado por autoridad nominadora<>O",doc);
     this.getDato("<>Persona Propuesta (en el caso de ascenso)<>       Breve descripcin de funciones del puesto:<>",">       __________________________________<>Guatemala,",doc);
     this.getDato("","",doc);
     this.getDato("","",doc);
     this.getDato("","",doc);
     this.getDato("","",doc);
     this.getDato("","",doc);
     this.getDato("","",doc);
     this.getDato("","",doc);
     this.getDato("","",doc);*/

    doc = null;
}