gpt4 book ai didi

java - 当我从 Java 下载 Excel 文件时不显示 "Save as"窗口,但文件已正确保存

转载 作者:太空宇宙 更新时间:2023-11-04 07:37:39 25 4
gpt4 key购买 nike

当我单击应用程序的“导出文件”选项时,我想向用户显示“另存为”Windows,该文件是一个 excel 2007,而不是我使用 Apache POI 创建的文件。文件正确保存在文件夹“D:\jboss-6.1.0.Final\bin”中,但不是显示“另存为”窗口,而是重定向到带有奇怪符号和字符的页面

最奇怪的是我在应用程序的其他页面中有其他按钮用于下载 Excel 文件,并且使用 POI 的相同代码也可以正常显示“另存为”。

为什么会这样???

谢谢。

我附上一张图片。 enter image description here

导出函数的代码是:

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”>

<!-- RichFaces -->

<context-param>
<param-name>org.richfaces.SKIN</param-name>
<param-value>blueSky</param-value>
</context-param>

<!-- Suppress spurious stylesheets -->

<context-param>
<param-name>org.richfaces.CONTROL_SKINNING</param-name>
<param-value>disable</param-value>
</context-param>

<context-param>
<param-name>org.richfaces.CONTROL_SKINNING_CLASSES</param-name>
<param-value>disable</param-value>
</context-param>

<!-- Change load strategy to DEFAULT to disable sending scripts/styles as
packs -->

<context-param>
<param-name>org.richfaces.LoadStyleStrategy</param-name>
<param-value>ALL</param-value>
</context-param>

<context-param>
<param-name>org.richfaces.LoadScriptStrategy</param-name>
<param-value>ALL</param-value>
</context-param>

<!-- Seam -->

<listener>
<listener-class>org.jboss.seam.servlet.SeamListener</listener-class>
</listener>

<filter>
<filter-name>Seam Filter</filter-name>
<filter-class>org.jboss.seam.servlet.SeamFilter</filter-class>

<init-param>
<param-name>maxRequestSize</param-name>
<param-value>1000000</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>Seam Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<servlet>
<servlet-name>Seam Resource Servlet</servlet-name>
<servlet-class>org.jboss.seam.servlet.SeamResourceServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Seam Resource Servlet</servlet-name>
<url-pattern>/seam/resource/*</url-pattern>
</servlet-mapping>

<!-- Facelets development mode (disable in production) -->

<context-param>
<param-name>facelets.DEVELOPMENT</param-name>
<param-value>@debug@</param-value>
</context-param>

<!-- JSF -->

<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
</context-param>

<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.seam</url-pattern>
</servlet-mapping>

<security-constraint>
<display-name>Restrict raw XHTML Documents</display-name>
<web-resource-collection>
<web-resource-name>XHTML</web-resource-name>
<url-pattern>*.xhtml</url-pattern>
</web-resource-collection>
<auth-constraint />
</security-constraint>

<persistence-unit-ref>
<persistence-unit-ref-name>demoencuesta06/pu</persistence-unit-ref-name>
<persistence-unit-name>demoencuesta06</persistence-unit-name>
</persistence-unit-ref>

最佳答案

感谢他们的回复,我发现了问题,问题出在 .xhtml 文件中带有 ajax 的按钮中。它是:

<a4j:commandButton id="btnexportar" onclick="this.disabled=true" oncomplete="this.disabled=false; alert('La Exportacion de Datos ha finalizado')" 
action="#{dndBean.exportarTabla}"
value="Exportar Datos">
</a4j:commandButton>

我改为:

<h:commandButton id="generarinforme" action="#{dndBean.exportarTabla}"                              
value="Exportar Datos">
</h:commandButton>

并且奇怪的角色不再出现。现在我必须寻找一种使用 onclick 和 oncomplete 的方法。谢谢!

关于java - 当我从 Java 下载 Excel 文件时不显示 "Save as"窗口,但文件已正确保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16592390/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com