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"));

 

示例下载: