如何使用Java + Struts2在浏览器中打开Excel工作表? [英] How to open an Excel sheet in browser using Java+Struts2?

查看:149
本文介绍了如何使用Java + Struts2在浏览器中打开Excel工作表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在浏览器中而不是在MS Excel中打开xls表.我已经尝试过Desktop.getDesktop().browse(fileName.toURI());,但是无法正常工作.这是execute方法的完整代码:

I'm trying to open an xls sheet in the browser, not in MS Excel. I've tried with Desktop.getDesktop().browse(fileName.toURI()); but is not working. This is the complete code of the execute method:

public String execute() throws Exception
{    
    String rutaArchivo = System.getProperty("catalina.base")+"/ejemploExcelJava.xls";     

    File archivoXLS = new File(rutaArchivo);

    if(archivoXLS.exists()) {
        archivoXLS.delete();
    }
    archivoXLS.createNewFile();

    Workbook libro = new HSSFWorkbook();

    FileOutputStream archivo = new FileOutputStream(archivoXLS);

    Sheet hoja = libro.createSheet("Mi hoja de trabajo 1");

    Date fechaActual = new Date();
    for (int f = 0; f < 10; f++) {            
        Row fila = hoja.createRow(f);            
        for (int c = 0; c < 5; c++) {
            Cell celda = fila.createCell(c);               
            if (f == 0) {
                celda.setCellValue("Encabezado #" + c);
            } else {
                celda.setCellValue(fechaActual.getHours() +  ":"  + fechaActual.getMinutes());
            }
        }
    }        
    libro.write(archivo);        
    archivo.close();        
    Desktop.getDesktop().browse(archivoXLS.toURI());                                                                                         
 }

无论如何,这可以从Microsoft Office Excel应用程序打开excel,但只能通过从Netbeans运行该项目.如果我尝试从不带Netbeans的Tomcat中打开它,它将无法正常工作.

Anyway, this works opening excel from Microsoft Office Excel application, but only by running the project from Netbeans. If I try to open it from Tomcat without Netbeans, it doesn't work.

推荐答案

  1. 您可以使用流结果;
  2. 要输出Excel文件(通过读取现有文件并创建新文件),您需要根据流出的excel文件类型设置正确的 Content Type (内容类型)(通常是XLS或XLSX),如该答案中所述.
  3. 您可以通过更改默认的内容处置来指示用户代理需要在浏览器中打开文件(而不是请求下载/使用桌面应用程序打开). strong>从attachmentinline;
  4. 内联打开未知二进制文件取决于客户端:如果您流式传输JPEG,浏览器将轻松打开它;如果您流式传输PDF(或Excel文档,Word文档等),浏览器将搜索适当的插件(例如Adobe Acrobat),如果找不到,将尝试使用桌面应用程序将其打开(例如Adobe Reader).这就是为什么在Internet Explorer(具有内置插件)中打开Excel却不能在Firefox中打开Excel的原因.
  1. You can output any binary result with the Stream Result;
  2. To output an Excel file (both by reading an existing one and by creating a new one) you need to set the right Content Type, according to the type of excel file you are streaming out (commonly XLS or XLSX), like described in this answer.
  3. You can instruct the User Agent that it needs to open a file inside the browser (instead of asking for download / open with a desktop application) by changing the default Content Disposition from attachment to inline;
  4. Opening an unknown binary file inline is up to the client: if you stream out a JPEG, the browser will open it easily; if you stream out a PDF (or an Excel document, a Word document, and so on), the browser will search for an appropriate plugin (for example Adobe Acrobat), and if not found, will try to open it with the desktop application (for example Adobe Reader) anyway. That's why opening an Excel in Internet Explorer (that has the plugin inbuilt) works, while opening it in Firefox doesn't.

这篇关于如何使用Java + Struts2在浏览器中打开Excel工作表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆