/// <summary> /// Note: SSRS cannot render the report if required parameters are missing. /// This will load any report parameters. /// If any of the parameters were required, but they were not provided, show an error message. /// </summary> /// <param name="report">Instance of the ReportViewer control or equiv object</param> private void CheckReportParameters(WebForms.LocalReport report) { //copy-in any report parameters which were not part of the DB query try { WebForms.ReportParameterInfoCollection rdlParams = report.GetParameters(); foreach (WebForms.ReportParameterInfo rdlParam in rdlParams) { if (this._reportParameters.ContainsKey(rdlParam.Name)) { string val = this._reportParameters[rdlParam.Name].ToString(); if (string.IsNullOrEmpty(val)) { if (rdlParam.Nullable) { val = null; report.SetParameters(new WebForms.ReportParameter(rdlParam.Name, val, false)); } else { ErrorMessages.Add(new ErrorMessage("Erro on Report Parameters.", $"Report Parameter value \"{ rdlParam.Name }\" is required, but was not provided.")); } } else { report.SetParameters(new WebForms.ReportParameter(rdlParam.Name, val)); } } else if (!rdlParam.AllowBlank) { ErrorMessages.Add(new ErrorMessage("Erro on Report Parameters.", $"Report Parameter \"{ rdlParam.Name }\" is required, but was not provided.")); } } if (ErrorMessages.Any()) { OnError?.Invoke(this, new EngineErrorEventArgs(ErrorMessages)); } } catch (WebForms.LocalProcessingException ex) { ErrorMessages.Add(new ErrorMessage(ex.Message, ex.StackTrace)); Exception exIn = ex.InnerException; while (exIn != null) { ErrorMessages.Add(new ErrorMessage(exIn.Message, exIn.StackTrace)); exIn = exIn.InnerException; } OnError?.Invoke(this, new EngineErrorEventArgs(ErrorMessages)); } }
public static byte[] GeReportStreamPDF(string reportName, string dataSetName, object dataSet, string reportPath, List <Microsoft.Reporting.WebForms.ReportParameter> parameters = null, Dictionary <string, object> dataSets = null) { Microsoft.Reporting.WebForms.LocalReport report = new Microsoft.Reporting.WebForms.LocalReport(); report.EnableExternalImages = true; report.ReportPath = reportPath + "/" + reportName + ".rdlc"; if (parameters.Count() > 0) { report.SetParameters(parameters); } ReportDataSource rds = new ReportDataSource(); rds.Name = dataSetName; rds.Value = dataSet; report.DataSources.Add(rds); if (dataSets != null) { foreach (KeyValuePair <string, object> kvp in dataSets) { ReportDataSource reportDataSource = new ReportDataSource(); reportDataSource.Name = kvp.Key; reportDataSource.Value = kvp.Value; report.DataSources.Add(reportDataSource); } } string deviceInfo = null; // http://msdn2.microsoft.com/en-us/library/ms155397.aspx string mimeType; string encoding; string fileNameExtension; string[] streams; Microsoft.Reporting.WebForms.Warning[] warnings; byte[] renderedBytes = report.Render( "PDF", deviceInfo, out mimeType, out encoding, out fileNameExtension, out streams, out warnings); return(renderedBytes); }
protected void generate_report_ServerClick(object sender, EventArgs e) { int briefId = 1; ReportGenerator rg = new ReportGenerator(Server.MapPath("~/download/") + Session.SessionID + ".rdlc"); rg.ChartImagePath = Server.MapPath("~/chart-images/"); string reportPath = rg.Generate(briefId); string fileName = "File_" + DateTime.Now.ToString("ddMMyyyyhhmmss") + ".docx"; string fullFilePath = Server.MapPath("~/download/") + fileName; string extension = string.Empty; string encoding = string.Empty; string mimeType = string.Empty; string[] streams; MRW.Warning[] warnings; MRW.LocalReport report = new MRW.LocalReport(); report.ReportPath = reportPath; report.SetParameters(GetReportParameters(briefId)); Byte[] mybytes = report.Render("WORDOPENXML", null, out mimeType, out encoding, out extension, out streams, out warnings); using (FileStream fs = File.Create(fullFilePath)) { fs.Write(mybytes, 0, mybytes.Length); } Response.ClearHeaders(); Response.ClearContent(); Response.Buffer = true; Response.Clear(); Response.ContentType = mimeType; Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName); Response.WriteFile(fullFilePath); Response.Flush(); Response.Close(); Response.End(); }
protected void BindLocalReport(MsLocalReport localReport, IReport report) { ILocalReportProcessingLocation location = (ILocalReportProcessingLocation)report.ReportProcessingLocation; localReport.LoadReportDefinition(ReportDefinitionCache.GetReportDefinition(location.File).GetReportStream()); localReport.EnableHyperlinks = true; localReport.EnableExternalImages = true; localReport.SetBasePermissionsForSandboxAppDomain(new System.Security.PermissionSet(System.Security.Permissions.PermissionState.Unrestricted)); //localReport.ExecuteReportInCurrentAppDomain(AppDomain.CurrentDomain.Evidence); //localReport.AddFullTrustModuleInSandboxAppDomain(new System.Security.Policy.StrongName(new System.Security.Permissions.StrongNamePublicKeyBlob(Nullable),"VB.Reports.App.ReportStyleLibrary.StyleSheet","1.0.0")); //localReport.AddTrustedCodeModuleInCurrentAppDomain(System.Reflection.Assembly.GetAssembly(typeof(VB.Reports.App.ReportStyleLibrary.StyleSheet)).ToString()); if (localReport.IsDrillthroughReport) { localReport.SetParameters(localReport.OriginalParametersToDrillthrough); } else { IList <MsReportParameter> parameters = new List <MsReportParameter>(); foreach (IReportParameter parameter in report.ReportParameters) { parameters.Add(new MsReportParameter(parameter.Name, GetParameterValue(parameter))); } localReport.SetParameters(parameters); } localReport.DataSources.Clear(); foreach (IReportDataCommandTemplate reportDataCommandTemplate in report.ReportDataCommandTemplates) { DictionaryDataParameterValue parameterValues = new DictionaryDataParameterValue(); foreach (IReportParameter parameter in reportDataCommandTemplate.Parameters) { if (localReport.IsDrillthroughReport) { parameterValues[parameter.Name] = GetParameterValue(parameter, localReport.OriginalParametersToDrillthrough); } else { parameterValues[parameter.Name] = GetParameterValue(parameter); } } using (IDataConnection connection = SimpleQuery.ConfigurationManagerConnection(location.DataSourceName)) { if (connection.State == ConnectionState.Closed) { connection.Open(); } using (IDataCommand command = connection.CreateCommand(reportDataCommandTemplate, parameterValues.DataParameterValue)) { command.CommandTimeout = 200; using (IDataReader reader = command.ExecuteReader()) { DataTable table = DataTableMapper.ToDataTable(reader); localReport.DataSources.Add(new MsReportDataSource(reportDataCommandTemplate.Name, table)); } } } } localReport.Refresh(); }
public static void SetReportParameter(Microsoft.Reporting.WebForms.LocalReport localreport, System.Collections.Hashtable Para) { Microsoft.Reporting.WebForms.ReportParameterInfoCollection RPara = localreport.GetParameters(); foreach (Microsoft.Reporting.WebForms.ReportParameterInfo item in RPara) { switch (item.Name) { case "CompanyName": localreport.SetParameters(new Microsoft.Reporting.WebForms.ReportParameter[] { new Microsoft.Reporting.WebForms.ReportParameter(item.Name, Services.GlobalVariant.GetSysCompany()["CompanyName"].ToString()) }); break; case "CompanyAddress": localreport.SetParameters(new Microsoft.Reporting.WebForms.ReportParameter[] { new Microsoft.Reporting.WebForms.ReportParameter(item.Name, Services.GlobalVariant.GetSysCompany()["CompanyAddress"].ToString()) }); break; case "CompanyPhone": localreport.SetParameters(new Microsoft.Reporting.WebForms.ReportParameter[] { new Microsoft.Reporting.WebForms.ReportParameter(item.Name, Services.GlobalVariant.GetSysCompany()["CompanyPhone"].ToString()) }); break; case "CompanyFax": localreport.SetParameters(new Microsoft.Reporting.WebForms.ReportParameter[] { new Microsoft.Reporting.WebForms.ReportParameter(item.Name, Services.GlobalVariant.GetSysCompany()["CompanyFax"].ToString()) }); break; case "CompanyTaxCode": localreport.SetParameters(new Microsoft.Reporting.WebForms.ReportParameter[] { new Microsoft.Reporting.WebForms.ReportParameter(item.Name, Services.GlobalVariant.GetSysCompany()["CompanyTaxCode"].ToString()) }); break; case "QDBTC": localreport.SetParameters(new Microsoft.Reporting.WebForms.ReportParameter[] { new Microsoft.Reporting.WebForms.ReportParameter(item.Name, Services.GlobalVariant.GetSysOption()["QDBTC"].ToString()) }); break; case "DirectorName": localreport.SetParameters(new Microsoft.Reporting.WebForms.ReportParameter[] { new Microsoft.Reporting.WebForms.ReportParameter(item.Name, Services.GlobalVariant.GetSysOption()["DirectorName"].ToString()) }); break; case "ChiefAccountantName": localreport.SetParameters(new Microsoft.Reporting.WebForms.ReportParameter[] { new Microsoft.Reporting.WebForms.ReportParameter(item.Name, Services.GlobalVariant.GetSysOption()["ChiefAccountantName"].ToString()) }); break; case "ReportCreatorName": localreport.SetParameters(new Microsoft.Reporting.WebForms.ReportParameter[] { new Microsoft.Reporting.WebForms.ReportParameter(item.Name, Services.GlobalVariant.GetSysOption()["ReportCreatorName"].ToString()) }); break; case "ReportFontSize": localreport.SetParameters(new Microsoft.Reporting.WebForms.ReportParameter[] { new Microsoft.Reporting.WebForms.ReportParameter(item.Name, Services.GlobalVariant.GetSysOption()["ReportFontSize"].ToString()) }); break; default: if (Para[item.Name] != null) { localreport.SetParameters(new Microsoft.Reporting.WebForms.ReportParameter[] { new Microsoft.Reporting.WebForms.ReportParameter(item.Name, Para[item.Name].ToString()) }); } break; } } }
public static void InitUIReport(Microsoft.Reporting.WebForms.LocalReport localreport, string metaname) { #region Set giá trị cho tham số Report Microsoft.Reporting.WebForms.ReportParameterInfoCollection RPara = localreport.GetParameters(); var Columns = Services.GlobalMeta.GetMetaObject(metaname).GetMetaTable(); foreach (Microsoft.Reporting.WebForms.ReportParameterInfo item in RPara) { if (item.Name.Length > 6) { string FieldName = item.Name.Substring(6); if (Columns.ContainsKey(FieldName)) { if (item.Name.IndexOf("Header") == 0) { localreport.SetParameters(new Microsoft.Reporting.WebForms.ReportParameter[] { new Microsoft.Reporting.WebForms.ReportParameter(item.Name, Columns[FieldName].Des) }); } if (item.Name.IndexOf("Format") == 0) { string Format = ""; if (Columns[FieldName].FormatValue != null) { Format = Columns[FieldName].FormatValue.ToString(); } if (Columns[FieldName].CultureInfo != null) { switch (Columns[FieldName].CultureInfo.ToString()) { case "CIFC": if (Format == "n") { //Format += Services.GlobalVariant.GetSysOption()["RoundAmountFC"].ToString(); Format = "#,##0" + SetRound(int.Parse(Services.GlobalVariant.GetSysOption()["RoundAmountFC"].ToString())); } break; case "CIUPCC": if (Format == "n") { //Format += Services.GlobalVariant.GetSysOption()["RoundUnitPrice"].ToString(); Format = "#,##0" + SetRound(int.Parse(Services.GlobalVariant.GetSysOption()["RoundUnitPrice"].ToString())); } break; case "CIUPFC": if (Format == "n") { //Format += Services.GlobalVariant.GetSysOption()["RoundUnitPriceFC"].ToString(); Format = "#,##0" + SetRound(int.Parse(Services.GlobalVariant.GetSysOption()["RoundUnitPriceFC"].ToString())); } break; default: switch (Format) { case "c": //hiện format dấu tiền tệ //Format += Services.GlobalVariant.GetSysOption()["RoundAmount"].ToString(); //Không hiện format dấu tiền tệ //Format = "n"+Services.GlobalVariant.GetSysOption()["RoundAmount"].ToString(); Format = "#,##0" + SetRound(int.Parse(Services.GlobalVariant.GetSysOption()["RoundAmount"].ToString())); break; case "n": //Format += Services.GlobalVariant.GetSysOption()["RoundQuantity"].ToString(); Format = "#,##0" + SetRound(int.Parse(Services.GlobalVariant.GetSysOption()["RoundQuantity"].ToString())); break; default: break; } break; } } else { switch (Format) { case "c": //hiện format dấu tiền tệ //Format += Services.GlobalVariant.GetSysOption()["RoundAmount"].ToString(); //Không hiện format dấu tiền tệ //Format = "n"+Services.GlobalVariant.GetSysOption()["RoundAmount"].ToString(); Format = "#,##0" + SetRound(int.Parse(Services.GlobalVariant.GetSysOption()["RoundAmount"].ToString())); break; case "n": //Format += Services.GlobalVariant.GetSysOption()["RoundQuantity"].ToString(); Format = "#,##0" + SetRound(int.Parse(Services.GlobalVariant.GetSysOption()["RoundQuantity"].ToString())); break; default: break; } } if (!string.IsNullOrEmpty(Format)) { Format = string.Format("{0};-{1};{2}", Format, Format, "#.#"); //Format = string.Format("{0};{1};'-'", Format, Format); localreport.SetParameters(new Microsoft.Reporting.WebForms.ReportParameter[] { new Microsoft.Reporting.WebForms.ReportParameter(item.Name, Format) }); } } } } } #endregion }