MVCxGridViewGeneratorHelper.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. using DevExpress.Data;
  2. using DevExpress.XtraEditors.Controls;
  3. using DevExpress.XtraPrinting;
  4. using DevExpress.XtraReports.UI;
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using System.Drawing;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Net.Mime;
  12. using System.Web;
  13. namespace GreenTree.Nachtragsmanagement.Web.Extensions
  14. {
  15. public class MVCxGridViewGeneratorHelper
  16. {
  17. public delegate void CustomizeColumnsCollectionEventHandler(object source, ColumnsCreationEventArgs e);
  18. public delegate void CustomizeColumnEventHandler(object source, ControlCustomizationEventArgs e);
  19. public delegate void CustomizeColumnSummaryEventHandler(object source, ColumnSummaryCreationEventArgs e);
  20. public class MVCReportGeneratonHelper
  21. {
  22. const int initialGroupOffset = 0;
  23. const int subGroupOffset = 10;
  24. const int bandHeight = 20;
  25. const bool shouldRepeatGroupHeadersOnEveryPage = false;
  26. XtraReport report;
  27. Hashtable detailsInfo = new Hashtable();
  28. Hashtable detailsWidth = new Hashtable();
  29. public event CustomizeColumnsCollectionEventHandler CustomizeColumnsCollection;
  30. public event CustomizeColumnEventHandler CustomizeColumn;
  31. public event CustomizeColumnSummaryEventHandler CustomizeColumnSummary;
  32. public XtraReport GenerateMVCReport(MVCxGridViewState gridViewState, object model)
  33. {
  34. report = new XtraReport
  35. {
  36. Font = new Font("Calibri", 10f, FontStyle.Regular),
  37. Landscape = true,
  38. PaperKind = System.Drawing.Printing.PaperKind.A4
  39. };
  40. InitStyles();
  41. InitDataSource(model);
  42. InitDetailsAndPageHeader(gridViewState);
  43. InitSortings(gridViewState);
  44. InitGroupHeaders(gridViewState);
  45. InitFilters(gridViewState);
  46. InitTotalSummaries(gridViewState);
  47. return report;
  48. }
  49. private void InitStyles()
  50. {
  51. report.StyleSheet.Add(new XRControlStyle
  52. {
  53. Name = "OddRow",
  54. Borders = (DevExpress.XtraPrinting.BorderSide.Bottom |
  55. DevExpress.XtraPrinting.BorderSide.Left |
  56. DevExpress.XtraPrinting.BorderSide.Right),
  57. Padding = new PaddingInfo(30, 600)
  58. });
  59. report.StyleSheet.Add(new XRControlStyle
  60. {
  61. Name = "EvenRow",
  62. BackColor = Color.LightGray,
  63. Borders = (DevExpress.XtraPrinting.BorderSide.Bottom |
  64. DevExpress.XtraPrinting.BorderSide.Left |
  65. DevExpress.XtraPrinting.BorderSide.Right),
  66. Padding = new PaddingInfo(30, 600)
  67. });
  68. report.StyleSheet.Add(new XRControlStyle
  69. {
  70. Name = "HeaderRow",
  71. BackColor = Color.FromArgb(156, 183, 191),
  72. Borders = DevExpress.XtraPrinting.BorderSide.All,
  73. Padding = new PaddingInfo(30, 600),
  74. Font = new Font(report.Font, FontStyle.Bold)
  75. });
  76. report.StyleSheet.Add(new XRControlStyle
  77. {
  78. Name = "SummaryRow",
  79. BackColor = Color.FromArgb(35, 133, 160),
  80. Borders = DevExpress.XtraPrinting.BorderSide.All,
  81. BorderColor = Color.FromArgb(35, 133, 160),
  82. Padding = new PaddingInfo(30, 600),
  83. ForeColor = Color.White,
  84. Font = new Font(report.Font, FontStyle.Bold)
  85. });
  86. }
  87. private void InitDataSource(object model)
  88. {
  89. report.DataSource = model;
  90. }
  91. private void InitDetailsAndPageHeader(MVCxGridViewState gridViewState)
  92. {
  93. var groupedColumns = gridViewState.GroupedColumns;
  94. var pagewidth = (report.PageWidth - (report.Margins.Left + report.Margins.Right)) - groupedColumns.Count * subGroupOffset;
  95. var columns = GetColumnsInfo(gridViewState, pagewidth);
  96. if (CustomizeColumnsCollection != null)
  97. CustomizeColumnsCollection(report, new ColumnsCreationEventArgs(pagewidth)
  98. {
  99. ColumnsInfo = columns
  100. });
  101. report.Bands.Add(new DetailBand()
  102. {
  103. HeightF = bandHeight
  104. });
  105. report.Bands.Add(new PageHeaderBand()
  106. {
  107. HeightF = bandHeight
  108. });
  109. var headerTable = new XRTable()
  110. {
  111. StyleName = "HeaderRow"
  112. };
  113. var row = new XRTableRow();
  114. var detailTable = new XRTable
  115. {
  116. OddStyleName = "OddRow",
  117. EvenStyleName = "EvenRow"
  118. };
  119. var row2 = new XRTableRow();
  120. for (var i = 0; i < columns.Count; i++)
  121. {
  122. if (columns[i].IsVisible)
  123. {
  124. var cell = new XRTableCell
  125. {
  126. Width = columns[i].ColumnWidth,
  127. Text = columns[i].GridViewColumn.Caption,
  128. Borders = DevExpress.XtraPrinting.BorderSide.All,
  129. TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleLeft
  130. };
  131. row.Cells.Add(cell);
  132. var cell2 = new XRTableCell
  133. {
  134. Width = columns[i].ColumnWidth
  135. };
  136. var cc = new ControlCustomizationEventArgs
  137. {
  138. FieldName = columns[i].FieldName,
  139. IsModified = false,
  140. Owner = cell2,
  141. Header = cell
  142. };
  143. if (CustomizeColumn != null)
  144. CustomizeColumn(report, cc);
  145. if (cc.IsModified == false)
  146. cell2.DataBindings.Add("Text", null, columns[i].FieldName,
  147. "{0:" + columns[i].GridViewColumn.DisplayFormat + "}");
  148. detailsInfo.Add(columns[i].GridViewColumn, cell2);
  149. row2.Cells.Add(cell2);
  150. detailsWidth.Add(columns[i].GridViewColumn, cell2.WidthF);
  151. }
  152. }
  153. headerTable.Rows.Add(row);
  154. headerTable.Width = pagewidth;
  155. headerTable.LocationF = new PointF(groupedColumns.Count * subGroupOffset, 0);
  156. headerTable.Borders = DevExpress.XtraPrinting.BorderSide.Bottom;
  157. detailTable.Rows.Add(row2);
  158. detailTable.LocationF = new PointF(groupedColumns.Count * subGroupOffset, 0);
  159. detailTable.Width = pagewidth;
  160. report.Bands[BandKind.PageHeader].Controls.Add(headerTable);
  161. report.Bands[BandKind.Detail].Controls.Add(detailTable);
  162. }
  163. private void InitSortings(MVCxGridViewState gridViewState)
  164. {
  165. var columns = gridViewState.Columns;
  166. var groupedColumns = gridViewState.GroupedColumns;
  167. for (var i = 0; i < columns.Count; i++)
  168. {
  169. if (!groupedColumns.Contains(columns[i]))
  170. {
  171. if (columns[i].SortOrder != ColumnSortOrder.None)
  172. ((DetailBand)report.Bands[BandKind.Detail]).SortFields.Add(new GroupField(columns[i].FieldName, columns[i].SortOrder == ColumnSortOrder.Ascending ? XRColumnSortOrder.Ascending : XRColumnSortOrder.Descending));
  173. }
  174. }
  175. }
  176. private void InitGroupHeaders(MVCxGridViewState gridViewState)
  177. {
  178. GridViewDataColumnStateCollection groupedColumns = gridViewState.GroupedColumns;
  179. for (int i = groupedColumns.Count - 1; i >= 0; i--)
  180. {
  181. {
  182. GridViewDataColumnState groupedColumn = groupedColumns[i];
  183. GroupHeaderBand gb = new GroupHeaderBand();
  184. gb.Height = bandHeight;
  185. XRLabel l = new XRLabel();
  186. l.Text = groupedColumn.FieldName + ": [" + groupedColumn.FieldName + "]";
  187. l.LocationF = new PointF(initialGroupOffset + i * 10, 0);
  188. l.BackColor = Color.Beige;
  189. l.SizeF = new SizeF((report.PageWidth - (report.Margins.Left + report.Margins.Right)) - (initialGroupOffset + i * subGroupOffset), bandHeight);
  190. gb.Controls.Add(l);
  191. gb.RepeatEveryPage = shouldRepeatGroupHeadersOnEveryPage;
  192. GroupField gf = new GroupField(groupedColumn.FieldName, groupedColumn.SortOrder == ColumnSortOrder.Ascending ? XRColumnSortOrder.Ascending : XRColumnSortOrder.Descending);
  193. gb.GroupFields.Add(gf);
  194. report.Bands.Add(gb);
  195. }
  196. }
  197. }
  198. private void InitFilters(MVCxGridViewState gridViewState)
  199. {
  200. report.FilterString = gridViewState.FilterExpression;
  201. }
  202. private void InitTotalSummaries(MVCxGridViewState gridViewState)
  203. {
  204. if (gridViewState.TotalSummary.Count == 0) return;
  205. report.Bands.Add(new ReportFooterBand
  206. {
  207. HeightF = bandHeight,
  208. StyleName = "SummaryRow"
  209. });
  210. foreach (MVCxSummaryItemState item in gridViewState.TotalSummary)
  211. {
  212. var col = gridViewState.Columns[item.ShowInColumn == string.Empty ? item.FieldName : item.ShowInColumn];
  213. if (col == null) continue;
  214. if (!detailsInfo.Contains(col)) return;
  215. var label = new XRLabel
  216. {
  217. LocationF = ((XRTableCell)detailsInfo[col]).LocationF,
  218. SizeF = ((XRTableCell)detailsInfo[col]).SizeF
  219. };
  220. label.DataBindings.Add("Text", null, col.FieldName);
  221. label.Summary = new XRSummary
  222. {
  223. Running = SummaryRunning.Report
  224. };
  225. label.Summary.FormatString = item.DisplayFormat;
  226. label.Summary.Func = GetSummaryFunc(item.SummaryType);
  227. report.Bands[BandKind.ReportFooter].Controls.Add(label);
  228. if (CustomizeColumnSummary != null)
  229. CustomizeColumnSummary(report, new ColumnSummaryCreationEventArgs(col.FieldName, label.Summary));
  230. }
  231. }
  232. private List<MVCxColumnInfo> GetColumnsInfo(MVCxGridViewState gridViewState, int pagewidth)
  233. {
  234. var columns = new List<MVCxColumnInfo>();
  235. var visibleColumns = gridViewState.Columns;
  236. foreach (var dataColumn in visibleColumns)
  237. {
  238. MVCxColumnInfo column = new MVCxColumnInfo(dataColumn)
  239. {
  240. ColumnCaption = string.IsNullOrEmpty(dataColumn.Caption) ? dataColumn.FieldName : dataColumn.Caption,
  241. ColumnWidth = ((int)pagewidth / visibleColumns.Count),
  242. FieldName = dataColumn.FieldName,
  243. IsVisible = true
  244. };
  245. columns.Add(column);
  246. }
  247. return columns;
  248. }
  249. private SummaryFunc GetSummaryFunc(SummaryItemType summaryItemType)
  250. {
  251. switch (summaryItemType)
  252. {
  253. case SummaryItemType.Sum:
  254. return SummaryFunc.Sum;
  255. case SummaryItemType.Average:
  256. return SummaryFunc.Avg;
  257. case SummaryItemType.Max:
  258. return SummaryFunc.Max;
  259. case SummaryItemType.Min:
  260. return SummaryFunc.Min;
  261. case SummaryItemType.Count:
  262. return SummaryFunc.Count;
  263. default:
  264. return SummaryFunc.Custom;
  265. }
  266. }
  267. public void WritePdfToResponse(HttpResponseBase Response, string fileName, string type)
  268. {
  269. report.CreateDocument(false);
  270. using (MemoryStream ms = new MemoryStream())
  271. {
  272. report.ExportToPdf(ms);
  273. ms.Seek(0, SeekOrigin.Begin);
  274. WriteResponse(Response, ms.ToArray(), "application/pdf", type, fileName);
  275. }
  276. }
  277. public void WriteXlsToResponse(HttpResponseBase Response, string fileName, string type)
  278. {
  279. report.CreateDocument(false);
  280. using (MemoryStream ms = new MemoryStream())
  281. {
  282. report.ExportToXls(ms);
  283. ms.Seek(0, SeekOrigin.Begin);
  284. WriteResponse(Response, ms.ToArray(), "application/vnd.ms-excel", type, fileName);
  285. }
  286. }
  287. public void WriteXlsxToResponse(HttpResponseBase Response, string fileName, string type)
  288. {
  289. report.CreateDocument(false);
  290. using (MemoryStream ms = new MemoryStream())
  291. {
  292. report.ExportToXlsx(ms);
  293. ms.Seek(0, SeekOrigin.Begin);
  294. WriteResponse(Response, ms.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", type, fileName);
  295. }
  296. }
  297. public static void WriteResponse(HttpResponseBase response, byte[] filearray, string contentType, string type, string fileName)
  298. {
  299. response.ClearContent();
  300. response.Buffer = true;
  301. response.Cache.SetCacheability(HttpCacheability.Private);
  302. response.ContentType = contentType;
  303. ContentDisposition contentDisposition = new ContentDisposition();
  304. contentDisposition.FileName = fileName;
  305. contentDisposition.DispositionType = type;
  306. response.AddHeader("Content-Disposition", contentDisposition.ToString());
  307. response.BinaryWrite(filearray);
  308. HttpContext.Current.ApplicationInstance.CompleteRequest();
  309. try
  310. {
  311. response.End();
  312. }
  313. catch (System.Threading.ThreadAbortException)
  314. {
  315. }
  316. }
  317. }
  318. public class ControlCustomizationEventArgs : EventArgs
  319. {
  320. XRControl owner;
  321. public XRControl Owner
  322. {
  323. get
  324. {
  325. return owner;
  326. }
  327. set
  328. {
  329. owner = value;
  330. }
  331. }
  332. XRControl header;
  333. public XRControl Header
  334. {
  335. get
  336. {
  337. return header;
  338. }
  339. set
  340. {
  341. header = value;
  342. }
  343. }
  344. bool isModified;
  345. public bool IsModified
  346. {
  347. get
  348. {
  349. return isModified;
  350. }
  351. set
  352. {
  353. isModified = value;
  354. }
  355. }
  356. string fieldName;
  357. public string FieldName
  358. {
  359. get
  360. {
  361. return fieldName;
  362. }
  363. set
  364. {
  365. fieldName = value;
  366. }
  367. }
  368. }
  369. public class ColumnSummaryCreationEventArgs : EventArgs
  370. {
  371. public ColumnSummaryCreationEventArgs(string fieldName, XRSummary summary)
  372. {
  373. this.fieldName = fieldName;
  374. this.summary = summary;
  375. }
  376. XRSummary summary;
  377. public XRSummary Summary
  378. {
  379. get
  380. {
  381. return summary;
  382. }
  383. set
  384. {
  385. summary = value;
  386. }
  387. }
  388. string fieldName;
  389. public string FieldName
  390. {
  391. get
  392. {
  393. return fieldName;
  394. }
  395. set
  396. {
  397. fieldName = value;
  398. }
  399. }
  400. }
  401. public class ColumnsCreationEventArgs : EventArgs
  402. {
  403. int pageWidth;
  404. public int PageWidth
  405. {
  406. get
  407. {
  408. return pageWidth;
  409. }
  410. }
  411. public ColumnsCreationEventArgs(int pageWidth)
  412. {
  413. this.pageWidth = pageWidth;
  414. }
  415. List<MVCxColumnInfo> columnsInfo;
  416. public List<MVCxColumnInfo> ColumnsInfo
  417. {
  418. get
  419. {
  420. return columnsInfo;
  421. }
  422. set
  423. {
  424. columnsInfo = value;
  425. }
  426. }
  427. }
  428. public class MVCxColumnInfo
  429. {
  430. public MVCxColumnInfo(GridViewDataColumnState gridViewColumn)
  431. {
  432. this.gridViewColumn = gridViewColumn;
  433. }
  434. GridViewDataColumnState gridViewColumn;
  435. public GridViewDataColumnState GridViewColumn
  436. {
  437. get
  438. {
  439. return gridViewColumn;
  440. }
  441. }
  442. string columnCaption;
  443. public string ColumnCaption
  444. {
  445. get
  446. {
  447. return columnCaption;
  448. }
  449. set
  450. {
  451. columnCaption = value;
  452. }
  453. }
  454. string fieldName;
  455. public string FieldName
  456. {
  457. get
  458. {
  459. return fieldName;
  460. }
  461. set
  462. {
  463. fieldName = value;
  464. }
  465. }
  466. int columnWidth;
  467. public int ColumnWidth
  468. {
  469. get
  470. {
  471. return columnWidth;
  472. }
  473. set
  474. {
  475. columnWidth = value;
  476. }
  477. }
  478. bool isVisible;
  479. public bool IsVisible
  480. {
  481. get
  482. {
  483. return isVisible;
  484. }
  485. set
  486. {
  487. isVisible = value;
  488. }
  489. }
  490. }
  491. }
  492. }