发布时间 : 2018-10-08 13:25:06.000|阅读 396 次
概述:在Stimulsoft显示如何将报表导出为各种格式。
相关链接:
【下载Stimulsoft Reports.Net最新版本】
此示例项目显示如何将报表导出为各种格式。对于此操作,只需使用实现所有导出格式的方法的特殊StiReportResponse类即可。这些方法接受所有必要参数的输入以配置导出。 例如,为各种报表实现几种流行的导出格式。在ASPX页面上添加选择列表,还添加导出按钮并在Web查看器中调用报表的预览页面。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Export_Report_from_Code.Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Export Report from Code</title> </head> <body> <form id="form1" runat="server"> <h2><span style="color: #0066ff">Export Example</span></h2> <table> <tr> <td width="200px"> <strong>Select Report:</strong> </td> <td width="200px"> <strong>Select Output Format:</strong> </td> </tr> <tr> <td> <asp:ListBox ID="ListBoxReports" runat="server" Height="100px"> <asp:ListItem Selected="True">Anchors</asp:ListItem> <asp:ListItem>MasterDetail</asp:ListItem> <asp:ListItem>MultiColumnList</asp:ListItem> <asp:ListItem>SimpleGroup</asp:ListItem> <asp:ListItem>SimpleList</asp:ListItem> </asp:ListBox> </td> <td> <asp:ListBox ID="ListBoxFormats" runat="server" Height="100px"> <asp:ListItem Selected="True">Pdf</asp:ListItem> <asp:ListItem>Html</asp:ListItem> <asp:ListItem>Xls</asp:ListItem> <asp:ListItem>Txt</asp:ListItem> <asp:ListItem>Rtf</asp:ListItem> </asp:ListBox> </td> </tr> <tr> <td> <asp:Button ID="ButtonPreview" runat="server" Text="Preview" onclick="ButtonPreview_Click" /> </td> <td> <asp:Button ID="ButtonExport" runat="server" Text="Export" onclick="ButtonExport_Click" /> </td> </tr> </table> </form> </body> </html>
在ButtonExport_Click事件中加载报表,连接到数据并以所选格式导出报表。使用StiReportResponse类的静态方法导出报表。
protected void ButtonExport_Click(object sender, EventArgs e) { StiReport report = new StiReport(); DataSet data = new DataSet(); report.Load(Server.MapPath(String.Format("Reports\\{0}.mrt", ListBoxReports.SelectedItem.Text))); data.ReadXml(Server.MapPath(@"Data\Demo.xml")); report.RegData(data); switch (ListBoxFormats.SelectedIndex) { case 0: StiReportResponse.ResponseAsPdf(report); break; case 1: StiReportResponse.ResponseAsHtml(report); break; case 2: StiReportResponse.ResponseAsXls(report); break; case 3: StiReportResponse.ResponseAsText(report); break; case 4: StiReportResponse.ResponseAsRtf(report); break; } }
所选报表可以显示在查看器中。为此,请使用ButtonPreview_Click事件,该事件将报表名称保存在会话中,并使用报表查看器重定向到Report.aspx页面。
protected void ButtonPreview_Click(object sender, EventArgs e) { Session["reportname"] = ListBoxReports.SelectedItem.Text; Response.Redirect("Report.aspx"); }
示例代码的结果如下图所示:
购买Stimulsoft正版授权,请点击“咨询在线客服”哟!