下面是导出按钮的部分代码:        //string fileurl = "C:\\";            //默认路径在C盘根目录
        string fileurl = TextBox1.Text.Trim();
        string filename = TextBox2.Text.Trim();
。        CrystalDecisions.Shared.DiskFileDestinationOptions DiskOpts = new CrystalDecisions.Shared.DiskFileDestinationOptions();
        ReportDoc.ExportOptions.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile;
        switch (dropdlist1.SelectedValue)
        {
            case "Rich Text (RTF)":
                ReportDoc.ExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.RichText;//
                DiskOpts.DiskFileName = fileurl+filename+".rtf";
                break;
            case "Portable Document (PDF)":
                ReportDoc.ExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat;//
                DiskOpts.DiskFileName = fileurl + filename+".pdf";
                break;
            case "MS Word (DOC)":
                ReportDoc.ExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.WordForWindows;//
                DiskOpts.DiskFileName = fileurl + filename + ".doc";
                break;
            case "MS Excel (XLS)":
                ReportDoc.ExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.Excel;//
                DiskOpts.DiskFileName = fileurl + filename + ".xls";
                break;
            default:
                break;
        }
        ReportDoc.ExportOptions.DestinationOptions = DiskOpts;
        try
        {
            ReportDoc.Export();
            Response.Write("<script defer>alert('导出数据成功');location.href='../MLiulan/Default2.aspx?type="+type+"&id="+id+"';</script>");
        }
        catch (Exception err)
        {
            Response.Write("<script defer>alert('出现错误,原因可能是" + err.Message + "');</script>");
        }
        finally
        {
            SqlConn.Close();
        }
上面是部分代码已经实现了水晶报表的导出,可是我做的是B/S的系统,点击导出时报表导出到了服务器上,不是客户端电脑上,如何把它保存到客户端电脑的磁盘中呢?急急急。

解决方案 »

  1.   

    用这个方法下载#region 下载服务器上的文件
        /// <summary>
        /// 下载服务器上的文件
        /// </summary>
        /// <param name="PageResponse">程序中可以设置参数:HttpResponse ht=Page.Response;</param>
        /// <param name="serverPath">服务器上的文件路径</param>
        public void DownloadFile(HttpResponse response, string serverPath)
        {
            FileStream fs = null;
            try
            {
                fs = File.OpenRead(serverPath);
                byte[] buffer = new byte[1024];
                long count = 1024;
                response.Buffer = true;
                response.AddHeader("Connection", "Keep-Alive");
                response.ContentType = "application/octet-stream";
                response.AddHeader("Content-Disposition", "attachment;filename=" + Path.GetFileName(serverPath));//下载时要保存的默认文件名
                response.AddHeader("Content-Length", fs.Length.ToString());
                while (count == 1024)
                {
                    count = fs.Read(buffer, 0, 1024);
                    response.BinaryWrite(buffer);
                }
            }
            catch
            {
            }
            finally
            {
                fs.Close();
            }
        }
        #endregion