MVCxGridViewGeneratorHelper.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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 CustomizeColumnTotalSummaryEventHandler(object source, ColumnSummaryCreationEventArgs e);
  20. public delegate void CustomizeColumnGroupSummaryEventHandler(object source, ColumnSummaryCreationEventArgs e);
  21. public class MVCReportGeneratonHelper
  22. {
  23. const int initialGroupOffset = 0;
  24. const int subGroupOffset = 10;
  25. const int bandHeight = 20;
  26. const bool shouldRepeatGroupHeadersOnEveryPage = false;
  27. XtraReport report;
  28. Hashtable detailsInfo = new Hashtable();
  29. Hashtable detailsWidth = new Hashtable();
  30. public event CustomizeColumnsCollectionEventHandler CustomizeColumnsCollection;
  31. public event CustomizeColumnEventHandler CustomizeColumn;
  32. public event CustomizeColumnTotalSummaryEventHandler CustomizeTotalColumnSummary;
  33. public event CustomizeColumnGroupSummaryEventHandler CustomizeGroupColumnSummary;
  34. public XtraReport GenerateMVCReport(MVCxGridViewState gridViewState, object model)
  35. {
  36. report = new XtraReport
  37. {
  38. Font = new Font("Calibri", 10f, FontStyle.Regular),
  39. Landscape = true,
  40. PaperKind = System.Drawing.Printing.PaperKind.A4
  41. };
  42. InitStyles();
  43. InitDataSource(model);
  44. InitDetailsAndPageHeader(gridViewState);
  45. InitDetailReports(gridViewState);
  46. InitSortings(gridViewState);
  47. InitGroupHeaders(gridViewState);
  48. InitFilters(gridViewState);
  49. InitTotalSummaries(gridViewState);
  50. return report;
  51. }
  52. private void InitStyles()
  53. {
  54. report.StyleSheet.Add(new XRControlStyle
  55. {
  56. Name = "OddRow",
  57. Borders = (DevExpress.XtraPrinting.BorderSide.Bottom |
  58. DevExpress.XtraPrinting.BorderSide.Left |
  59. DevExpress.XtraPrinting.BorderSide.Right),
  60. Padding = new PaddingInfo(30, 600)
  61. });
  62. report.StyleSheet.Add(new XRControlStyle
  63. {
  64. Name = "EvenRow",
  65. BackColor = Color.LightGray,
  66. Borders = (DevExpress.XtraPrinting.BorderSide.Bottom |
  67. DevExpress.XtraPrinting.BorderSide.Left |
  68. DevExpress.XtraPrinting.BorderSide.Right),
  69. Padding = new PaddingInfo(30, 600)
  70. });
  71. report.StyleSheet.Add(new XRControlStyle
  72. {
  73. Name = "HeaderRow",
  74. BackColor = Color.FromArgb(156, 183, 191),
  75. Borders = DevExpress.XtraPrinting.BorderSide.All,
  76. Padding = new PaddingInfo(30, 600),
  77. Font = new Font(report.Font, FontStyle.Bold)
  78. });
  79. report.StyleSheet.Add(new XRControlStyle
  80. {
  81. Name = "GroupHeader",
  82. BackColor = Color.Beige,
  83. Borders = DevExpress.XtraPrinting.BorderSide.All,
  84. Padding = new PaddingInfo(30, 600),
  85. Font = new Font(report.Font, FontStyle.Bold)
  86. });
  87. report.StyleSheet.Add(new XRControlStyle
  88. {
  89. Name = "SummaryRow",
  90. BackColor = Color.FromArgb(35, 133, 160),
  91. Borders = DevExpress.XtraPrinting.BorderSide.All,
  92. BorderColor = Color.FromArgb(35, 133, 160),
  93. Padding = new PaddingInfo(30, 600),
  94. ForeColor = Color.White,
  95. Font = new Font(report.Font, FontStyle.Bold)
  96. });
  97. report.StyleSheet.Add(new XRControlStyle
  98. {
  99. Name = "DetailRow",
  100. BackColor = Color.FromArgb(209, 228, 255),
  101. Borders = DevExpress.XtraPrinting.BorderSide.All
  102. });
  103. report.StyleSheet.Add(new XRControlStyle
  104. {
  105. Name = "DetailRowLabel",
  106. Padding = new PaddingInfo(30, 600),
  107. Borders = DevExpress.XtraPrinting.BorderSide.Bottom |
  108. DevExpress.XtraPrinting.BorderSide.Left |
  109. DevExpress.XtraPrinting.BorderSide.Right
  110. });
  111. }
  112. private void InitDataSource(object model)
  113. {
  114. report.DataSource = model;
  115. }
  116. private void InitDetailsAndPageHeader(MVCxGridViewState gridViewState)
  117. {
  118. var groupedColumns = gridViewState.GroupedColumns;
  119. var pagewidth = (report.PageWidth - (report.Margins.Left + report.Margins.Right)) - groupedColumns.Count * subGroupOffset;
  120. var columns = GetColumnsInfo(gridViewState, pagewidth);
  121. if (CustomizeColumnsCollection != null)
  122. CustomizeColumnsCollection(report, new ColumnsCreationEventArgs(pagewidth)
  123. {
  124. ColumnsInfo = columns
  125. });
  126. report.Bands.Add(new DetailBand()
  127. {
  128. HeightF = bandHeight,
  129. KeepTogetherWithDetailReports = true
  130. });
  131. report.Bands.Add(new PageHeaderBand()
  132. {
  133. HeightF = bandHeight
  134. });
  135. var headerTable = new XRTable()
  136. {
  137. StyleName = "HeaderRow"
  138. };
  139. var row = new XRTableRow();
  140. var detailTable = new XRTable
  141. {
  142. OddStyleName = "OddRow",
  143. EvenStyleName = "EvenRow"
  144. };
  145. var row2 = new XRTableRow();
  146. for (var i = 0; i < columns.Count; i++)
  147. {
  148. if (columns[i].IsVisible)
  149. {
  150. var cell = new XRTableCell
  151. {
  152. Width = columns[i].ColumnWidth,
  153. Text = columns[i].GridViewColumn.Caption,
  154. Borders = DevExpress.XtraPrinting.BorderSide.All,
  155. TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleLeft
  156. };
  157. row.Cells.Add(cell);
  158. var cell2 = new XRTableCell
  159. {
  160. Width = columns[i].ColumnWidth
  161. };
  162. var cc = new ControlCustomizationEventArgs
  163. {
  164. FieldName = columns[i].FieldName,
  165. IsModified = false,
  166. Owner = cell2,
  167. Header = cell
  168. };
  169. if (CustomizeColumn != null)
  170. CustomizeColumn(report, cc);
  171. if (cc.IsModified == false)
  172. {
  173. cell2.DataBindings.Add("Text", null, columns[i].FieldName,
  174. "{0:" + columns[i].GridViewColumn.DisplayFormat + "}");
  175. }
  176. detailsInfo.Add(columns[i].GridViewColumn, cell2);
  177. row2.Cells.Add(cell2);
  178. detailsWidth.Add(columns[i].GridViewColumn, cell2.WidthF);
  179. }
  180. }
  181. headerTable.Rows.Add(row);
  182. headerTable.Width = pagewidth;
  183. headerTable.LocationF = new PointF(groupedColumns.Count * subGroupOffset, 0);
  184. headerTable.Borders = DevExpress.XtraPrinting.BorderSide.Bottom;
  185. detailTable.Rows.Add(row2);
  186. detailTable.LocationF = new PointF(groupedColumns.Count * subGroupOffset, 0);
  187. detailTable.Width = pagewidth;
  188. report.Bands[BandKind.PageHeader].Controls.Add(headerTable);
  189. report.Bands[BandKind.Detail].Controls.Add(detailTable);
  190. }
  191. private void InitDetailReports(MVCxGridViewState gridViewState)
  192. {
  193. var groupedColumns = gridViewState.GroupedColumns;
  194. var pagewidth = (report.PageWidth - (report.Margins.Left + report.Margins.Right)) - groupedColumns.Count * subGroupOffset;
  195. var columns = GetColumnsInfo(gridViewState, pagewidth);
  196. if (CustomizeColumnsCollection != null)
  197. CustomizeColumnsCollection(report, new ColumnsCreationEventArgs(pagewidth)
  198. {
  199. ColumnsInfo = columns
  200. });
  201. var detailColumns = columns
  202. .Where(c => c.IsDetail)
  203. .ToList();
  204. if (!detailColumns.Any()) return;
  205. var detailReportBand = new DetailReportBand()
  206. {
  207. HeightF = bandHeight
  208. };
  209. report.Bands.Add(detailReportBand);
  210. var detailReportTable = new XRTable
  211. {
  212. OddStyleName = "DetailRow",
  213. EvenStyleName = "DetailRow",
  214. WidthF = pagewidth
  215. };
  216. var detailReportRow = new XRTableRow();
  217. foreach (var detailColumn in detailColumns)
  218. {
  219. var cellDetail = new XRTableCell
  220. {
  221. WidthF = pagewidth / detailColumns.Count
  222. };
  223. var label = new XRLabel
  224. {
  225. StyleName = "DetailRowLabel",
  226. WidthF = pagewidth / detailColumns.Count,
  227. WordWrap = true
  228. };
  229. label.DataBindings.Add("Text", null, detailColumn.FieldName,
  230. "{0:" + detailColumn.GridViewColumn.DisplayFormat + "}");
  231. cellDetail.Controls.Add(label);
  232. detailReportRow.Cells.Add(cellDetail);
  233. }
  234. detailReportTable.Rows.Add(detailReportRow);
  235. detailReportTable.LocationF = new PointF(groupedColumns.Count * subGroupOffset, 0);
  236. DetailBand detailBand = new DetailBand();
  237. detailBand.Height = detailReportTable.Height;
  238. detailReportBand.Bands.Add(detailBand);
  239. detailBand.Controls.Add(detailReportTable);
  240. }
  241. private void InitSortings(MVCxGridViewState gridViewState)
  242. {
  243. var columns = gridViewState.Columns;
  244. var groupedColumns = gridViewState.GroupedColumns;
  245. for (var i = 0; i < columns.Count; i++)
  246. {
  247. if (!groupedColumns.Contains(columns[i]))
  248. {
  249. if (columns[i].SortOrder != ColumnSortOrder.None)
  250. ((DetailBand)report.Bands[BandKind.Detail]).SortFields.Add(new GroupField(columns[i].FieldName, columns[i].SortOrder == ColumnSortOrder.Ascending ? XRColumnSortOrder.Ascending : XRColumnSortOrder.Descending));
  251. }
  252. }
  253. }
  254. private void InitGroupHeaders(MVCxGridViewState gridViewState)
  255. {
  256. var groupedColumns = gridViewState.GroupedColumns;
  257. for (var i = groupedColumns.Count - 1; i >= 0; i--)
  258. {
  259. var groupedColumn = groupedColumns[i];
  260. var gb = new GroupHeaderBand
  261. {
  262. Height = bandHeight
  263. };
  264. var groupHeaderTable = new XRTable
  265. {
  266. StyleName = "GroupHeader",
  267. LocationF = new PointF(initialGroupOffset + i * 10, 0),
  268. SizeF = new SizeF((report.PageWidth - (report.Margins.Left + report.Margins.Right)) - (initialGroupOffset + i * subGroupOffset), bandHeight)
  269. };
  270. var groupHeaderRow = new XRTableRow();
  271. var l = new XRTableCell
  272. {
  273. Text = groupedColumn.Caption + ": [" + groupedColumn.FieldName + "]",
  274. Borders = DevExpress.XtraPrinting.BorderSide.Left |
  275. DevExpress.XtraPrinting.BorderSide.Top |
  276. DevExpress.XtraPrinting.BorderSide.Bottom
  277. };
  278. groupHeaderRow.Cells.Add(l);
  279. foreach (MVCxSummaryItemState item in gridViewState.TotalSummary)
  280. {
  281. var sum = new XRTableCell
  282. {
  283. Borders = DevExpress.XtraPrinting.BorderSide.Top |
  284. DevExpress.XtraPrinting.BorderSide.Bottom
  285. };
  286. sum.Summary = new XRSummary
  287. {
  288. Running = SummaryRunning.Group
  289. };
  290. sum.Summary.Func = GetSummaryFunc(item.SummaryType);
  291. sum.DataBindings.Add("Text", null, item.FieldName);
  292. groupHeaderRow.Cells.Add(sum);
  293. if (CustomizeGroupColumnSummary != null)
  294. CustomizeGroupColumnSummary(report, new ColumnSummaryCreationEventArgs(item.FieldName, sum.Summary));
  295. }
  296. groupHeaderTable.Rows.Add(groupHeaderRow);
  297. gb.Controls.Add(groupHeaderTable);
  298. gb.RepeatEveryPage = shouldRepeatGroupHeadersOnEveryPage;
  299. var gf = new GroupField(
  300. groupedColumn.FieldName, groupedColumn.SortOrder == ColumnSortOrder.Ascending
  301. ? XRColumnSortOrder.Ascending
  302. : XRColumnSortOrder.Descending);
  303. gb.GroupFields.Add(gf);
  304. report.Bands.Add(gb);
  305. }
  306. }
  307. private void InitFilters(MVCxGridViewState gridViewState)
  308. {
  309. report.FilterString = gridViewState.FilterExpression;
  310. }
  311. private void InitTotalSummaries(MVCxGridViewState gridViewState)
  312. {
  313. if (gridViewState.TotalSummary.Count == 0) return;
  314. report.Bands.Add(new ReportFooterBand
  315. {
  316. HeightF = bandHeight,
  317. StyleName = "SummaryRow"
  318. });
  319. foreach (MVCxSummaryItemState item in gridViewState.TotalSummary)
  320. {
  321. var col = gridViewState.Columns[item.ShowInColumn == string.Empty
  322. ? item.FieldName
  323. : item.ShowInColumn];
  324. if (col == null) continue;
  325. if (!detailsInfo.Contains(col)) return;
  326. var label = new XRLabel
  327. {
  328. LocationF = new PointF(
  329. ((XRTableCell)detailsInfo[col]).LocationF.X + subGroupOffset * gridViewState.GroupedColumns.Count,
  330. ((XRTableCell)detailsInfo[col]).LocationF.Y),
  331. SizeF = ((XRTableCell)detailsInfo[col]).SizeF
  332. };
  333. label.DataBindings.Add("Text", null, col.FieldName);
  334. label.Summary = new XRSummary
  335. {
  336. Running = SummaryRunning.Report
  337. };
  338. label.Summary.FormatString = item.DisplayFormat;
  339. label.Summary.Func = GetSummaryFunc(item.SummaryType);
  340. report.Bands[BandKind.ReportFooter].Controls.Add(label);
  341. if (CustomizeTotalColumnSummary != null)
  342. CustomizeTotalColumnSummary(report, new ColumnSummaryCreationEventArgs(col.FieldName, label.Summary));
  343. }
  344. }
  345. private List<MVCxColumnInfo> GetColumnsInfo(MVCxGridViewState gridViewState, int pagewidth)
  346. {
  347. var columns = new List<MVCxColumnInfo>();
  348. var visibleColumns = gridViewState.Columns;
  349. foreach (var dataColumn in visibleColumns)
  350. {
  351. MVCxColumnInfo column = new MVCxColumnInfo(dataColumn)
  352. {
  353. ColumnCaption = string.IsNullOrEmpty(dataColumn.Caption) ? dataColumn.FieldName : dataColumn.Caption,
  354. ColumnWidth = ((int)pagewidth / visibleColumns.Count),
  355. FieldName = dataColumn.FieldName,
  356. IsVisible = true
  357. };
  358. columns.Add(column);
  359. }
  360. return columns;
  361. }
  362. private SummaryFunc GetSummaryFunc(SummaryItemType summaryItemType)
  363. {
  364. switch (summaryItemType)
  365. {
  366. case SummaryItemType.Sum:
  367. return SummaryFunc.Sum;
  368. case SummaryItemType.Average:
  369. return SummaryFunc.Avg;
  370. case SummaryItemType.Max:
  371. return SummaryFunc.Max;
  372. case SummaryItemType.Min:
  373. return SummaryFunc.Min;
  374. case SummaryItemType.Count:
  375. return SummaryFunc.Count;
  376. default:
  377. return SummaryFunc.Custom;
  378. }
  379. }
  380. public void WritePdfToResponse(HttpResponseBase Response, string fileName, string type)
  381. {
  382. report.CreateDocument(false);
  383. using (MemoryStream ms = new MemoryStream())
  384. {
  385. report.ExportToPdf(ms);
  386. ms.Seek(0, SeekOrigin.Begin);
  387. WriteResponse(Response, ms.ToArray(), "application/pdf", type, fileName);
  388. }
  389. }
  390. public void WriteXlsToResponse(HttpResponseBase Response, string fileName, string type)
  391. {
  392. report.CreateDocument(false);
  393. using (MemoryStream ms = new MemoryStream())
  394. {
  395. report.ExportToXls(ms);
  396. ms.Seek(0, SeekOrigin.Begin);
  397. WriteResponse(Response, ms.ToArray(), "application/vnd.ms-excel", type, fileName);
  398. }
  399. }
  400. public void WriteXlsxToResponse(HttpResponseBase Response, string fileName, string type)
  401. {
  402. report.CreateDocument(false);
  403. using (MemoryStream ms = new MemoryStream())
  404. {
  405. report.ExportToXlsx(ms);
  406. ms.Seek(0, SeekOrigin.Begin);
  407. WriteResponse(Response, ms.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", type, fileName);
  408. }
  409. }
  410. public static void WriteResponse(HttpResponseBase response, byte[] filearray, string contentType, string type, string fileName)
  411. {
  412. response.ClearContent();
  413. response.Buffer = true;
  414. response.Cache.SetCacheability(HttpCacheability.Private);
  415. response.ContentType = contentType;
  416. ContentDisposition contentDisposition = new ContentDisposition();
  417. contentDisposition.FileName = fileName;
  418. contentDisposition.DispositionType = type;
  419. response.AddHeader("Content-Disposition", contentDisposition.ToString());
  420. response.BinaryWrite(filearray);
  421. HttpContext.Current.ApplicationInstance.CompleteRequest();
  422. try
  423. {
  424. response.End();
  425. }
  426. catch (System.Threading.ThreadAbortException)
  427. {
  428. }
  429. }
  430. }
  431. public class ControlCustomizationEventArgs : EventArgs
  432. {
  433. XRControl owner;
  434. public XRControl Owner
  435. {
  436. get
  437. {
  438. return owner;
  439. }
  440. set
  441. {
  442. owner = value;
  443. }
  444. }
  445. XRControl header;
  446. public XRControl Header
  447. {
  448. get
  449. {
  450. return header;
  451. }
  452. set
  453. {
  454. header = value;
  455. }
  456. }
  457. bool isModified;
  458. public bool IsModified
  459. {
  460. get
  461. {
  462. return isModified;
  463. }
  464. set
  465. {
  466. isModified = value;
  467. }
  468. }
  469. string fieldName;
  470. public string FieldName
  471. {
  472. get
  473. {
  474. return fieldName;
  475. }
  476. set
  477. {
  478. fieldName = value;
  479. }
  480. }
  481. }
  482. public class ColumnSummaryCreationEventArgs : EventArgs
  483. {
  484. public ColumnSummaryCreationEventArgs(string fieldName, XRSummary summary)
  485. {
  486. this.fieldName = fieldName;
  487. this.summary = summary;
  488. }
  489. XRSummary summary;
  490. public XRSummary Summary
  491. {
  492. get
  493. {
  494. return summary;
  495. }
  496. set
  497. {
  498. summary = value;
  499. }
  500. }
  501. string fieldName;
  502. public string FieldName
  503. {
  504. get
  505. {
  506. return fieldName;
  507. }
  508. set
  509. {
  510. fieldName = value;
  511. }
  512. }
  513. }
  514. public class ColumnsCreationEventArgs : EventArgs
  515. {
  516. int pageWidth;
  517. public int PageWidth
  518. {
  519. get
  520. {
  521. return pageWidth;
  522. }
  523. }
  524. public ColumnsCreationEventArgs(int pageWidth)
  525. {
  526. this.pageWidth = pageWidth;
  527. }
  528. List<MVCxColumnInfo> columnsInfo;
  529. public List<MVCxColumnInfo> ColumnsInfo
  530. {
  531. get
  532. {
  533. return columnsInfo;
  534. }
  535. set
  536. {
  537. columnsInfo = value;
  538. }
  539. }
  540. }
  541. public class MVCxColumnInfo
  542. {
  543. public MVCxColumnInfo(GridViewDataColumnState gridViewColumn)
  544. {
  545. this.gridViewColumn = gridViewColumn;
  546. }
  547. GridViewDataColumnState gridViewColumn;
  548. public GridViewDataColumnState GridViewColumn
  549. {
  550. get
  551. {
  552. return gridViewColumn;
  553. }
  554. }
  555. string columnCaption;
  556. public string ColumnCaption
  557. {
  558. get
  559. {
  560. return columnCaption;
  561. }
  562. set
  563. {
  564. columnCaption = value;
  565. }
  566. }
  567. string fieldName;
  568. public string FieldName
  569. {
  570. get
  571. {
  572. return fieldName;
  573. }
  574. set
  575. {
  576. fieldName = value;
  577. }
  578. }
  579. int columnWidth;
  580. public int ColumnWidth
  581. {
  582. get
  583. {
  584. return columnWidth;
  585. }
  586. set
  587. {
  588. columnWidth = value;
  589. }
  590. }
  591. bool isVisible;
  592. public bool IsVisible
  593. {
  594. get
  595. {
  596. return isVisible;
  597. }
  598. set
  599. {
  600. isVisible = value;
  601. }
  602. }
  603. bool isDetail;
  604. public bool IsDetail
  605. {
  606. get
  607. {
  608. return isDetail;
  609. }
  610. set
  611. {
  612. isDetail = value;
  613. }
  614. }
  615. }
  616. }
  617. }