java excel 另存为_当我从Java下载Excel文件不显示“另存为”窗口,但文件被正确保存...

在Java应用中使用Apache POI库创建并下载Excel文件时,用户通常会期望看到浏览器的'另存为'对话框。然而,问题在于某个特定的下载场景下,该对话框并未出现,尽管文件被正常保存。其他页面使用相同代码下载Excel文件时,'另存为'窗口正常显示。问题可能与HTTP响应设置或文件处理方式有关。代码示例中展示了如何创建Excel工作簿并将其写入文件,然后通过HttpServletResponse进行输出。解决方法可能是检查响应头设置,确保'Content-Disposition'设置为'attachment',以触发浏览器的下载行为。

最奇怪的是我有其他按钮用于下载我的应用程序的其他页面中的excel文件,并且与POI同样的代码y也一样,“另存为”通常出现。

为什么发生?

谢谢。

我附上一张图片。

53a1df0ef8a33b155803ad58e623624a.png

函数导出的代码是: public void exportarTabla(){

XSSFWorkbook workbook = new XSSFWorkbook();

XSSFSheet sheet = workbook.createSheet("Hoja 1");

XSSFRow row;

XSSFCell cell;

for (int i = 0; i < getLstEtiquetasCol().size(); i++) {

row = sheet.createRow(i+3);

for (int j = 0; j < getLstEtiquetasCol().get(i).size(); j++) {

cell = row.createCell(j+3+getLstEtiquetasFil().get(0).size());

cell.setCellValue(getLstEtiquetasCol().get(i).get(j).getValor().getEtiqueta());

}

}

for (int i = 0; i < getLstEtiquetasFil().size(); i++) {

row = sheet.createRow(i+3+getLstEtiquetasCol().size());

for (int j = 0; j < getLstEtiquetasFil().get(i).size(); j++) {

cell = row.createCell(j+3);

cell.setCellValue(getLstEtiquetasFil().get(i).get(j).getValor().getEtiqueta());

}

}

for (int i = 0; i < getTableContact().size(); i++) {

row = sheet.getRow( i+3+getLstEtiquetasCol().size());

for (int j = 0; j < getTableContact().get(i).size(); j++) {

cell = row.createCell(j+3+getLstEtiquetasFil().get(0).size());

cell.setCellValue(Double.parseDouble(getTableContact().get(i).get(j).getEtiqueta()));

}

}

try {

this.archivo_salida = "D:/jboss-6.1.0.Final/bin/output2.xlsx";

FileOutputStream fileOut = new FileOutputStream(new File(archivo_salida));

workbook.write(fileOut);

if(fileOut != null){

try{

File ficheroXLS = new File(archivo_salida);

FacesContext ctx = FacesContext.getCurrentInstance();

FileInputStream fis = new FileInputStream(ficheroXLS);

byte[] bytes = new byte[1000];

int read = 0;

if (!ctx.getResponseComplete()) {

String fileName = ficheroXLS.getName();

String contentType = "application/vnd.ms-excel";

//String contentType = "application/pdf";

HttpServletResponse response =(HttpServletResponse) ctx.getExternalContext().getResponse();

response.setContentType(contentType);

response.setHeader("Content-Disposition","attachment;filename=\"" + fileName + "\"");

ServletOutputStream out = response.getOutputStream();

while ((read = fis.read(bytes)) != -1) {

out.write(bytes, 0, read);

}

out.flush();

out.close();

System.out.println("\nDescargado\n");

ctx.responseComplete();

}

}catch(IOException e){

e.printStackTrace();

} finally {

try {

if (fileOut != null) {

fileOut.close();

}

} catch (IOException ex) {

ex.printStackTrace();

}

}

}

} catch (IOException e) {

e.printStackTrace();

}

}

在web.xml <?xml version="1.0" ?>

http://java.sun.com/xml/ns/javaee/web-app_2_5的.xsd” 版本= “2.5”>

org.richfaces.SKIN

blueSky

org.richfaces.CONTROL_SKINNING

disable

org.richfaces.CONTROL_SKINNING_CLASSES

disable

org.richfaces.LoadStyleStrategy

ALL

org.richfaces.LoadScriptStrategy

ALL

org.jboss.seam.servlet.SeamListener

Seam Filter

org.jboss.seam.servlet.SeamFilter

maxRequestSize

1000000

Seam Filter

/*

Seam Resource Servlet

org.jboss.seam.servlet.SeamResourceServlet

Seam Resource Servlet

/seam/resource/*

facelets.DEVELOPMENT

@[email protected]

javax.faces.DEFAULT_SUFFIX

.xhtml

Faces Servlet

javax.faces.webapp.FacesServlet

1

Faces Servlet

*.seam

Restrict raw XHTML Documents

XHTML

*.xhtml

demoencuesta06/pu

demoencuesta06

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值