segue minha classe de criar excel… eu pego o arquivo vindo de um jsp passo para o sevlet o arquivo que será pego uns valores para realizar uma consulta no BD.
File uploadedFile = new File("./arquivoUpload/"
+ fileItem.getName());
fileItem.write(uploadedFile);
caminho = "./arquivoUpload/" + fileItem.getName();
Esse arquivo gravo no servidor, para poder ler e chamo a classe criar excel
package br.com.simcard;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import org.apache.commons.fileupload.FileUploadException;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class CriarExcel {
/*public static void main(String[] args) {
try{
CriarPlanilha();
System.out.println("Planilha escrita com sucesso");
} catch (NullPointerException npe){
npe.printStackTrace();
} catch (IOException ioe){
ioe.printStackTrace();
} catch (RowsExceededException ree){
ree.printStackTrace();
} catch (WriteException we){
we.printStackTrace();
}
}*/
public void CriarPlanilhaColetiva(String dispositivo, String codTecnologia, String apnConfigurada, String simCard,
String linha, String operadora, String apnProvisionada, String statusSIMCard, String fonte, String imei,
String imsi, String statusLinha, String consDados, String matricula) throws IOException, RowsExceededException, WriteException, BiffException{
//FORMATANDO FONTE
// Adicionando formato de fonte no título... se adicionar ", true" depois de bold adiciona Italico
WritableFont bold = new WritableFont(WritableFont.ARIAL,
10, WritableFont.BOLD);
bold.setColour(Colour.WHITE);
WritableCellFormat arial10font = new WritableCellFormat(bold);
arial10font.setBackground(Colour.BLUE);
arial10font.setAlignment(Alignment.CENTRE);
arial10font.setBorder(Border.ALL,BorderLineStyle.MEDIUM,Colour.BLACK);
// Adicionando formato de fonte resultados... se adicionar ", true" depois de bold adiciona Italico
WritableFont bold2 = new WritableFont(WritableFont.ARIAL, 10);
WritableCellFormat arial10fontResult = new WritableCellFormat(bold2);
arial10fontResult.setAlignment(Alignment.CENTRE);
arial10fontResult.setBorder(Border.ALL,BorderLineStyle.THIN,Colour.BLACK);
//ADICIONA DATA/HORA NO ARQUIVO GERADO
GregorianCalendar calendar = new GregorianCalendar();
SimpleDateFormat formatador = new SimpleDateFormat("dd' de 'MMMMM' de 'yyyy");
System.out.println(formatador.format(calendar.getTime()));
String filename = "./arquivoUpload/Consulta_Coletiva - "+formatador.format(calendar.getTime())+".xls";
File filename2 = new File(filename);
if (!filename2.exists()){
//Instanciando a classe q gera o novo arquivo do Excel
WritableWorkbook workbook = Workbook.createWorkbook(new File(filename));
//Criando uma nova planilha
WritableSheet sheet = workbook.createSheet("First Sheet", 0);
//col, lin
Label labelTitulo0 = new Label(0, 0, "Dispositivo", arial10font);
sheet.addCell(labelTitulo0);
Label labelTitulo1 = new Label(1, 0, "SIMCard", arial10font);
sheet.addCell(labelTitulo1);
Label labelTitulo2 = new Label(2, 0, "Linha", arial10font);
sheet.addCell(labelTitulo2);
Label labelTitulo3 = new Label(3, 0, "Tecnologia", arial10font);
sheet.addCell(labelTitulo3);
Label labelTitulo4 = new Label(4, 0, "APN Configurada", arial10font);
sheet.addCell(labelTitulo4);
Label labelTitulo5 = new Label(5, 0, "Operadora", arial10font);
sheet.addCell(labelTitulo5);
Label labelTitulo6 = new Label(6, 0, "APN Provisionada", arial10font);
sheet.addCell(labelTitulo6);
Label labelTitulo7 = new Label(7, 0, "Status SIMCard", arial10font);
sheet.addCell(labelTitulo7);
Label labelTitulo8 = new Label(8, 0, "Fonte", arial10font);
sheet.addCell(labelTitulo8);
Label labelTitulo9 = new Label(9, 0, "IMEI", arial10font);
sheet.addCell(labelTitulo9);
Label labelTitulo10 = new Label(10, 0, "IMSI", arial10font);
sheet.addCell(labelTitulo10);
Label labelTitulo11 = new Label(11, 0, "Status Linha", arial10font);
sheet.addCell(labelTitulo11);
Label labelTitulo12 = new Label(12, 0, "Consumo de Dados", arial10font);
sheet.addCell(labelTitulo12);
Label labelTitulo13 = new Label(13, 0, "Matricula", arial10font);
sheet.addCell(labelTitulo13);
int i = sheet.getRows();
//col, lin
Label label = new Label(0, i, dispositivo, arial10fontResult);
sheet.addCell(label);
Label label1 = new Label(1, i, simCard, arial10fontResult);
sheet.addCell(label1);
Label label2 = new Label(2, i, linha, arial10fontResult);
sheet.addCell(label2);
Label label3 = new Label(3, i, codTecnologia, arial10fontResult);
sheet.addCell(label3);
Label label4 = new Label(4, i, apnConfigurada, arial10fontResult);
sheet.addCell(label4);
Label label5 = new Label(5, i, operadora, arial10fontResult);
sheet.addCell(label5);
Label label6 = new Label(6, i, apnProvisionada, arial10fontResult);
sheet.addCell(label6);
Label label7 = new Label(7, i, statusSIMCard, arial10fontResult);
sheet.addCell(label7);
Label label8 = new Label(8, i, fonte, arial10fontResult);
sheet.addCell(label8);
Label label9 = new Label(9, i, imei, arial10fontResult);
sheet.addCell(label9);
Label label10 = new Label(10, i, imsi, arial10fontResult);
sheet.addCell(label10);
Label label11 = new Label(11, i, statusLinha, arial10fontResult);
sheet.addCell(label11);
Label label12 = new Label(12, i, consDados, arial10fontResult);
sheet.addCell(label12);
Label label13 = new Label(13, i, matricula, arial10fontResult);
sheet.addCell(label13);
// Escrevedo o arquivo em disco
workbook.write();
// Fechando a IO
workbook.close();
} else {
Workbook rw = Workbook.getWorkbook (new File (filename2.toString()));
WritableWorkbook wwb = Workbook.createWorkbook (new File (filename2.toString()), rw);
WritableSheet sheet = wwb.getSheet (0);
int i = sheet.getRows();
//col, lin
Label label = new Label(0, i, dispositivo, arial10fontResult);
sheet.addCell(label);
Label label1 = new Label(1, i, simCard, arial10fontResult);
sheet.addCell(label1);
Label label2 = new Label(2, i, linha, arial10fontResult);
sheet.addCell(label2);
Label label3 = new Label(3, i, codTecnologia, arial10fontResult);
sheet.addCell(label3);
Label label4 = new Label(4, i, apnConfigurada, arial10fontResult);
sheet.addCell(label4);
Label label5 = new Label(5, i, operadora, arial10fontResult);
sheet.addCell(label5);
Label label6 = new Label(6, i, apnProvisionada, arial10fontResult);
sheet.addCell(label6);
Label label7 = new Label(7, i, statusSIMCard, arial10fontResult);
sheet.addCell(label7);
Label label8 = new Label(8, i, fonte, arial10fontResult);
sheet.addCell(label8);
Label label9 = new Label(9, i, imei, arial10fontResult);
sheet.addCell(label9);
Label label10 = new Label(10, i, imsi, arial10fontResult);
sheet.addCell(label10);
Label label11 = new Label(11, i, statusLinha, arial10fontResult);
sheet.addCell(label11);
Label label12 = new Label(12, i, consDados, arial10fontResult);
sheet.addCell(label12);
Label label13 = new Label(13, i, matricula, arial10fontResult);
sheet.addCell(label13);
// Escrevedo o arquivo em disco
wwb.write();
// Fechando a IO
wwb.close();
}
}
}