UP而已public void ToExcel(System.Web.UI.Control ctl)  
{
HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=Excel.xls");
HttpContext.Current.Response.Charset ="UTF-8";
HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType ="application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
ctl.Page.EnableViewState =false;
System.IO.StringWriter  tw = new System.IO.StringWriter() ;
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
但是不知道怎么用可以如此调用:ToExcel(DataGrid1)

解决方案 »

  1.   

    装office还不如.net framework呢!!!(才20M)
    看看这儿吧:http://www.yesky.com/20030214/1652186.shtml
      

  2.   

    关键是有些机器配置很低,无法装.net framework
      

  3.   

    我以前做过,是用html画的页面,在里面填数据,效果很不错,关键是位置要对
      

  4.   

    areliang(清风.net)这个方法不行,因为人家要自定义
      

  5.   

    ToExcel可以用,只是不是很理想,因为会导致下载Excel文件,并不是直接打开
      

  6.   

    ToExcel可以用,只是不是很理想,因为会导致下载Excel文件,并不是直接嵌入打开
      

  7.   

    我也试过ToExcel,也是同样的感觉有更好的方法吗?
      

  8.   

    用水晶报表,导出Excel文件后传到客户端。很好用,请参考相关书籍。
      

  9.   

    up
    这个方法可行,我在PB下也是这么作的。
      

  10.   

    to nmgrlt(nmgrlt) 提示一点,我也想知道
      

  11.   

    可以通气一下,但是不要在此打广告,我用高分给你换如何?分数是CSDN的规矩
      

  12.   

    UP¬¬¬¬¬``比较难。
      

  13.   

    这是用C#做的。
    报表程序的PageLoad()中加入:
    //CrystalReport1 为报表名,dataset1 为报表要用的数据集名。 string filename;
    ReportDocument CrReportDocument=new ReportDocument();
    CrReportDocument.Load(Server.MapPath("CrystalReport1.rpt"));
    filename=Server.MapPath("文件名.XLS");
    CrystalReport1 oRpt=new CrystalReport1();
    dataset1 dataset=new dataset1();
                               SqlConnection Cn=new SqlConnection("数据库连接");
    SqlCommand mycommand=new SqlCommand("查询",Cn);
    SqlDataAdapter myad=new SqlDataAdapter();
    myad.SelectCommand=mycommand;
    myad.Fill(dataset,"dataset中的表名");
                                 oRpt.SetDataSource(dataset);
    CrReportDocument.SetDataSource(dataset);
    DiskFileDestinationOptions CrDiskFileDestinationOptions=new DiskFileDestinationOptions();
    ExportOptions CrExportOptions=new ExportOptions();
    CrDiskFileDestinationOptions.DiskFileName=filename;
    CrExportOptions=CrReportDocument.ExportOptions;
    CrExportOptions.DestinationOptions=CrDiskFileDestinationOptions;
    CrExportOptions.ExportDestinationType=ExportDestinationType.DiskFile;
    CrExportOptions.ExportFormatType=ExportFormatType.Excel;
    CrReportDocument.Export();//把转换的文件送到客户端 Response.ContentType="Application/pdf";
    Response.WriteFile("文件名.XLS",true);
    Response.End();
             // 200分?  :)
      

  14.   

    我试试,如果行的话当然给分了,到目前为止,我所有的帖子都有结的
    我在WebForm中放入一个CrystalReport,显示如下的错误,是为什么?我无法如下所列出来的改变什么权限“/WebCrystalReport”应用程序中的服务器错误。
    --------------------------------------------------------------------------------对路径“D:\Program Files\Microsoft Visual Studio .NET\Crystal Reports\Viewers”的访问被拒绝。 
    说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.UnauthorizedAccessException: 对路径“D:\Program Files\Microsoft Visual Studio .NET\Crystal Reports\Viewers”的访问被拒绝。 未授权此 ASP.NET 进程访问所请求的资源。出于安全原因,默认的 ASP.NET 进程标识为“{machinename}\ASPNET”,它只具有有限的特权。请考虑授予该 ASP.NET 进程标识访问此资源的权限。 若要授予 ASP.NET 对文件的写访问权,请在资源管理器中右击该文件,选择“属性”,然后选择“安全”选项卡。单击“添加”以添加“{machinename}\ASPNET”用户。突出显示此 ASP.NET 帐户,在“允许”列中选中“写”框。源错误: 执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。  堆栈跟踪: 
    [UnauthorizedAccessException: 对路径“D:\Program Files\Microsoft Visual Studio .NET\Crystal Reports\Viewers”的访问被拒绝。]
       System.IO.__Error.WinIOError(Int32 errorCode, String str) +393
       System.IO.Directory.InternalCreateDirectory(String fullPath, String path) +596
       System.IO.Directory.CreateDirectory(String path) +195
       CrystalDecisions.Web.ReportAgentBase.get_ImagePhysicalPath()
       CrystalDecisions.Web.ReportAgentBase.p()
       CrystalDecisions.Web.ReportAgent.v(Boolean `)
       CrystalDecisions.Web.CrystalReportViewer.OnPreRender(EventArgs e)
       System.Web.UI.Control.PreRenderRecursiveInternal()
       System.Web.UI.Control.PreRenderRecursiveInternal()
       System.Web.UI.Control.PreRenderRecursiveInternal()
       System.Web.UI.Page.ProcessRequestMain() 
    --------------------------------------------------------------------------------
    版本信息: Microsoft .NET 框架版本:1.0.3705.288; ASP.NET 版本:1.0.3705.288 
      

  15.   

    用C#,就是在wenform中放一个CrystalReportViewer1,然后指定一个rpt就是了帮我解决一下,谢谢
      

  16.   

    请问再ASP.Net中如何使用Crystal Reports
      

  17.   

    “D:\Program Files\Microsoft Visual Studio .NET\Crystal Reports\Viewers”的访问被拒绝。 
    是什么意思?
      

  18.   

    写DLL,满足Windows系列的套打,主要是写两种类型的DLL,一种类型是Win32普通模式的,另外是.NET的,判断前端有没有.NET,如果没有则使用普通的DLL完成套打
      

  19.   

    引用以下命名空间:
    using CrystalDisions.CrystalReport.En***;
    using CrystalDisions.Sh*****;//如果想转换成EXCEL ,报表程序的PageLoad()中加入:
    //CrystalReport1 为报表名,dataset1 为报表要用的数据集名。
    //开始**************************************************************       //上面我写的那段代码。//结束******************************************************************
    //如果想预览:在页面上放一个CrystalReportViewer控件,ID=CrystalReportViewer1
    //报表程序的PageLoad()中加入:
    //CrystalReport1 为报表名,dataset1 为报表要用的数据集名。
    //开始***************************************************************** CrystalReport1 oRpt=new CrystalReport1();
    dataset1 dataset=new dataset1();
                               SqlConnection Cn=new SqlConnection("数据库连接");
    SqlCommand mycommand=new SqlCommand("查询",Cn);
    SqlDataAdapter myad=new SqlDataAdapter();
    myad.SelectCommand=mycommand;
    myad.Fill(dataset,"dataset中的表名");
                                 oRpt.SetDataSource(dataset);                          CrystalReportViewer1.ReportSource=oRpt;
    //结束*********************************************************************我在此只给你提供了如何预览和转换成其他格式的文件的代码。
    具体的怎么设置报表,因内容太多,在此不一一举例,请参考有关书籍。如果你想用 .net提供的报表例子(假设World_Sales_Report.rpt),用以下设置:
    1.把报表文件(World_Sales_Report.rpt)添加到当前项目中。(用“添加已有项”功能)
    2.在页面上放一个CrystalReportViewer1
    3.PageLoad()中加入:
       // 报表名 oRpt=new 报表名();
       World_Sales_Report oRpt=new World_Sales_Report();
       CrystalReportViewer1.ReportSource=oRpt;
    就可以浏览该报表了。通过上面的这些例子,我想你会成功的做出报表的。
      

  20.   

    to: nmgrlt
    怎么导入他的名称空间?
    using CrystalDisions.CrystalReport.En***;
    using CrystalDisions.Sh*****;
    怎么出错?
      

  21.   

    using CrystalDisions.CrystalReport.Engine;
    using CrystalDisions.Shared;