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)
{
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)
看看这儿吧:http://www.yesky.com/20030214/1652186.shtml
这个方法可行,我在PB下也是这么作的。
报表程序的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分? :)
我在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
是什么意思?
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;
就可以浏览该报表了。通过上面的这些例子,我想你会成功的做出报表的。
怎么导入他的名称空间?
using CrystalDisions.CrystalReport.En***;
using CrystalDisions.Sh*****;
怎么出错?
using CrystalDisions.Shared;