ReportStorageWebExtension.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using DevExpress.XtraReports.UI;
  2. using Microsoft.AspNetCore.Hosting;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Web;
  10. namespace MTWorkHR.Infrastructure.Reports
  11. {
  12. public class CustomReportStorageWebExtension : DevExpress.XtraReports.Web.Extensions.ReportStorageWebExtension
  13. {
  14. // private readonly GlobalInfo globalInfo;
  15. // private readonly AppSettingsConfiguration settings;
  16. public CustomReportStorageWebExtension()
  17. {
  18. }
  19. public override byte[] GetData(string url)
  20. {
  21. try
  22. {
  23. // Parse the string with the report name and parameter values.
  24. string[] parts = url.Split('?');
  25. string reportName = parts[0];
  26. string parametersQueryString = parts.Length > 1 ? parts[1] : String.Empty;
  27. // Create a report instance.
  28. XtraReport report= getReport(reportName);
  29. if (report != null)
  30. {
  31. // Apply the parameter values to the report.
  32. var parameters = HttpUtility.ParseQueryString(parametersQueryString);
  33. foreach (string parameterName in parameters.AllKeys)
  34. {
  35. if (parameters.Get(parameterName) != "null"&& parameters.Get(parameterName) != null)
  36. {
  37. report.Parameters[parameterName].Value = Convert.ChangeType(
  38. parameters.Get(parameterName), report.Parameters[parameterName].Type);
  39. }
  40. }
  41. // Disable the Visible property for all report parameters
  42. // to hide the Parameters Panel in the viewer.
  43. foreach (var parameter in report.Parameters)
  44. {
  45. parameter.Visible = false;
  46. }
  47. // If you do not hide the panel, disable the report's RequestParameters property.
  48. // report.RequestParameters = false;
  49. using (MemoryStream ms = new MemoryStream())
  50. {
  51. report.SaveLayoutToXml(ms);
  52. return ms.ToArray();
  53. }
  54. }
  55. }
  56. catch (Exception ex)
  57. {
  58. throw new DevExpress.XtraReports.Web.ClientControls.FaultException(
  59. "Could not get report data.", ex);
  60. }
  61. throw new DevExpress.XtraReports.Web.ClientControls.FaultException(
  62. string.Format("Could not find report '{0}'.", url));
  63. }
  64. private XtraReport getReport(string reportName)
  65. {
  66. switch (reportName)
  67. {
  68. case "ContractReport":
  69. return new ContractReport();
  70. case "EmployeeContract":
  71. return new EmployeeContract();
  72. default: return null;
  73. }
  74. }
  75. }
  76. }