ssh都行我就是在做导入导出,导出的时候点击按钮跳转到Action方法,然后返回一个保存路径(不会指定这个要返回的路径问题1),然后弹出一会打开保存对话框,接收返回的路径,保存到本地冒个盘,(问题2打开保存对话框不会弄。)应该是这个流程吧。卡这很久了,望赐教
各位有源码更好,有重谢。着急的很。。

解决方案 »

  1.   

    不要想复杂了。就2步,进Action 生成excel。然后下载。下载代码网上到处都有
      

  2.   

    LZ要清楚。保存路径是弹出保存对话框的时候自己选择的只能弹出保存文件名 /**
     * 下载服务器中的excel文件
     * @param filePath
     * @param filename
     * @param response
     * @throws Exception
     */
    public static void download(File filePath, String filename, HttpServletResponse response) throws Exception{
    // TODO Auto-generated method stub
    BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        OutputStream fos = null;
        InputStream fis = null;     //File uploadFile = new File(filePath);
         File uploadFile = filePath;
        fis = new FileInputStream(uploadFile);
        bis = new BufferedInputStream(fis);
        fos = response.getOutputStream();
        bos = new BufferedOutputStream(fos);
        filename = URLEncoder.encode(filename, "GBK");
        
        //弹出下载对话框的关键代码,filename为显示的保存文件名称
        response.setContentType("application/x-download");
        response.setHeader("Content-Disposition","attachment;filename="+ filename);
        int bytesRead = 0;
        //都是用输入流进行先读,然后用输出流去写,用的是缓冲输入输出流
        byte[] buffer = new byte[8192];
        while ((bytesRead = bis.read(buffer, 0, 8192)) != -1)  {
         try{
         bos.write(buffer, 0, bytesRead);
         }catch(Exception e){}
        }
        try{bos.flush();}catch(Exception e){} 
        try{bos.close();}catch(Exception e){}
        try{bis.close();}catch(Exception e){}
        try{fos.close();}catch(Exception e){}
        try{fis.close();}catch(Exception e){}
    }
      

  3.   

    public void DownExcelFile(HttpServletResponse response,String fileDownPath) {
        File file = new File(fileDownPath);
        response.setContentType("text/plain;charset=utf-8");
        if (file.exists()) {
         try {
          log.info("enter try");     
          // 要用servlet 来打开一个 EXCEL 文档,需要将 response 对象中 header 的 contentType 设置成"application/x-msexcel"。
          response.setContentType("application/x-msexcel");
          // 保存文件名称
          fileDownPath = fileDownPath.substring(fileDownPath.lastIndexOf("/") + 1);
          // 处理中文文件名
    //       fileName = new String(fileName.getBytes("GB2312"), "utf-8");
          //servlet中,要在 header中设置下载方式
          response.setHeader("Content-Disposition","attachment; filename=" + fileDownPath);
          //FileInputStream输入流
          //FileInputStream bis = new FileInputStream(file);
          //缓冲流(BufferedStream)可以一次读写一批数据,,缓冲流(Buffered Stream)大大提高了I/O的性能。
          BufferedInputStream  bis = new BufferedInputStream(new FileInputStream(file));
          //OutputStream输出流
          OutputStream bos = response.getOutputStream();
          byte[] buff = new byte[1024];
          int readCount = 0;
          //每次从文件流中读1024个字节到缓冲里。
          readCount = bis.read(buff);
          while (readCount != -1) {
           //把缓冲里的数据写入浏览器
           bos.write(buff, 0, readCount);
           readCount = bis.read(buff);
          }
          if (bis != null) {
           bis.close();
          }
          if (bos != null) {
           bos.close();
          }
          response.setStatus(HttpServletResponse.SC_OK);
          response.flushBuffer();
          response.getWriter().close();
         } catch (Exception e) {
       log.error("DownExcelFile error:" + e.getMessage());
         }
        }
       }
      

  4.   


    public void DownExcelFile(HttpServletResponse response,String fileDownPath) {
        File file = new File(fileDownPath);
        response.setContentType("text/plain;charset=utf-8");
        if (file.exists()) {
         try {
          log.info("enter try");     
          // 要用servlet 来打开一个 EXCEL 文档,需要将 response 对象中 header 的 contentType 设置成"application/x-msexcel"。
          response.setContentType("application/x-msexcel");
          // 保存文件名称
          fileDownPath = fileDownPath.substring(fileDownPath.lastIndexOf("/") + 1);
          // 处理中文文件名
    //       fileName = new String(fileName.getBytes("GB2312"), "utf-8");
          //servlet中,要在 header中设置下载方式
          response.setHeader("Content-Disposition","attachment; filename=" + fileDownPath);
          //FileInputStream输入流
          //FileInputStream bis = new FileInputStream(file);
          //缓冲流(BufferedStream)可以一次读写一批数据,,缓冲流(Buffered Stream)大大提高了I/O的性能。
          BufferedInputStream  bis = new BufferedInputStream(new FileInputStream(file));
          //OutputStream输出流
          OutputStream bos = response.getOutputStream();
          byte[] buff = new byte[1024];
          int readCount = 0;
          //每次从文件流中读1024个字节到缓冲里。
          readCount = bis.read(buff);
          while (readCount != -1) {
           //把缓冲里的数据写入浏览器
           bos.write(buff, 0, readCount);
           readCount = bis.read(buff);
          }
          if (bis != null) {
           bis.close();
          }
          if (bos != null) {
           bos.close();
          }
          response.setStatus(HttpServletResponse.SC_OK);
          response.flushBuffer();
          response.getWriter().close();
         } catch (Exception e) {
       log.error("DownExcelFile error:" + e.getMessage());
         }
        }
       }
      

  5.   

      String str = request.getParameter("exstr");
      //str = java.net.URLDecoder.decode(str, "UTF-8");
    ServletOutputStream sos = response.getOutputStream();// 获取输出流
    response.reset();
    response.setHeader("Content-Disposition", "attachment; filename=\""
    + URLEncoder.encode("文件名称", "UTF8") + ".xls"
    + "\"");// 设定输出文件头
    response.setContentType("application/msexcel");// 设定输出类型 
    OutputStreamWriter osw = new OutputStreamWriter(sos,"gbk");//设置编码以及内容
    osw.write(str);
    osw.flush();  
    osw.close();  
    sos.close(); 
      

  6.   


    我想问我导出是要返回文件名和文件的file是吗
      

  7.   


    //弹出下载对话框的关键代码,filename为显示的保存文件名称
            response.setContentType("application/x-download");
            response.setHeader("Content-Disposition","attachment;filename="+ filename);filename是返回的文件名,在另存为的时候也可以改的。。
    file已经被写入到一个输出流中。。另存为的时候就能取出。。
      

  8.   


    String filepath = "e:\\文件名.xls";
    FileInputStream fis = new FileInputStream(new File(filepath));
    OutputStream os = response.getOutputStream(); response.setHeader("Content-disposition","attachment;filename=" + 
        URLEncoder.encode("文件名.xls", "utf-8"));
    int i = 0;
    byte[] b = new byte[8192];
    while ((i = fis.read(b, 0, 8192)) != -1) 
    {
    os.write(b, 0, i);
    }
    os.flush();
    fis.close();

    return null;
      

  9.   


    帅哥你这个filePath是写导出方法的时候返回的创建workBook的是作为参数的new file(“d:/t.xls”);的引用名吗
      

  10.   


    我想问下3楼的filePath是写导出方法的时候返回的创建workBook的是作为参数的new file(“d:/t.xls”);的引用名吗
      

  11.   


    汗!!!我代码中只是将两者名称都用filename参数罢了你要用别的名字可以随便取
      

  12.   

    我说你的file类型的参数是传什么,你理解错了,我肯定知道随便改名字
      

  13.   

    file类型参数。。是服务器上面的文件。。如excel文件经过了File file = new File("...")包装过的。
    意思就是需要下载的文件。。代码中难道不是很明显就能看出来吗?