Report.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using Microsoft.Reporting.WebForms;
  6. using System.Net;
  7. using System.Collections.Specialized;
  8. using System.IO;
  9. using Spire.Pdf;
  10. using System.Globalization;
  11. using System.Text;
  12. using Newtonsoft.Json;
  13. using System.Drawing.Printing;
  14. using System.Drawing;
  15. using System.Diagnostics;
  16. using Aspose.Pdf.Facades;
  17. namespace EmisTerminal.EIF
  18. {
  19. public class Report
  20. {
  21. public static void PrintStudentScore(Guid userID)
  22. {
  23. string reportpath = "/EducationResult/GztyPersonalStudentScoreView";
  24. List<ReportParameter> parameters = new List<ReportParameter>();
  25. parameters.Add(new ReportParameter("LoginID", (string)null));
  26. parameters.Add(new ReportParameter("UserID", userID.ToString()));
  27. parameters.Add(new ReportParameter("CollegeID", (string)null));
  28. parameters.Add(new ReportParameter("StandardID", (string)null));
  29. parameters.Add(new ReportParameter("YearID", (string)null));
  30. parameters.Add(new ReportParameter("SchoolyearNumID", (string)null));
  31. parameters.Add(new ReportParameter("SchoolcodeID", (string)null));
  32. parameters.Add(new ReportParameter("ClassmajorID", (string)null));
  33. PrintReport(reportpath, parameters);
  34. }
  35. public static string GenerateStudentScore(Guid userID, string idNum)
  36. {
  37. string reportpath = "/EducationResult/GztyPersonalStudentScoreView";
  38. List<ReportParameter> parameters = new List<ReportParameter>();
  39. parameters.Add(new ReportParameter("LoginID", (string)null));
  40. parameters.Add(new ReportParameter("UserID", userID.ToString()));
  41. parameters.Add(new ReportParameter("CollegeID", (string)null));
  42. parameters.Add(new ReportParameter("StandardID", (string)null));
  43. parameters.Add(new ReportParameter("YearID", (string)null));
  44. parameters.Add(new ReportParameter("SchoolyearNumID", (string)null));
  45. parameters.Add(new ReportParameter("SchoolcodeID", (string)null));
  46. parameters.Add(new ReportParameter("ClassmajorID", (string)null));
  47. return GenerateReport(reportpath, idNum, parameters);
  48. }
  49. private static string GenerateReport(string reportpath, string idNum, List<ReportParameter> parameters = null)
  50. {
  51. string User = Const.LOCAL_SETTING_REPORT_USER_NAME;
  52. string Pass = Const.LOCAL_SETTING_REPORT_PASSWORD;
  53. string ReportServerUrl = Const.LOCAL_SETTING_REPORT_URL;
  54. IReportServerCredentials irsc = new CustomReportCredentials(User, Pass, "");
  55. Uri uri = new Uri(ReportServerUrl);
  56. ReportViewer reportViewer = new ReportViewer();
  57. reportViewer.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Remote;
  58. reportViewer.ServerReport.ReportServerCredentials = irsc;
  59. reportViewer.ServerReport.ReportServerUrl = uri;
  60. reportViewer.ServerReport.ReportPath = reportpath;
  61. if (parameters != null && parameters.Count != 0)
  62. {
  63. reportViewer.ServerReport.SetParameters(parameters);
  64. }
  65. string deviceInfo = ReportPrintHelper.CreateEMFDeviceInfo(reportViewer.ServerReport);
  66. // Generating Image renderer pages one at a time can be expensive. In order
  67. // to generate page 2, the server would need to recalculate page 1 and throw it
  68. // away. Using PersistStreams causes the server to generate all the pages in
  69. // the background but return as soon as page 1 is complete.
  70. NameValueCollection firstPageParameters = new NameValueCollection();
  71. firstPageParameters.Add("rs:PersistStreams", "True");
  72. // GetNextStream returns the next page in the sequence from the background process
  73. // started by PersistStreams.
  74. NameValueCollection nonFirstPageParameters = new NameValueCollection();
  75. nonFirstPageParameters.Add("rs:GetNextStream", "True");
  76. string mimeType;
  77. string fileExtension;
  78. Stream pageStream = reportViewer.ServerReport.Render("PDF", deviceInfo, firstPageParameters, out mimeType, out fileExtension);
  79. PDFSign signControl = new PDFSign();
  80. string filePath = signControl.Sign("StudentScore" + idNum, pageStream);
  81. return filePath;
  82. }
  83. private static void PrintReport(string reportpath, List<ReportParameter> parameters = null)
  84. {
  85. var fileString = GenerateReport(reportpath, "1045123", parameters);
  86. PdfDocument pdf = new PdfDocument(HttpContext.Current.Server.MapPath(fileString));
  87. pdf.Print();
  88. }
  89. public static void PrintFile(string reportFilePath, Guid userID)
  90. {
  91. PdfViewer viewer = new PdfViewer();
  92. viewer.BindPdf(HttpContext.Current.Server.MapPath(reportFilePath));
  93. viewer.PrintDocument();
  94. viewer.Close();
  95. string postString = "userID=" + userID.ToString();
  96. byte[] postData = Encoding.UTF8.GetBytes(postString);
  97. HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(Const.LOCAL_SETTING_EMIS_URL + "/PrintServices/Count");
  98. req.ContentType = "application/x-www-form-urlencoded";
  99. req.Method = "POST";
  100. req.Referer = HttpContext.Current.Request.Url.AbsoluteUri;
  101. Stream reqStream = req.GetRequestStream();
  102. reqStream.Write(postData, 0, postData.Length);
  103. reqStream.Close();
  104. HttpWebResponse response = (HttpWebResponse)req.GetResponse();
  105. StreamReader resReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
  106. string srcString = resReader.ReadToEnd();
  107. var returnObj = JsonConvert.DeserializeObject<ResultMessage<string>>(srcString);
  108. if (!returnObj.IsSuccess)
  109. {
  110. throw new Exception(returnObj.Message);
  111. }
  112. }
  113. }
  114. public class CustomReportCredentials : IReportServerCredentials
  115. {
  116. private string _UserName;
  117. private string _PassWord;
  118. private string _DomainName;
  119. public CustomReportCredentials(string UserName, string PassWord, string DomainName)
  120. {
  121. _UserName = UserName;
  122. _PassWord = PassWord;
  123. _DomainName = DomainName;
  124. }
  125. public System.Security.Principal.WindowsIdentity ImpersonationUser
  126. {
  127. get { return null; }
  128. }
  129. public ICredentials NetworkCredentials
  130. {
  131. get { return new NetworkCredential(_UserName, _PassWord, _DomainName); }
  132. }
  133. public bool GetFormsCredentials(out Cookie authCookie, out string user,
  134. out string password, out string authority)
  135. {
  136. authCookie = null;
  137. user = password = authority = null;
  138. return false;
  139. }
  140. }
  141. }