bom dia galera, eu estou tentando gerar um arquivo xlsx e aplicar um template alguem pode dar uma força?!
fiz assim…
ContatoDAO exDAO = new ContatoDAO();
String arquivo = getServlet().getServletContext().getRealPath("relatorios/template.xlsx");
FileInputStream file = new FileInputStream(new File(arquivo));
POIFSFileSystem fsSystem = new POIFSFileSystem(file);
HSSFWorkbook workbook = new HSSFWorkbook(fsSystem);
HSSFSheet Sheet = workbook.createSheet("template.xlsx");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(new File("teste.xlsx"));
String dataIni = request.getParameter("dataInicio");
String dataFim = request.getParameter("dataFim");
LinkedList<Contato> lista = exDAO.ContatoDias(dataIni, dataFim);
JRBeanCollectionDataSource jrds = new JRBeanCollectionDataSource(exDAO.ContatoDias(dataIni, dataFim));
int i = 1;
for (Contato ca : lista) {
HSSFRow row = Sheet.createRow(i);
row.createCell((short) 0).setCellValue(ca.getNomeContato());
row.createCell((short) 1).setCellValue(ca.getTipo());
row.createCell((short) 2).setCellValue(ca.getEndereco);
row.createCell((short) 3).setCellValue(ca.getCep);
i++;
}
FileOutputStream output = new FileOutputStream("c:/teste.xlsx");
workbook.write(output);
workbook.write(fos);
} catch (Exception e) {
e.printStackTrace();
System.out.println("erro ao exportar o arquivo");
}finally{
try {
fos.flush();
fos.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
fazendo uma pesquisa vi no site da apache poi que esse codigo acima ja é antigo e apliquei o novo porém agora ele nem passa dessa linha
Workbook[] workbooks = new Workbook[]{new HSSFWorkbook(), new XSSFWorkbook()};
o novo codigo ficou assim
ContatoDAO exDAO = new ContatoDAO();
FileInputStream file = new FileInputStream(new File("c:/workspace/estudos/relatorios/template.xlsx"));
Workbook[] workbooks = new Workbook[]{new HSSFWorkbook(file), new XSSFWorkbook(file)};
/// Workbook workbook = new HSSFWorkbook(file);
for(int i = 0; i < workbooks.length; i++){
Workbook workbook = workbooks[i];
Sheet Sheet = workbook.createSheet("planilha.xls");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(new File("planilha.xls"));
String dataIni = request.getParameter("dataInicio");
String dataFim = request.getParameter("dataFim");
LinkedList<ContatoApoio> listaApoio = exDAO.ContatoDias(dataIni, dataFim);
int linhas = 1;
for (ContatoApoio ca : listaApoio) {
Row row = Sheet.createRow(linhas);
row.createCell((short) 0).setCellValue(ca.getNomeContato());
row.createCell((short) 1).setCellValue(ca.getTipo());
row.createCell((short) 2).setCellValue(ca.getEndereco);
row.createCell((short) 3).setCellValue(ca.getCep);
linhas++;
}
String fileName = "c:/teste.xls";
if(workbook instanceof XSSFWorkbook){
fileName = fileName + "x";
}
FileOutputStream output = new FileOutputStream(fileName);
workbook.write(output);
workbook.write(fos);
} catch (Exception e) {
e.printStackTrace();
System.out.println("erro ao exportar o arquivo");
}finally{
try {
System.out.println("arquivo criado com sucesso");
fos.flush();
fos.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}