您好,
excel导出主要使用XlsExport和Rendering两个方法。
XlsExport可以用于导出rpx和rdlx报表
Rendering只能用于导出rdlx报表。
虽然,XlsExport和Rendering都可以导出rdlx的报表,但是Rendering是最新实现的方法,对很多地方进行了增强。
XlsExport导出rpx格式的报表
- protected void Button5_Click(object sender, EventArgs e)
- {
- GrapeCity.ActiveReports.SectionReport sr = new GrapeCity.ActiveReports.SectionReport();
- System.Xml.XmlTextReader xtr = new System.Xml.XmlTextReader(Server.MapPath("") + "\\Reports\\RP_YN_YarnSalesSummary.rpx");
- sr.LoadLayout(xtr);
- xtr.Close();
- GrapeCity.ActiveReports.Export.Excel.Section.XlsExport XlsExport1 = new GrapeCity.ActiveReports.Export.Excel.Section.XlsExport();
- System.IO.MemoryStream ms = new System.IO.MemoryStream();
- XlsExport1.FileFormat = GrapeCity.ActiveReports.Export.Excel.Section.FileFormat.Xlsx;
- XlsExport1.Export(sr.Document, ms);
- ms.Position = 0;
- Response.ContentType = "application/vnd.ms-excel";
- Response.AddHeader("content-disposition", Server.UrlPathEncode("attachment;filename=MyExport.xlsx"));
- Response.BinaryWrite(ms.ToArray());
- Response.End();
- }
复制代码
Rendering导出rdlx格式的报表
- protected void Button3_Click(object sender, EventArgs e)
- {
- // Provide the page report you want to render.
- GrapeCity.ActiveReports.PageReport _reportDef = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath("/Reports/" + report + ".rdlx")));
- _reportDef.Report.DataSources[0].DataSourceReference = "";
- _reportDef.Report.DataSources[0].ConnectionProperties.DataProvider = "OLEDB";
- _reportDef.Report.DataSources[0].ConnectionProperties.ConnectString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};", Server.MapPath("/Data/NWind_CHS.mdb"));
- GrapeCity.ActiveReports.Document.PageDocument _reportRuntime = new GrapeCity.ActiveReports.Document.PageDocument(_reportDef);
- // Create an output directory
- System.IO.MemoryStream ms = new System.IO.MemoryStream();
- // Provide settings for your rendering output.
- GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtensionSettings
- excelSetting = new GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtensionSettings();
- excelSetting.FileFormat = GrapeCity.ActiveReports.Export.Excel.Page.FileFormat.Xls;
- //excelSetting.MultiSheet = false;
- GrapeCity.ActiveReports.Extensibility.Rendering.ISettings setting = excelSetting;
- //Set the rendering extension and render the report.
- GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtension
- excelRenderingExtension = new
- GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtension();
- GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider outputProvider = new GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider();
- _reportRuntime.Render(excelRenderingExtension, outputProvider, excelSetting.GetSettings());
- Response.ContentType = "application/vnd.ms-excel";
- Response.AddHeader("content-disposition", "inline;filename=MyExport.xls");
- outputProvider.GetPrimaryStream().OpenStream().CopyTo(ms);
- Response.BinaryWrite(ms.ToArray());
- Response.End();
- }
复制代码
希望能帮助到您。 |