在jsp中 
response.setContentType("application/x-xls ");
response.setHeader("Content-isposition","inline; attachment;filename=" + filePath);File file = new File(filePath);
if (file.exists()) {

DataOutputStream temps = new DataOutputStream(response.getOutputStream());
DataInputStream in = new DataInputStream(new FileInputStream(filePath));

byte[] b = new byte[2048];
while((in.read(b))!=-1){
temps.write(b);
temps.flush();
}
in.close();
temps.close();
}
} catch(Exception e) {
e.printStackTrace();

以上代码 office 2003 excel 能直接在jsp页面打开
但是 office 2007 excel 就开不开 大家有知道的吗?
帮帮忙 谢谢啦!!!!

解决方案 »

  1.   

    contentType="application/vnd.ms-excel"
      

  2.   

    在本地安装一个office2007兼容包试试
      

  3.   

    office2007 我都安装了,那个兼容包不是包含在 office2007 中了吗?
      

  4.   

    你是将文档保存在本地以后再打开了?还是在线打开???Office 07 和 03有些不同,你需要安装兼容包。你不妨试试用office07新建一个文件,然后在通过jsp打开看看行不行?如果不行那就是contentType 的问题了。
      

  5.   


    我是在服务器一个文件夹中存放着 xlsx 文件
    在IE中 直接打开文件, 那也就是在线打开吧。
    office07新建一个文件 这个我用了 一样是开不开的contentType  的问题 能帮我想想是有什么问题吗
      

  6.   

    test.jsp<%
    try{
    if("pdf".equals(format)){
    response.setContentType("application/pdf");
    }else if ("txt".equals(format)) {
    response.setContentType("text/plain;charset=shift-jis");
    }else if ("pptx".equals(format)) {
    response.setContentType("application/vnd.openxmlformats-officedocument.presentationml.presentation");
    }else if ("xlsx".equals(format)) {
    response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    }else if ("docx".equals(format)) {
    response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
    }else if ("dotx".equals(format)) {
    response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.template");
    }else if ("doc".equals(format)) {
    response.setContentType("application/msword");
    }else if ("xls".equals(format)) {
    response.setContentType("application/vnd.ms-excel application/x-excel");
    }else if ("pps".equals(format)) {
    response.setContentType("application/vnd.ms-powerpoint");
    }else if ("ppsx".equals(format)) {
    response.setContentType("application/vnd.openxmlformats-officedocument.presentationml.slideshow");
    }else {
    response.setContentType("application/octet-stream"); 
    }
    response.addHeader("Content-Disposition","filename="+sfilename);
     FileInputStream   fis   =   new   FileInputStream(filePath);
                 bos   =   new   BufferedOutputStream(response.getOutputStream());
                 byte[]   buffer   =   new   byte[2048];
                     while   (fis.read(buffer)   !=   -1)   {
                         bos.write(buffer);  
                     }  
                     buffer = null;
            
                     fis.close();  
                     bos.close();
                     out.clear();
         out = pageContext.pushBody();

    }catch (Exception   e){
    e.printStackTrace();
    }
    %>
    以上是我的全部代码 大家帮忙看看哪个地方有问题呀!!!!
      

  7.   

    兼容包和office是分开的:http://xiazai.zol.com.cn/detail/15/145524.shtml
    安装一个试试。你是改成这样了吗:response.setContentType("application/vnd.ms-excel");
      

  8.   


    你给的网址是 office2007免费版下载 
    我的客户端都安装完了 office2007
    response.setContentType("application/vnd.ms-excel");
    这个我都适用过了 ,还是不行
    再帮忙看看呗 ,到底是哪的问题呢?2003 的 没问题 2007 的就打不开。
      

  9.   

    用java写过excel但是没有通过JSP打开过。
    刚刚“摆”了一下发现,网上一致认为2003和2007的兼容是个问题,插件也不一定能解决你程序的问题。http://13480.blog.163.com/blog/static/16285514200792525044606/
    http://blog.csdn.net/chenzhuolin1002/article/details/6213960
    http://hi.baidu.com/274084093/item/11b35f95d1a79dd81a49df6a
    http://blog.163.com/rigger21@126/blog/static/9279147200781731014452/
    http://download.csdn.net/detail/uixor_/4016423最后一个是别人上传的一个工程,不知道这些搜索能不能有用啊!
      

  10.   

    在write(buffer); zhui jia :  out.clear(); out = pageContext.pushBody();
      

  11.   


    用了你的方法还是不好用呀! 
    现象是 出来一个  打开 保存 的选择
    我选择打开 但是出现说我一个action 类什么的
    再点 yes 就在IE 中 说我的session 过期什么的
    我都没用到session 呀。。
      

  12.   

    把你原来的out.clear(); out = pageContext.pushBody();删掉了么?  就在while循环里面有这个就行了
      

  13.   

    你循环那里换一下呢?
    byte b= new byte[1024];
    int i=0;
    while((i==fis.read(b)) > 0){
    os.write(b, 0, i);
    out.clear();
    out = pageContext.pushBody();
    }fis.close();
    os.flush();
    os.close();
      

  14.   

    2007的读取<%
    try{
      String file = application.getRealPath("/") + "upload\\test.xlsx";
      XSSFWorkbook xwb = new XSSFWorkbook(new FileInputStream(file));
      //读取第一个sheet
      XSSFSheet sheet = xwb.getSheetAt(0);  //定义 row、cell
      XSSFRow row;
      XSSFCell cell;
      String output;
      int rowNum;
      int cellNum;  //循环输出表格中的内容
      output = "";
      rowNum = sheet.getLastRowNum();  for (int i = 0; i <= rowNum; i++)
      {
        row = sheet.getRow(i);
        cellNum = row.getPhysicalNumberOfCells();
        for (int j = 0; j < cellNum; j++)
        {
          cell = row.getCell(j);
          output = output + cell.getStringCellValue() + "   ";
        }
        output = output + "<br />";
      }  out.print(output);}catch(Exception e){
      out.print(e.toString());
    }%>
      

  15.   

    2003的读取<%
    try{
      String file = application.getRealPath("/") + "upload\\test.xls";
      POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file));
      HSSFWorkbook xwb = new HSSFWorkbook(fs);
      //读取第一个sheet
      HSSFSheet sheet = xwb.getSheetAt(0);  //定义 row、cell
      HSSFRow row;
      HSSFCell cell;
      String output;
      int rowNum;
      int cellNum;  //循环输出表格中的内容
      output = "";
      rowNum = sheet.getPhysicalNumberOfRows();  for (int i = 0; i < rowNum; i++)
      {
        row = sheet.getRow(i);
        cellNum = row.getPhysicalNumberOfCells();
        for (int j = 0; j < cellNum; j++)
        {
          cell = row.getCell(j);
          output = output + cell.getStringCellValue() + "   ";
        }
        output = output + "<br />";
      }  out.print(output);}catch(Exception e){
      out.print(e.toString());
    }%>
      

  16.   


    <%@page contentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"%>
    <%@page import="java.io.*"%>
    <%
    String filePath = request.getRealPath("/")+"order.xlsx";
    System.out.println(filePath);out.clearBuffer(); 
    response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    response.addHeader("Content-Disposition","inline; attachment; filename=" + filePath);File file = new File(filePath);
    if (file.exists()) {
     
        OutputStream sout = response.getOutputStream();
        InputStream in = new FileInputStream(filePath);
     
        byte[] b = new byte[2048];
        int len = 0;
        while((len = in.read(b))!=-1){
            sout.write(b,0,len);
            sout.flush();
        }
        in.close();
        sout.close();
    }%>