功能是:点击导出的时候,把查询出来的内容导出到Excel表中,并把文件上传到服务器上,弹出一个保存的对话框.。
哪位高手请指点一下,谢谢

解决方案 »

  1.   

    网上虽然很多用法,用例啊,但是我表示我还没有搞明白,而且赋值他们的代码之后我的还没有成功,特来询问。我的程序大致的意思是将excel与sql server2000的数据导入和导出.导入导出功能我用jxl.jar都已经实现了.但是我用tomcat发布之后,发现了一个问题.就是别人登录后,无法使用导入导出的功能,在服务器就可以.(这是肯定的了,导入导出的功能都是在服务器实现的,我需要将导入的excel表上传到服务器,这就引出了文件的上传和下载,由此知道了smartupload这个jar包).弄了一中午,都睡着了还没弄出明白.直接上代码
    <input name="fileupload" type="file" />
    <input type="checkbox" name="isDeleteTableData" value="checked">
    <p>
    <input name ="upload" type="submit" value="确定" /><input name="cancel" type="reset" value="取消"/>
    <%
    String fPathUp=request.getParameter("fileupload");
    SmartUpload su=null;
    if(fPathUp!=null)
    {
    String thisfPathUp="";
    try
    {
    su = new SmartUpload();
      // 上传初始化
      su.initialize(pageContext);  //其中这一句我不明白什么意思,我怎么觉得搞来搞去,我用表单提交的文件哪去了
      // 设定允许上传的文件(通过扩展名限制),仅允许doc,txt文件。
      su.setAllowedFilesList("xls,XLS");
      // 上传文件
      su.upload();
      // 将上传文件全部保存到指定目录
      int count = su.save("/myimport.xls");
      out.println(count+"个文件上传成功!<br>");
      thisfPathUp="/myimport.xls";
      }
      catch(Exception ex)
      {
      out.println("上传失败!");
      }
      

  2.   

    文件下载:protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //processRequest(request, response);
        String fileName = "", fileName1 = "",flag="",filePath="",logPath="";
        FileInputStream fis = null;
        BufferedOutputStream out = null;
        try {
            if (request.getParameter("filename") != null) {
                fileName = request.getParameter("filename");
            }        fileName1 = StringUtil.toUtf8String(fileName.substring(fileName.lastIndexOf("/")+1));        response.setContentType("application/octet-stream");
            response.setContentType("application/OCTET-STREAM;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName1);        filePath = yourFilePath;
            
            File file = new File(filePath + fileName);        try {
                fis = new FileInputStream(file);
                out = new BufferedOutputStream(response.getOutputStream());
                byte[] buffer = new byte[1024];
                int len;
                while ((len = fis.read(buffer)) != -1) {
                    out.write(buffer, 0, len);
                    out.flush();
                }
            } catch (FileNotFoundException e) {
                throw new AppException("您下载的文件文件不存在。");
            //e.printStackTrace();
            }
        } catch (Exception e) {
            String msg="";
            response.reset();
            response.setContentType("text/html;charset=UTF-8");
            if(e.getLocalizedMessage()!=null)
                msg=e.getMessage();
            else
                msg="系统错误,捕捉的错误类型为"+e.getClass();
            response.getWriter().println("<script>parent.downback('"+ fileName + "','" + msg + "');</script>");
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
      

  3.   

    你可以用jxl将这个excel文件产生在流里面,然后内容赋好之后,流可以下载的。
      

  4.   

    文件生成时,直接读取成流文件,out.ptint(io)
      

  5.   

    我是这样做的 
    页面里有个iframe
    这个是下载方法
    function(){
    dowmurl = "t_BUSI_ACOUNT.do?method=statisticsWord&"+searchPanel.getForm().getValues(true);
      downf.location.href = dowmurl;
    }
    //进入这个方法
    @RequestMapping(params = RMP_EXPORTBYCONDITION)
    public String exportByCondition(HttpServletRequest request,HttpServletResponse response) {
    String filename="我的存取款记录.xls";
    String configName="bankcash.xml";
    Map<String, String> params = RequestUtils.buildParamsByNamesFromRequest(request);
    String rqs = request.getParameter("rqs");
    String rqe = request.getParameter("rqe");
    String type = request.getParameter("type");
    User user = (User)RequestUtils.getCurrentUser(request);
    OutputStream outStream=null;
            try {   
             String tempfileName = new String(filename.getBytes("GBK"),"ISO8859_1");       
             response.setContentType("application/vnd.ms-excel"); 
            response.addHeader("Content-Disposition", "attachment; filename="+tempfileName); 
            //获取系统所在服务器本地路径
            String webPath = request.getSession().getServletContext().getRealPath("/");
            System.out.println(webPath);
            //创建一个存放下载文件的临时目录
            String tempFile = "tempDownLoad";
            File tempDir = new File(webPath + "/" + tempFile);
            if(!tempDir.exists()){
             tempDir.mkdirs();
            }
            outStream = response.getOutputStream();
            myBankCashService.exportByCondition(outStream,user,rqs,rqe,type, configName);
         outStream.flush();
         outStream.close();
            }catch(Exception e)
            {
             e.printStackTrace();
            }
            return "BPM/client/blank";
    }//这个是内部实现
    Workbook hssf;
    hssf = BuildXLSHelper.buildXLS(configure, record);
    //out是前面传进来的
    hssf.write(out);记得给分
      

  6.   

    这个在服务器上见一个文件夹先把文件传到服务器上在进行读写InputStream is = null;
    OutputStream fos = null;
    int byteSum = 0;//文件字节数
    try{
    is = formFile.getInputStream();
    if(is.available()==0){
    return 0;
    }

    fos = new FileOutputStream(filePath + "/" + fileName);//建立一个上传文件的输出流
    int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ( (bytesRead = is.read(buffer, 0, 8192)) != -1) {
                fos.write(buffer, 0, bytesRead);//将文件写入服务器
                byteSum += bytesRead;
            }
            logger.debug(filePath+"/"+formFile.getFileName()+"已经建立");