关于ActiveReports导出PDF,exce,word的问题。
哪位大哥能帮个忙。
我现在想做一个通用的导出功能。并且多个报表可以调用。
首先我是
得出工具条的代码我是这样写的。
- /// <summary>
- /// 输出报表工具
- /// </summary>
- /// <param name="rdlxurl"></param>
- /// <param name="WebViewers"></param>
- public void sToolTip(string rdlxurl, GrapeCity.ActiveReports.Web.WebViewer WebViewers, string url)
- {
- //设置显示语言版本为中文
- WebViewers.FlashViewerOptions.ResourceLocale = "zh_CN";
- //创建自定义工具条按钮
- ToolButton btnPDF = Tool.CreateButton("PDF");
- btnPDF.ToolTip = "导出到 PDF";
- btnPDF.Caption = "导出到 PDF";
- //设置点击按钮执行的服务
- btnPDF.ClickNavigateTo = url + "/SubSystem/ActiveReports/ARExport.ashx?exporttype=PDF&PageReportFile=" + rdlxurl;
- //添加按钮到 FlashViewer 中
- WebViewers.FlashViewerToolBar.Tools.Add(btnPDF);
- ToolButton btnWord = Tool.CreateButton("Word");
- btnWord.ToolTip = "导出到 Word";
- btnWord.Caption = "导出到 Word";
- btnWord.ClickNavigateTo = url + "/SubSystem/ActiveReports/ARExport.ashx?exporttype=Word&PageReportFile=" + rdlxurl;
- WebViewers.FlashViewerToolBar.Tools.Add(btnWord);
- ToolButton btnExcel = Tool.CreateButton("Excel");
- btnExcel.ToolTip = "导出到 Excel";
- btnExcel.Caption = "导出到 Excel";
- btnExcel.ClickNavigateTo = url + "/SubSystem/ActiveReports/ARExport.ashx?exporttype=Excel&PageReportFile=" + rdlxurl;
- WebViewers.FlashViewerToolBar.Tools.Add(btnExcel);
- }
复制代码
调用实现如下:
- ;
-
- GrapeCity.ActiveReports.PageReport rpt2 = new GrapeCity.ActiveReports.PageReport();
- string rdlxurl = "~/SubSystem/ActiveReports/IOT_Billhead/Active_IOT_Billhead.rdlx";
- rpt2.Load(new System.IO.FileInfo(Server.MapPath(rdlxurl)));
- //生成PDF,EXCEL,WORD 导出按钮
- sToolTip(rdlxurl, this.WebViewer1, HttpContext.Current.Request.Url.ToString().Substring(0, HttpContext.Current.Request.Url.ToString().IndexOf("/SubSystem")));//主要是这一句调佣出工具条
- WebViewer1.Report = rpt2;
复制代码
这样得出工具条,然后点调整即可导出,在调整的页面传相关的参数进去进行读取数据。
然后再地址栏传参数到指定页面进行输出对应的导出文件
- public void ProcessRequest(HttpContext context)
- {
- context.Response.ContentType = "text/html";
- var key = context.Request["exporttype"];
- var PageReportFile = context.Request["PageReportFile"];
- PageReport pr = new PageReport(new System.IO.FileInfo(context.Server.MapPath(PageReportFile)));
- switch (key)
- {
- case "PDF":
- ExportPdf(context, pr);
- break;
- case "Word":
- ExportWord(context, pr);
- break;
- case "Excel":
- ExportExcel(context, pr);
- break;
- }
- }
- private void ExportExcel(HttpContext context, PageReport _reportDef)
- {
- context.Response.ContentType = "application/excel";
- context.Response.Clear();
- context.Response.AddHeader("content-disposition", "attachment;filename=" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls");
- XlsExport xls = new XlsExport();
- System.IO.MemoryStream memStream = new System.IO.MemoryStream();
- xls.FileFormat = FileFormat.Xlsx;
- xls.Export(_reportDef.Document, memStream);
- context.Response.BinaryWrite(memStream.ToArray());
- context.Response.End();
- }
- private void ExportWord(HttpContext context, PageReport pr)
- {
- context.Response.ContentType = "application/msword";
- context.Response.Clear();
- context.Response.HeaderEncoding = System.Text.Encoding.Default;
- context.Response.AddHeader("content-disposition", "attachment;filename=" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".doc");
- var word = new RtfExport();
- var memStream = new System.IO.MemoryStream();
- word.Export(pr.Document, memStream);
- context.Response.BinaryWrite(memStream.ToArray());
- context.Response.End();
- }
- private void ExportPdf(HttpContext context, PageReport pr)
- {
- context.Response.ContentType = "application/pdf";
- context.Response.Clear();
- context.Response.HeaderEncoding = System.Text.Encoding.Default;
- context.Response.AddHeader("content-disposition", "attachment;filename=" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".pdf");
- var pdf = new PdfExport();
- var memStream = new System.IO.MemoryStream();
- pdf.Export(pr.Document, memStream);
- context.Response.BinaryWrite(memStream.ToArray());
- context.Response.Flush();
- context.Response.End();
- }
复制代码
以上是我的导出代码,请问怎么样赋值,或者是怎么样完善它能导出。
- GrapeCity.ActiveReports.PageReport _reportDef = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath("这个是我报表文件")));
- switch (tableName)
- {
- case "IOT_Billhead":
- _reportDef.Document.LocateDataSource += new GrapeCity.ActiveReports.LocateDataSourceEventHandler(“这个是我的数据载入方法”);
- break;
- }
- GrapeCity.ActiveReports.Document.PageDocument _reportRuntime = new GrapeCity.ActiveReports.Document.PageDocument(_reportDef);
- 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(_reportRuntime, ms);
- Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
- Response.AddHeader("content-disposition", Server.UrlPathEncode("attachment;filename=" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".xlsx"));
- Response.BinaryWrite(ms.ToArray());
- Response.End();
复制代码
- protected void Page_Load(object sender, EventArgs e)
- {
- if (Request["tableName"] != null && Request["exporttype"] != null
- && Request["ID"] != null && Request["PageReportFile"] != null &&
- Request["tableName"].ToString().Trim() != "" && Request["exporttype"].ToString().Trim() != ""
- && Request["ID"].ToString().Trim() != "" && Request["PageReportFile"].ToString().Trim() != ""
- )
- {
- string key = Request["exporttype"].ToString().Trim();
- TextBox_ID.Text = Request["ID"].ToString().Trim();
- string PageReportFile = Request["PageReportFile"].ToString().Trim();
- PageReport pr = new PageReport(new System.IO.FileInfo(Server.MapPath(PageReportFile)));
- switch (key)
- {
- case "PDF":
- ExportPdf(pr, Request["tableName"].ToString().Trim(), PageReportFile);
- break;
- case "Word":
- ExportWord(pr, Request["tableName"].ToString().Trim(), PageReportFile);
- break;
- case "Excel":
- ExportExcel(pr, Request["tableName"].ToString().Trim(), PageReportFile);
- break;
- }
- }
- else
- {
- Response.Write("错误!");
- Response.End();
- }
- }
- private void ExportExcel(PageReport pr, string tableName, string PageReportFile)
- {
- //Response.ContentType = "application/excel";
- //Response.Clear();
- //Response.AddHeader("content-disposition", "attachment;filename=" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".xlsx");
- //XlsExport xls = new XlsExport();
- //System.IO.MemoryStream memStream = new System.IO.MemoryStream();
- //xls.FileFormat = FileFormat.Xlsx;
- //xls.Export(pr.Document, memStream);
- //Response.BinaryWrite(memStream.ToArray());
- //Response.End();
- GrapeCity.ActiveReports.PageReport _reportDef = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath(PageReportFile)));
- switch (tableName)
- {
- case "IOT_Billhead":
- _reportDef.Document.LocateDataSource += new GrapeCity.ActiveReports.LocateDataSourceEventHandler(IOT_Billhead_LocateDataSource);
- break;
- }
- GrapeCity.ActiveReports.Document.PageDocument _reportRuntime = new GrapeCity.ActiveReports.Document.PageDocument(_reportDef);
- 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(_reportRuntime, ms);
- Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
- Response.AddHeader("content-disposition", Server.UrlPathEncode("attachment;filename=" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".xlsx"));
- Response.BinaryWrite(ms.ToArray());
- Response.End();
- }
- private void ExportWord(PageReport pr, string tableName, string PageReportFile)
- {
- Response.ContentType = "application/msword";
- Response.Clear();
- Response.HeaderEncoding = System.Text.Encoding.Default;
- Response.AddHeader("content-disposition", "attachment;filename=" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".doc");
- var word = new RtfExport();
- var memStream = new System.IO.MemoryStream();
- word.Export(pr.Document, memStream);
- Response.BinaryWrite(memStream.ToArray());
- Response.End();
- }
- private void ExportPdf(PageReport pr, string tableName, string PageReportFile)
- {
- Response.ContentType = "application/pdf";
- Response.Clear();
- Response.HeaderEncoding = System.Text.Encoding.Default;
- Response.AddHeader("content-disposition", "attachment;filename=" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".pdf");
- var pdf = new PdfExport();
- var memStream = new System.IO.MemoryStream();
- pdf.Export(pr.Document, memStream);
- Response.BinaryWrite(memStream.ToArray());
- Response.Flush();
- Response.End();
- }
- #region IOT_Billhead
- public void IOT_Billhead_LocateDataSource(object sender, GrapeCity.ActiveReports.LocateDataSourceEventArgs args)
- {
- if (args.DataSourceName == "DataSource1")
- {
- if (args.DataSetName == "DataSet1")
- {
- string sqltext = @" select tab1.CreateName,tab1.ID,tab1.PaymentReasons,tab1.ReferenceNumber,tab1.SourceOfCharge,tab5.Remark AS OrganizationName,tab1.CreateTime
- ,tab1.ContractName,(tab6.BillNo)as ContractNo,tab2.Contract_Money,
- isnull(tab1.AccumulativeThePrepaidAmount,0) as AccumulativeThePrepaidAmount
- ,isnull(tab1.ThePrepaidAmount,0) as ThePrepaidAmount,
-
- isnull(tab1.AccumulativeCloseAnAccount,0) as AccumulativeCloseAnAccount
- ,isnull(tab1.CloseAnAccount,0) as CloseAnAccount,
- isnull(tab1.CutPaymentCashDeposit,0) as CutPaymentCashDeposit
- ,isnull(tab1.CutPaymentAdvancePayment,0) as CutPaymentAdvancePayment,
- isnull(tab1.CutPaymentOther,0) as CutPaymentOther,
- isnull(tab1.ActualPayment,0) as ActualPayment
- ,tab1.Remarks,
- tab1.Three_AccumulativeTotal,
- tab1.Three_Total,
- tab1.Three_Remarks,
- isnull(tab1.AccessoryReceipts,0) AccessoryReceipts,
- (isnull(tab1.ThePrepaidAmount,0) +isnull(tab1.ActualPayment,0)+isnull(tab1.Three_Total,0)) AS min_total,
- (select dbo.L2U((isnull(tab1.ThePrepaidAmount,0) +isnull(tab1.ActualPayment,0)+isnull(tab1.Three_Total,0)),0)) AS max_total
- ,
- CASe PaymentMethod when 0 then '√' end as PaymentMethod1,
- CASe PaymentMethod when 1 then '√' end as PaymentMethod2,
- CASe PaymentMethod when 2 then '√' end as PaymentMethod3,tab1.TheInvoiceNumber ,
- tab3.EnterpriseName,tab3.BankName,tab3.Account,tab3.EnterpriseNo,tab1.GeneralLedgerJournalName
- from IOT_Billhead as tab1
- left join IOT_Contract as tab2 on tab1.ContractID=tab2.ID
- LEFT JOIN dbo.IOT_Contract_Body AS tab6 ON tab2.ID=tab6.IOT_ContractID
- left join Project_Enterprise as tab3 on tab1.EnterpriseID=tab3.ID
- left join Sys_User as tab4 on tab1.CreateName=tab4.ID
- left join Sys_User_Organization as tab5 on tab4.ID=tab5.Sys_User_ID
-
-
-
- where tab1.id=" + TextBox_ID.Text.ToString();
- args.Data = new BLL.Sys_DBHelper().GetData(sqltext).Tables[0];
- }
- }
- }
- #endregion
复制代码
以上是我的一个完整代码。调用如上代码。请问为什么提示:
No data has been set. Please specify either a DataSet or a DataView to use |
|