在我的项目中,需要对excel报表进行处理,现在有一个问题,描述如下:
使用这个系统已经在项目的目录下生成了excel文件
使用网页访问该excel文件,想要的效果是弹出另存为对话框
但是现在有的机器能弹出,有的则是把excel文件在网页上打开
高分求解。

解决方案 »

  1.   

    文件头错误,少了一个attachment的参数,有这个就是下载,没有就是打开!
    response.addHeader("Content-Disposition", new String(("attachment; filename=" + filename).getBytes("GBK"), "ISO-8859-1")); // 针对中文文件名  
      

  2.   

    // 客户端下载时显示的名称
    String filenamedisplay = url + ".xls";
    // 生成文件的文件名(绝对路径)
    String filenamedownload = "C:\\" + url + ".xls";//文件所在地址
    System.out.println("real URL:" + filenamedownload);
    filenamedisplay = new String(filenamedisplay
    .getBytes("GBK"), "ISO-8859-1");//下载时显示的地址
    response.reset();
    response.setContentType("application/x-msdownload");
    response.addHeader("Content-Disposition",
    "attachment;filename=" + filenamedisplay); OutputStream output = response.getOutputStream(); InputStream fis = new FileInputStream(filenamedownload);
    byte[] b = new byte[1024];
    int i = 0;
    while ((i = fis.read(b)) > 0) {
    output.write(b, 0, i);
    }
    output.flush();
    fis.close();
    output.close();