Como fazer download de planilha excel após gerada pelo poi apache
Olá, não estou conseguindo fazer o download do arquivo excel gerado pela api poi apache.
estou com o seguinte código:
public void createXLS(RowSetIterator iter, AttributeDef[] attr, String[] attr2, String filename, long maxrowcount,
FacesContext context, ServletOutputStream out) {
ExternalContext ectx = context.getExternalContext();
HttpServletResponse response = (HttpServletResponse) ectx.getResponse();
try {
response.setHeader("Content-disposition", "attachment; filename=" + filename);
response.setContentType("application/vnd.ms-excel");
response.setHeader("Pragma", "private");
response.setHeader("Cache-control", "must-revalidate");
HSSFWorkbook workbook = createWorkbook(iter, attr, attr2, maxrowcount);
out=response.getOutputStream();
workbook.write(out);
out.flush();
context.responseComplete();
} catch (IOException e) {
e.printStackTrace();
} finally {
ADFUtil.closePopup("export");
}
}
private HSSFWorkbook createWorkbook(RowSetIterator iter, AttributeDef[] attr, String[] attr2,
long maxrowcount) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet((String) EL.get("#{attrs.fileName}"));
int idx = 0; // rows index
HSSFRow row = sheet.createRow(idx);
HSSFFont boldFont = wb.createFont();
boldFont.setColor(HSSFColor.AUTOMATIC.index);
boldFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
boldFont.setFontHeightInPoints((short) 9);
HSSFCellStyle boldStyle = wb.createCellStyle();
boldStyle.setFont(boldFont);
boldStyle.setFillBackgroundColor(HSSFColor.AQUA.index);
HSSFCellStyle style = wb.createCellStyle();
style.setDataFormat(HSSFDataFormat.getBuiltinFormat("($#,##0_);[Red]($#,##0)"));
style.setFillBackgroundColor(HSSFColor.AQUA.index);
style.setFillPattern(HSSFCellStyle.BIG_SPOTS);
HSSFCellStyle cellStyle = wb.createCellStyle();
HSSFCellStyle headerStyle = wb.createCellStyle();
headerStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); //define a intensidade da cor de fundo da celula
headerStyle.setFillForegroundColor(HSSFColor.BLACK.index); // Define a cor de fundo da celula
HSSFFont fonte = wb.createFont(); //cria nova fonte
fonte.setColor(HSSFColor.WHITE.index); //define a cor da fonte
headerStyle.setFont(fonte); //adiciona a nova fonte aos estilos
/******************************** Creating headers ************************************/
for (int i = 0; i < attr.length; i++) {
AttributeHints hints = attr[i].getUIHelper();
String label = hints.getLabel(Util.getLocaleContext());
row.createCell(i).setCellValue(label);
row.getCell(i).setCellStyle(headerStyle);
}
idx++;
/******************************* Creating first row **************************************/
Row fr = iter.getCurrentRow();
long rowcount = 1;
row = sheet.createRow(idx);
/******************************** Creating cells *************************************/
for (int j = 0; j < attr2.length; j++) {
HSSFCell cell = row.createCell(j);
Object value = fr.getAttribute(attr2[j]);
setConvertedCellValue(cell, value, cellStyle);
}
idx++;
/******************************** Creating rows ****************************************/
while (iter.hasNext() && ++rowcount <= maxrowcount) {
Row r = iter.next();
row = sheet.createRow(idx);
/******************************** Creating cels ***********************************/
for (int j = 0; j < attr2.length; j++) {
HSSFCell cell = row.createCell(j);
Object value = r.getAttribute(attr2[j]);
setConvertedCellValue(cell, value, cellStyle);
sheet.autoSizeColumn(j); // define tamanho automatico para a largura da celula
}
idx++;
}
return wb;
}
estou com o seguinte código:
public void createXLS(RowSetIterator iter, AttributeDef[] attr, String[] attr2, String filename, long maxrowcount,
FacesContext context, ServletOutputStream out) {
ExternalContext ectx = context.getExternalContext();
HttpServletResponse response = (HttpServletResponse) ectx.getResponse();
try {
response.setHeader("Content-disposition", "attachment; filename=" + filename);
response.setContentType("application/vnd.ms-excel");
response.setHeader("Pragma", "private");
response.setHeader("Cache-control", "must-revalidate");
HSSFWorkbook workbook = createWorkbook(iter, attr, attr2, maxrowcount);
out=response.getOutputStream();
workbook.write(out);
out.flush();
context.responseComplete();
} catch (IOException e) {
e.printStackTrace();
} finally {
ADFUtil.closePopup("export");
}
}
private HSSFWorkbook createWorkbook(RowSetIterator iter, AttributeDef[] attr, String[] attr2,
long maxrowcount) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet((String) EL.get("#{attrs.fileName}"));
int idx = 0; // rows index
HSSFRow row = sheet.createRow(idx);
HSSFFont boldFont = wb.createFont();
boldFont.setColor(HSSFColor.AUTOMATIC.index);
boldFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
boldFont.setFontHeightInPoints((short) 9);
HSSFCellStyle boldStyle = wb.createCellStyle();
boldStyle.setFont(boldFont);
boldStyle.setFillBackgroundColor(HSSFColor.AQUA.index);
HSSFCellStyle style = wb.createCellStyle();
style.setDataFormat(HSSFDataFormat.getBuiltinFormat("($#,##0_);[Red]($#,##0)"));
style.setFillBackgroundColor(HSSFColor.AQUA.index);
style.setFillPattern(HSSFCellStyle.BIG_SPOTS);
HSSFCellStyle cellStyle = wb.createCellStyle();
HSSFCellStyle headerStyle = wb.createCellStyle();
headerStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); //define a intensidade da cor de fundo da celula
headerStyle.setFillForegroundColor(HSSFColor.BLACK.index); // Define a cor de fundo da celula
HSSFFont fonte = wb.createFont(); //cria nova fonte
fonte.setColor(HSSFColor.WHITE.index); //define a cor da fonte
headerStyle.setFont(fonte); //adiciona a nova fonte aos estilos
/******************************** Creating headers ************************************/
for (int i = 0; i < attr.length; i++) {
AttributeHints hints = attr[i].getUIHelper();
String label = hints.getLabel(Util.getLocaleContext());
row.createCell(i).setCellValue(label);
row.getCell(i).setCellStyle(headerStyle);
}
idx++;
/******************************* Creating first row **************************************/
Row fr = iter.getCurrentRow();
long rowcount = 1;
row = sheet.createRow(idx);
/******************************** Creating cells *************************************/
for (int j = 0; j < attr2.length; j++) {
HSSFCell cell = row.createCell(j);
Object value = fr.getAttribute(attr2[j]);
setConvertedCellValue(cell, value, cellStyle);
}
idx++;
/******************************** Creating rows ****************************************/
while (iter.hasNext() && ++rowcount <= maxrowcount) {
Row r = iter.next();
row = sheet.createRow(idx);
/******************************** Creating cels ***********************************/
for (int j = 0; j < attr2.length; j++) {
HSSFCell cell = row.createCell(j);
Object value = r.getAttribute(attr2[j]);
setConvertedCellValue(cell, value, cellStyle);
sheet.autoSizeColumn(j); // define tamanho automatico para a largura da celula
}
idx++;
}
return wb;
}
Jorge Wickert
Curtidas 0