如何在MVC项目中抓取报表参数用于后台的导出

发布时间:2015/07/30 00:07 发布者:frank.zhang

返回博客中心

ActiveReports 支持多种格式的报表导出,包括PDF、Excel、Word、RTF、HTML、Text、TIFF以及其它图片格式.本文介绍如何在后台导出带有参数的ActiveReports 报表。

1.在MVC 4 中,运行报表

2015-07-30_100422

 

2.使用JavaScript进行参数的抓取,可以看到对应的三种类型

2015-07-30_100804

 

3.将所有的参数抓取

    		var arr = [];
    		
    		arr.push($("#parametersPane .parameterTextBox").val());
    		arr.push($("#parametersPane .parameterComboBox option:selected" ).text());
    		
            $("#parametersPane .parameterListBox input:checked").each(
			   function () {
			   	var t = $(this).next().text();
			   	arr.push(t);
			   }
		   );

 

4.通过给subForm追加隐藏的input实现参数的传递

            $('#subForm').append('<input type="hidden" name="paramCount" value="' + arr.length + '">');
            arr.forEach(function (item, i, arr) {
            	$('#subForm').append('<input type="hidden" name="param' + i + '" value="' + item + '">');
            });

 

5.在前台调用

<div style="border:0;">
    @using (Html.BeginForm("dcExcel", "Home", FormMethod.Post, new { id = "subForm", onsubmit = "return appendParam();" }))
    {
	    <div><button type="submit">导出Excel</button></div>
        @Html.Partial("WeBViewer", ViewData.Model as object)
    }
</div>

 

6.后台进行处理

需要注意的是货主城市是通过key和value的方式传递的,需要使用Dictionary进行关联

            int paramCount = Convert.ToInt32(Request["paramCount"]);
            GrapeCity.ActiveReports.PageReport _reportDef = new GrapeCity.ActiveReports.PageReport(new FileInfo(Server.MapPath("/Reports/RdlReport1.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"));
            var paramsInReport = _reportDef.Report.ReportParameters[2].ValidValues;
            Dictionary<string, string> allValidParams = new Dictionary<string, string>();
            foreach (var param in paramsInReport.ParameterValues)
            {
                var p = (ParameterValue)param;
                allValidParams.Add(p.Label, p.Value);
            }
            int numberParam;
            for (int i = 0; i < paramCount; i++)
            {
                string paramKey = "param{0}";
                if (i < 2)
                {
                    numberParam = i;
                }
                else
                {
                    numberParam = 2;
                }
                paramKey = string.Format(paramKey, i);
                string param = Request[paramKey];
                if (numberParam < 2)
                {
                    _reportDef.Report.ReportParameters[numberParam].DefaultValue.Values.Add(param);
                }
                else
                {
                    if (allValidParams.ContainsKey(param))
                        _reportDef.Report.ReportParameters[numberParam].DefaultValue.Values.Add(allValidParams[param]);
                }
            }

 

7.导出为一个Excel,可以根据需要修改为任意的导出格式

            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();
            GrapeCity.ActiveReports.Extensibility.Rendering.ISettings setting = excelSetting;
            //Set the rendering extension and render the report.
            GrapeCity.ActiveReports.Export.Excel.Page.ExcelTransformationDevice
            excelRenderingExtension = new GrapeCity.ActiveReports.Export.Excel.Page.ExcelTransformationDevice();
            GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider outputProvider = new GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider();
            _reportRuntime.Render(excelRenderingExtension, outputProvider, excelSetting.GetSettings());
            outputProvider.GetPrimaryStream().OpenStream().CopyTo(ms);
            return File(ms.ToArray(), "application/vnd.ms-excel", Url.Encode("test.xls"));

 

示例下载:


关于葡萄城

赋能开发者!葡萄城是专业的集开发工具、商业智能解决方案、低代码开发平台于一身的软件和服务提供商,为超过 75% 的全球财富 500 强企业提供服务。葡萄城专注控件软件领域30年,希望通过模块化的开发控件、灵活的低代码应用开发平台等一系列开发工具、解决方案和服务,帮助开发者快速响应复杂多变的业务需求,最大程度地发挥开发者的才智和潜能,让开发者的 IT 人生更从容更美好。

了解详情,请访问葡萄城官网