web報表工具Stimulsoft Reports.Web在mvc項目中使用


Stimulsoft Reports.Web,是一款可以直接在Web中編輯報表的報表工具

web項目技術框架mvc4+easyui+knockoutjs

1.在項目中添加引用

Stimulsoft.Base.dll,

Stimulsoft.Report.dll,

Stimulsoft.Report.Web.dll,

Stimulsoft.Report.WebDesign.dll

2.定義模板文件:新建web窗體文件report.aspx

前台代碼:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="report.aspx.cs" Inherits="PE.IEM.Web.report" %>
<%@ Register  Namespace="Stimulsoft.Report.Web" TagPrefix="cc1"  Assembly="Stimulsoft.Report.Web, Version=2012.3.1500.0, Culture=neutral, PublicKeyToken=096a9279a87304f1"%>
<%@ Register  Namespace="Stimulsoft.Report.Web" TagPrefix="cc2"  Assembly="Stimulsoft.Report.WebDesign, Version=2012.3.1500.0, Culture=neutral, PublicKeyToken=096a9279a87304f1"%>

<!doctype html>
<html>
    <head runat="server">
        <title></title>
    </head>
    <body style="background-color: #e8e8e8">
        <form id="form1" runat="server">
        <div style="width: 960px;margin: 0 auto;">
            <cc1:StiWebViewer ID="StiWebViewer1" runat="server"  GlobalizationFile="/Content/page/reports/Localization/zh-CHS.xml" ShowDesignButton="True"  onreportdesign="StiWebViewer1_ReportDesign" Theme="Office2010"  BackColor="#e8e8e8"/>
            <cc2:StiWebDesigner ID="StiWebDesigner1" runat="server" LocalizationDirectory="/Content/page/reports/Localization/" Localization="zh-CHS" onsavereport="StiWebDesigner1_SaveReport" />
        </div>
        </form>
    </body>
</html>

后台代碼:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Stimulsoft.Report;

namespace PE.IEM.Web
{
    public partial class report : System.Web.UI.Page
    {
        /// <summary>
        /// 將獲取的報表放在閱讀器中顯示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            StiWebViewer1.Report = GetReport();
        }

        /// <summary>
        /// 編輯報表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void StiWebViewer1_ReportDesign(object sender, EventArgs e)
        {
            StiWebDesigner1.Design(GetReport());
        }

        /// <summary>
        /// 保存報表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void StiWebDesigner1_SaveReport(object sender, Stimulsoft.Report.Web.StiWebDesigner.StiSaveReportEventArgs e)
        {
            var report = e.Report;
            report.Save(GetReportPath());
        }

        /// <summary>
        /// 獲取報表
        /// </summary>
        /// <returns></returns>
        private StiReport GetReport()
        {
            var report = new StiReport();
            //根據路徑加載報表文件
            report.Load(GetReportPath());
            //動態改變數據庫連接
            ChangeConnectString(report);
            //設置參數等
            report.Compile();
            SetReportParamaters(report);
           
            return report;
        }

        private void SetReportParamaters(StiReport report)
        {
            var dataSource = report.CompiledReport.DataSources;
            foreach (Stimulsoft.Report.Dictionary.StiDataSource ds in dataSource)
            {
                var param = Request.QueryString;
                foreach (string key in param.Keys)
                {
                    if (!ds.Parameters.Contains(key)) continue;
                    var p = ds.Parameters[key];
                    var v = param[key];
                    p.ParameterValue = v;
                }
            }
        }

        private string GetReportPath()
        {
            var path = String.Format("~/Areas/{0}/Reports/{1}.mrt",Request["area"], Request["rpt"]);
            path = Server.MapPath(path);
            if (!System.IO.File.Exists(path))
          //如果報表文件不存在,返回默認的報表文件 path
= Server.MapPath("~/Content/page/reports/helloworld.mrt"); return path; } private void ChangeConnectString(StiReport report) { foreach (Stimulsoft.Report.Dictionary.StiSqlDatabase item in report.Dictionary.Databases) { var prefix = item.Name.Split('_')[0]; item.ConnectionString = ConfigurationManager.ConnectionStrings["sqlConnection"].ConnectionString; } } } }

 

就是把閱讀器和編輯器放在模板中

3.注冊路由:在App_Start文件夾的RouteConfig中注冊報表模板訪問路徑

routes.MapPageRoute("Report", "report", "~/Content/page/report.aspx");

4.js調用:調用報表模板訪問路徑並傳遞2個參數,根據傳遞的參數去查找並加載指定報表文件

this.printClick = function () {
   parent.wrapper.addTab.apply(this,{
'打印報表', '/report?area=XX&rpt=helloworld', 'icon-printer_color'});
};

5.效果展示


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM