Pessoal…
Estou com um probleminha… tenho que converter minha classe que está com a api do jexcel para a api do POI para usar o XSSF (formatos do excel xlsx)
Alguem poderia me ajudar com isso? pois tentei de diversas formas e ão sei como fazer…
COM JEXCEL:
[code]
package br.com.simcard;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import jxl.Cell;
import jxl.DateCell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import br.com.simcard.dao.PlanilhaTelecomDAO;
public class VerificarRegistroTelecom {
private static final int LINHA_INICIAL = 1;
private Workbook workbook;
private Sheet sheet;
public Date transformaData(String data) {
Date retorno = null;
SimpleDateFormat formatador = new SimpleDateFormat("dd/MM/yyyy");
try {
retorno = formatador.parse(data);
} catch (ParseException ex) {
throw new RuntimeException(ex);
}
return retorno;
}
public VerificarRegistroTelecom(File vr, String login) throws Exception,
IOException, BiffException, ClassNotFoundException, SQLException {
this.workbook = Workbook.getWorkbook(vr);
this.sheet = workbook.getSheet(0);
Verificador(vr, login);
}
public void Verificador(File vr, String login) {
int nuLinhas = sheet.getRows();
PlanilhaTelecomDAO dao = new PlanilhaTelecomDAO();
NomeArquivo nomeArq = new NomeArquivo();
nomeArq.setNome(vr.toString());
Calendar horaAtual = Calendar.getInstance();
horaAtual.set(Calendar.MILLISECOND, 0);
int QtdeErro = 0;
int QtdeAlt = 0;
int CodProc = 0;
// INSERE PROCESSAMENTO
dao.insereProcessamento(17, (nuLinhas) - 1, login, nomeArq.getNome(),
horaAtual);
CodProc = dao.CodigoProcessamento();
for (int nuLinhaAtual = LINHA_INICIAL; nuLinhaAtual < nuLinhas; nuLinhaAtual++) {
Telecom telecom = carregarLinha(nuLinhaAtual);
// VERIFICA SE PLANILHA É VAZIA
if (telecom.getSimcard().equals("")
|| telecom.getSimcard().equals(null)
|| telecom.getLinha().equals("")
|| telecom.getLinha().equals(null)) {
System.out.println("Campo Vazio");
dao.InsereLogErro(0, telecom.getSimcard(), nuLinhaAtual,
CodProc);
QtdeErro++;
dao.QtdeErro(QtdeErro);
continue;
}
// SE O SIMCARD NÃO EXISTE CHAMA INSERE
if (!dao.existeSimCard(telecom.getSimcard())) {
System.out.println("Chama Insert");
dao.simCardInsereTelecom(telecom.getSimcard(),
telecom.getLinha(), telecom.getApnUtilizada(),
telecom.getConta(), telecom.getOperadora(),
telecom.getPlano(), telecom.getStatusSIMCard(),
telecom.getDtAtivacao(), telecom.getFonte(),
telecom.getStatusLinha(), telecom.getImsi(),
telecom.getConsumoDados(), nomeArq.getNome(), CodProc,
login, horaAtual);
QtdeAlt++;
// SENÃO VERIFICA SE PRECISA UPDATE
} else {
if (!dao.existeSimCardLinha(telecom.getSimcard(),
telecom.getLinha())) {
// GRAVA HISTORICO
System.out.println("Grava na Histórico");
dao.HistoricoTELECOM(telecom.getSimcard());
// FAZ UPDATE
System.out.println("Faz UPDATE");
dao.simCardUpdateTelecom(telecom.getSimcard(),
telecom.getLinha(), telecom.getApnUtilizada(),
telecom.getConta(), telecom.getOperadora(),
telecom.getPlano(), telecom.getStatusSIMCard(),
telecom.getDtAtivacao(), telecom.getFonte(),
telecom.getStatusLinha(), telecom.getImsi(),
telecom.getConsumoDados(), nomeArq.getNome(),
CodProc, login, horaAtual);
QtdeAlt++;
} else if (!dao.simCardVerificaUpdate(telecom.getSimcard(),
telecom.getLinha(), telecom.getApnUtilizada(),
telecom.getConta(), telecom.getOperadora(),
telecom.getPlano(), telecom.getStatusSIMCard(),
telecom.getDtAtivacao(), telecom.getFonte(),
telecom.getStatusLinha(), telecom.getImsi(),
telecom.getConsumoDados())) {
if (!dao.planoMudou(telecom.getLinha(), telecom.getPlano())) {
// GRAVA HISTORICO
System.out.println("Grava na Histórico");
dao.HistoricoTELECOM(telecom.getSimcard());
// FAZ UPDATE
System.out.println("Faz UPDATE");
dao.simCardUpdateTelecom(telecom.getSimcard(),
telecom.getLinha(), telecom.getApnUtilizada(),
telecom.getConta(), telecom.getOperadora(),
telecom.getPlano(), telecom.getStatusSIMCard(),
telecom.getDtAtivacao(), telecom.getFonte(),
telecom.getStatusLinha(), telecom.getImsi(),
telecom.getConsumoDados(), nomeArq.getNome(),
CodProc, login, horaAtual);
dao.InsereLogErro(29, telecom.getSimcard(), nuLinhaAtual,
CodProc);
QtdeErro++;
QtdeAlt++;
} else {
// GRAVA HISTORICO
System.out.println("Grava na Histórico");
dao.HistoricoTELECOM(telecom.getSimcard());
// FAZ UPDATE
System.out.println("Faz UPDATE");
dao.simCardUpdateTelecom(telecom.getSimcard(),
telecom.getLinha(), telecom.getApnUtilizada(),
telecom.getConta(), telecom.getOperadora(),
telecom.getPlano(), telecom.getStatusSIMCard(),
telecom.getDtAtivacao(), telecom.getFonte(),
telecom.getStatusLinha(), telecom.getImsi(),
telecom.getConsumoDados(), nomeArq.getNome(),
CodProc, login, horaAtual);
QtdeAlt++;
}
} else {
System.out.println("Chama Próximo");
dao.QtdeErro(QtdeErro);
dao.QtdeAlterada(QtdeAlt);
continue;
}
}
dao.QtdeErro(QtdeErro);
dao.QtdeAlterada(QtdeAlt);
}
}
private Telecom carregarLinha(int nuLinhaAtual) {
ValoresPlanilhaEnum planilhaEnum = null;
Telecom linhaTelecom = new Telecom();
Cell[] celulas = sheet.getRow(nuLinhaAtual);
for (int nuCelulaAtual = 0; nuCelulaAtual < celulas.length; nuCelulaAtual++) {
Cell celula = celulas[nuCelulaAtual];
TelecomCelulasEnum celulaTelecom = TelecomCelulasEnum
.obterCelulaTelecom(nuCelulaAtual);
switch (celulaTelecom) {
case SIMCARD:
linhaTelecom.setSimcard(celula.getContents());
break;
case LINHA:
linhaTelecom.setLinha(celula.getContents());
break;
case APN_UTILIZADA:
planilhaEnum = ValoresPlanilhaEnum.obtervALORPorDesc(celula
.getContents());
linhaTelecom.setApnUtilizada(planilhaEnum.getCodigo());
break;
case CONTA:
linhaTelecom.setConta(celula.getContents());
break;
case OPERADORA:
planilhaEnum = ValoresPlanilhaEnum.obtervALORPorDesc(celula
.getContents());
linhaTelecom.setOperadora(planilhaEnum.getCodigo());
break;
case PLANO:
linhaTelecom.setPlano(celula.getContents());
break;
case STATUS_SIM_CARD:
planilhaEnum = ValoresPlanilhaEnum.obtervALORPorDesc(celula
.getContents());
linhaTelecom.setStatusSIMCard(planilhaEnum.getCodigo());
break;
case DATA_ATIVACAO:
TimeZone gmtZone = TimeZone.getTimeZone("GMT");
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
format.setTimeZone(gmtZone);
if (celula.getContents() != null && celula.getContents() != "") {
DateCell dataCell = (DateCell) celula;
String dataFormatada = format.format(dataCell.getDate());
linhaTelecom.setDtAtivacao(transformaData(dataFormatada));
} else {
linhaTelecom.setDtAtivacao(null);
}
break;
case FONTE:
planilhaEnum = ValoresPlanilhaEnum.obtervALORPorDesc(celula
.getContents());
linhaTelecom.setFonte(planilhaEnum.getCodigo());
break;
case STATUS_LINHA:
planilhaEnum = ValoresPlanilhaEnum.obtervALORPorDesc(celula
.getContents());
linhaTelecom.setStatusLinha(planilhaEnum.getCodigo());
break;
case IMSI:
linhaTelecom.setImsi(celula.getContents());
break;
case CONSUMO_DADOS:
linhaTelecom.setConsumoDados(celula.getContents());
break;
}
}
return linhaTelecom;
}
}[/code]
COM POI:
[code]
package br.com.simcard;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import jxl.Cell;
import jxl.DateCell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import br.com.simcard.dao.PlanilhaTelecomDAO;
public class LeituraArquivoPOI {
private static final int LINHA_INICIAL = 1;
private XSSFWorkbook workbook;
private XSSFSheet sheet;
public Date transformaData(String data) {
Date retorno = null;
SimpleDateFormat formatador = new SimpleDateFormat("dd/MM/yyyy");
try {
retorno = formatador.parse(data);
} catch (ParseException ex) {
throw new RuntimeException(ex);
}
return retorno;
}
public LeituraArquivoPOI(File vr, String login) throws Exception,
IOException, BiffException, ClassNotFoundException, SQLException {
this.workbook = new XSSFWorkbook(new FileInputStream(vr));
this.sheet = workbook.getSheetAt(0);
Verificador(vr, login);
}
public void Verificador(File vr, String login) {
int nuLinhas = sheet.getFirstRowNum();
PlanilhaTelecomDAO dao = new PlanilhaTelecomDAO();
NomeArquivo nomeArq = new NomeArquivo();
nomeArq.setNome(vr.toString());
Calendar horaAtual = Calendar.getInstance();
horaAtual.set(Calendar.MILLISECOND, 0);
int QtdeErro = 0;
int QtdeAlt = 0;
int CodProc = 0;
// INSERE PROCESSAMENTO
dao.insereProcessamento(17, (nuLinhas) - 1, login, nomeArq.getNome(),
horaAtual);
CodProc = dao.CodigoProcessamento();
for (int nuLinhaAtual = LINHA_INICIAL; nuLinhaAtual < nuLinhas; nuLinhaAtual++) {
Telecom telecom = carregarLinha(nuLinhaAtual);
// VERIFICA SE PLANILHA É VAZIA
if (telecom.getSimcard().equals("")
|| telecom.getSimcard().equals(null)
|| telecom.getLinha().equals("")
|| telecom.getLinha().equals(null)) {
System.out.println("Campo Vazio");
dao.InsereLogErro(0, telecom.getSimcard(), nuLinhaAtual,
CodProc);
QtdeErro++;
dao.QtdeErro(QtdeErro);
continue;
}
// SE O SIMCARD NÃO EXISTE CHAMA INSERE
if (!dao.existeSimCard(telecom.getSimcard())) {
System.out.println("Chama Insert");
dao.simCardInsereTelecom(telecom.getSimcard(),
telecom.getLinha(), telecom.getApnUtilizada(),
telecom.getConta(), telecom.getOperadora(),
telecom.getPlano(), telecom.getStatusSIMCard(),
telecom.getDtAtivacao(), telecom.getFonte(),
telecom.getStatusLinha(), telecom.getImsi(),
telecom.getConsumoDados(), nomeArq.getNome(), CodProc,
login, horaAtual);
QtdeAlt++;
// SENÃO VERIFICA SE PRECISA UPDATE
} else {
if (!dao.existeSimCardLinha(telecom.getSimcard(),
telecom.getLinha())) {
// GRAVA HISTORICO
System.out.println("Grava na Histórico");
dao.HistoricoTELECOM(telecom.getSimcard());
// FAZ UPDATE
System.out.println("Faz UPDATE");
dao.simCardUpdateTelecom(telecom.getSimcard(),
telecom.getLinha(), telecom.getApnUtilizada(),
telecom.getConta(), telecom.getOperadora(),
telecom.getPlano(), telecom.getStatusSIMCard(),
telecom.getDtAtivacao(), telecom.getFonte(),
telecom.getStatusLinha(), telecom.getImsi(),
telecom.getConsumoDados(), nomeArq.getNome(),
CodProc, login, horaAtual);
QtdeAlt++;
} else if (!dao.simCardVerificaUpdate(telecom.getSimcard(),
telecom.getLinha(), telecom.getApnUtilizada(),
telecom.getConta(), telecom.getOperadora(),
telecom.getPlano(), telecom.getStatusSIMCard(),
telecom.getDtAtivacao(), telecom.getFonte(),
telecom.getStatusLinha(), telecom.getImsi(),
telecom.getConsumoDados())) {
if (!dao.planoMudou(telecom.getLinha(), telecom.getPlano())) {
// GRAVA HISTORICO
System.out.println("Grava na Histórico");
dao.HistoricoTELECOM(telecom.getSimcard());
// FAZ UPDATE
System.out.println("Faz UPDATE");
dao.simCardUpdateTelecom(telecom.getSimcard(),
telecom.getLinha(), telecom.getApnUtilizada(),
telecom.getConta(), telecom.getOperadora(),
telecom.getPlano(), telecom.getStatusSIMCard(),
telecom.getDtAtivacao(), telecom.getFonte(),
telecom.getStatusLinha(), telecom.getImsi(),
telecom.getConsumoDados(), nomeArq.getNome(),
CodProc, login, horaAtual);
dao.InsereLogErro(29, telecom.getSimcard(), nuLinhaAtual,
CodProc);
QtdeErro++;
QtdeAlt++;
} else {
// GRAVA HISTORICO
System.out.println("Grava na Histórico");
dao.HistoricoTELECOM(telecom.getSimcard());
// FAZ UPDATE
System.out.println("Faz UPDATE");
dao.simCardUpdateTelecom(telecom.getSimcard(),
telecom.getLinha(), telecom.getApnUtilizada(),
telecom.getConta(), telecom.getOperadora(),
telecom.getPlano(), telecom.getStatusSIMCard(),
telecom.getDtAtivacao(), telecom.getFonte(),
telecom.getStatusLinha(), telecom.getImsi(),
telecom.getConsumoDados(), nomeArq.getNome(),
CodProc, login, horaAtual);
QtdeAlt++;
}
} else {
System.out.println("Chama Próximo");
dao.QtdeErro(QtdeErro);
dao.QtdeAlterada(QtdeAlt);
continue;
}
}
dao.QtdeErro(QtdeErro);
dao.QtdeAlterada(QtdeAlt);
}
}
//
// NESSE MÉTODO AQUI ESTOU CONFUSO EM PEGAR A LINHA E COLOCAR EM VETOR PARA SER LIDO…
//
private Telecom carregarLinha(int nuLinhaAtual) {
ValoresPlanilhaEnum planilhaEnum = null;
Telecom linhaTelecom = new Telecom();
XSSFRow celulas = sheet.getRow(nuLinhaAtual);
for (int nuCelulaAtual = 0; nuCelulaAtual < celulas.getLastCellNum(); nuCelulaAtual++) {
// AQUI TA ERRADO…
[]celula = celulas[nuCelulaAtual];
TelecomCelulasEnum celulaTelecom = TelecomCelulasEnum
.obterCelulaTelecom(nuCelulaAtual);
switch (celulaTelecom) {
case SIMCARD:
linhaTelecom.setSimcard(celula.getContents());
break;
case LINHA:
linhaTelecom.setLinha(celula.getContents());
break;
case APN_UTILIZADA:
planilhaEnum = ValoresPlanilhaEnum.obtervALORPorDesc(celula
.getContents());
linhaTelecom.setApnUtilizada(planilhaEnum.getCodigo());
break;
case CONTA:
linhaTelecom.setConta(celula.getContents());
break;
case OPERADORA:
planilhaEnum = ValoresPlanilhaEnum.obtervALORPorDesc(celula
.getContents());
linhaTelecom.setOperadora(planilhaEnum.getCodigo());
break;
case PLANO:
linhaTelecom.setPlano(celula.getContents());
break;
case STATUS_SIM_CARD:
planilhaEnum = ValoresPlanilhaEnum.obtervALORPorDesc(celula
.getContents());
linhaTelecom.setStatusSIMCard(planilhaEnum.getCodigo());
break;
case DATA_ATIVACAO:
TimeZone gmtZone = TimeZone.getTimeZone("GMT");
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
format.setTimeZone(gmtZone);
if (celula.getContents() != null && celula.getContents() != "") {
DateCell dataCell = (DateCell) celula;
String dataFormatada = format.format(dataCell.getDate());
linhaTelecom.setDtAtivacao(transformaData(dataFormatada));
} else {
linhaTelecom.setDtAtivacao(null);
}
break;
case FONTE:
planilhaEnum = ValoresPlanilhaEnum.obtervALORPorDesc(celula
.getContents());
linhaTelecom.setFonte(planilhaEnum.getCodigo());
break;
case STATUS_LINHA:
planilhaEnum = ValoresPlanilhaEnum.obtervALORPorDesc(celula
.getContents());
linhaTelecom.setStatusLinha(planilhaEnum.getCodigo());
break;
case IMSI:
linhaTelecom.setImsi(celula.getContents());
break;
case CONSUMO_DADOS:
linhaTelecom.setConsumoDados(celula.getContents());
break;
}
}
return linhaTelecom;
}
}[/code]
Se repararem não é muita coisa… eu comecei dando uma mexida (Mas não sei se está certo), a lógica no meio não muda em nada…
o que precisa mesmo é declarar as linhas o workbook, pegar os conteúdos da célula…