【Stimulsoft Reports.Net教程】在DesignerFx中加載和保存報表

【下載Stimulsoft Reports.Netstimulsoft最新版本】

首先,我們需要將Flash設計器組件放在ASPX頁面上並定義必要的事件處理器:OnSaveReport用於保存報表模板,而OnPreviewReport用於註冊預覽數據。還可以創建一個表和Web控件,允許以多種方式加載報表。

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="Load_and_Save_Report_in_the_DesignerFx.Default" %>
<%@ Register assembly="Stimulsoft.Report.WebDesign" namespace="Stimulsoft.Report.Web" tagprefix="cc1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head runat="server">
<title>Load and Save Report in the DesignerFx</title>
<style type="text/css">
.style1 {
width: 80px;
text-align: center;
}
.style2 {
width: 260px;
vertical-align: top;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<cc1:StiWebDesignerFx ID="StiWebDesignerFx1" runat="server" visible="false"
OnSaveReport="StiWebDesignerFx1_SaveReport"
OnPreviewReport="StiWebDesignerFx1_PreviewReport" />
<table>
<tr>
<td class="style2">
<asp:Label ID="Label1" runat="server" Font-Names="Arial" Font-Size="11pt"
Text='1. Choose ".mrt" file'></asp:Label><br />
<asp:Label ID="Label2" runat="server" Font-Names="Arial" Font-Size="11pt"
Text='2. Click "Design" button'></asp:Label><br />
<br />
<asp:FileUpload ID="FileReport" runat="server" Font-Names="Arial" Font-Size="11pt" Width="264px" />
<br />
<br />
<asp:Button ID="ButtonDesignFile" runat="server" Text="Design" Width="141px"
OnClick="ButtonDesignFile_Click" />
<br />
</td>

            <td class="style1"><b>or</b></td>

            <td class="style2">
                <asp:Label ID="Label3" runat="server" Font-Names="Arial" Font-Size="11pt"
                        Text='1. Choose Report on server'></asp:Label><br />
                <asp:Label ID="Label4" runat="server" Font-Names="Arial" Font-Size="11pt"
                        Text='2. Click "Design" button'></asp:Label><br />
                <br />
                <asp:DropDownList ID="DropDownListReport" runat="server" Width="250px">
                    <asp:ListItem Value="SimpleList.mrt"></asp:ListItem>
                    <asp:ListItem Value="TwoSimpleLists.mrt"></asp:ListItem>
                    <asp:ListItem Value="Invoice.mrt"></asp:ListItem>
                    <asp:ListItem Value="Shapes.mrt"></asp:ListItem>
                </asp:DropDownList>
                <br />
                <br />
                <asp:Button ID="ButtonDesignServer" runat="server" Text="Design" Width="141px"
                        onclick="ButtonDesignServer_Click" />
                <br />
                <td class="style1"><b>or</b></td>

                <td class="style2">
                    <br />
                    <br />
                    <br />
                    <asp:Button ID="ButtonDesignNew" runat="server" Text="Design New Report" 
                            Width="157px" onclick="ButtonDesignNew_Click" />
                </td>
            </td>
        </tr>
    </table>
</div>
</form>

</body>
</html>
在下一步中,創建按鈕三個單擊處理程序,以多種方式加載報表模板。該ButtonDesignFile_Click方法加載從本地計算機的報表中,ButtonDesignServer_Click方法從列表加載selecter報表和ButtonDesignNew_Click方法創建新的報表模板。

protected void ButtonDesignFile_Click(object sender, EventArgs e)
{
if (FileReport.PostedFile != null && FileReport.PostedFile.FileName.Length > 0 &&
FileReport.PostedFile.InputStream != null)
{
StiReport report = new StiReport();
report.Load(FileReport.PostedFile.InputStream);
StiWebDesignerFx1.Design(report);
}
}

protected void ButtonDesignServer_Click(object sender, EventArgs e)
{
if (DropDownListReport.Text != null && DropDownListReport.Text.Length > 0)
{
string applicationDirectory = HttpContext.Current.Server.MapPath(string.Empty);
string reportFileName = applicationDirectory + "\Reports\" + DropDownListReport.Text;

    StiReport report = new StiReport();
    report.Load(reportFileName);
    StiWebDesignerFx1.Design(report);
}

}

protected void ButtonDesignNew_Click(object sender, EventArgs e)
{
StiReport report = new StiReport();
StiWebDesignerFx1.Design(report);
}
該StiWebDesignerFx1_SaveReport方法通過點擊Flash設計的保存按鈕調用。在此方法的參數中,將傳遞報表模板對象。您可以將此報表模板保存到文件,將數據庫保存爲打包字符串或使用其他方式保存。您還可以設置保存報表後將在設計器中顯示的錯誤代碼或字符串消息。

protected void StiWebDesignerFx1_SaveReport(object sender, StiSaveReportEventArgs e)
{
// Web Designer return StiReport object in the e.Report property
var reportString = e.Report.SaveToString();

// You can set the error code which will be displayed by the designer after saving
// -1: default value, the message is not displayed
// 0: display 'Report is successfully saved' message
//e.ErrorCode = 1;

// Also you can set the custom message, it will be displayed after saving
e.ErrorString = "Your report has been saved.";

}
如果需要爲報表預覽註冊一些數據,則應定義StiWebDesignerFx1_PreviewReport方法。在此方法中,您可以加載和註冊必要的數據集,例如加載樣本報表的XML數據。

protected void StiWebDesignerFx1_PreviewReport(object sender, StiReportDataEventArgs e)
{
string applicationDirectory = HttpContext.Current.Server.MapPath(string.Empty);

DataSet data = new DataSet();
data.ReadXml(applicationDirectory + "\\Data\\Demo.xml");
data.ReadXmlSchema(applicationDirectory + "\\Data\\Demo.xsd");

e.Report.RegData(data);

}
示例代碼的結果如下圖所示:

【Stimulsoft Reports.Net教程】在DesignerFx中加載和保存報表

查看原文,下載示例news

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章