当我单击我的应用程序的“导出文件”选项时,我想向用户显示“另存为”窗口,该文件是一个 2007 年的 Excel,而不是我使用 Apache POI 创建的。该文件已正确保存在文件夹“D:\jboss-6.1.0.Final\bin”中,而是显示“另存为”窗口,它重定向到带有奇怪符号和字符的页面。
最奇怪的是我在我的应用程序的其他页面中有其他按钮用于下载 excel 文件,并且使用与 POI 相同的代码 y,“另存为”正常显示。
为什么会发生???
谢谢你。
附上图片。
函数导出的代码是:
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" version="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>