| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813 |
- using Autofac;
- using DevExpress.Data;
- using DevExpress.XtraEditors.Controls;
- using DevExpress.XtraPrinting;
- using DevExpress.XtraReports.UI;
- using GreenTree.Nachtragsmanagement.Core;
- using GreenTree.Nachtragsmanagement.Core.Authentication;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Net.Mime;
- using System.Web;
- namespace GreenTree.Nachtragsmanagement.Web.Extensions
- {
- public class MVCxGridViewGeneratorHelper
- {
- public delegate void CustomizeColumnsCollectionEventHandler(object source, ColumnsCreationEventArgs e);
- public delegate void CustomizeColumnEventHandler(object source, ControlCustomizationEventArgs e);
- public delegate void CustomizeColumnTotalSummaryEventHandler(object source, ColumnSummaryCreationEventArgs e);
- public delegate void CustomizeColumnGroupSummaryEventHandler(object source, ColumnSummaryCreationEventArgs e);
- public class MVCReportGeneratonHelper
- {
- const int initialGroupOffset = 0;
- const int subGroupOffset = 10;
- const int bandHeight = 20;
- const bool shouldRepeatGroupHeadersOnEveryPage = false;
- XtraReport report;
- Hashtable detailsInfo = new Hashtable();
- Hashtable detailsWidth = new Hashtable();
- public event CustomizeColumnsCollectionEventHandler CustomizeColumnsCollection;
- public event CustomizeColumnEventHandler CustomizeColumn;
- public event CustomizeColumnTotalSummaryEventHandler CustomizeTotalColumnSummary;
- public event CustomizeColumnGroupSummaryEventHandler CustomizeGroupColumnSummary;
- public XtraReport GenerateMVCReport(MVCxGridViewState gridViewState, object model, string title)
- {
- report = new XtraReport
- {
- Font = new Font("Calibri", 10f, FontStyle.Regular),
- Landscape = true,
- PaperKind = System.Drawing.Printing.PaperKind.A4
- };
- InitStyles();
- InitReportTitle(title);
- InitDataSource(model);
- InitDetailsAndPageHeader(gridViewState);
- InitDetailReports(gridViewState);
- InitSortings(gridViewState);
- InitGroupHeaders(gridViewState);
- InitFilters(gridViewState);
- InitTotalSummaries(gridViewState);
- InitPageFooter();
- return report;
- }
- private void InitStyles()
- {
- report.StyleSheet.Add(new XRControlStyle
- {
- Name = "Title",
- Borders = DevExpress.XtraPrinting.BorderSide.None,
- Font = new Font(report.Font.FontFamily, 24f, FontStyle.Regular),
- ForeColor = Color.FromArgb(35, 133, 160)
- });
- report.StyleSheet.Add(new XRControlStyle
- {
- Name = "OddRow",
- Borders = (DevExpress.XtraPrinting.BorderSide.Bottom |
- DevExpress.XtraPrinting.BorderSide.Left |
- DevExpress.XtraPrinting.BorderSide.Right),
- Padding = new PaddingInfo(30, 600)
- });
- report.StyleSheet.Add(new XRControlStyle
- {
- Name = "EvenRow",
- BackColor = Color.LightGray,
- Borders = (DevExpress.XtraPrinting.BorderSide.Bottom |
- DevExpress.XtraPrinting.BorderSide.Left |
- DevExpress.XtraPrinting.BorderSide.Right),
- Padding = new PaddingInfo(30, 600)
- });
- report.StyleSheet.Add(new XRControlStyle
- {
- Name = "HeaderRow",
- BackColor = Color.FromArgb(156, 183, 191),
- Borders = DevExpress.XtraPrinting.BorderSide.All,
- Padding = new PaddingInfo(30, 600),
- Font = new Font(report.Font, FontStyle.Bold)
- });
- report.StyleSheet.Add(new XRControlStyle
- {
- Name = "GroupHeader",
- BackColor = Color.Beige,
- Borders = DevExpress.XtraPrinting.BorderSide.All,
- Padding = new PaddingInfo(30, 600),
- Font = new Font(report.Font, FontStyle.Bold)
- });
- report.StyleSheet.Add(new XRControlStyle
- {
- Name = "SummaryRow",
- BackColor = Color.FromArgb(35, 133, 160),
- Borders = DevExpress.XtraPrinting.BorderSide.All,
- BorderColor = Color.FromArgb(35, 133, 160),
- Padding = new PaddingInfo(30, 600),
- ForeColor = Color.White,
- Font = new Font(report.Font, FontStyle.Bold)
- });
- report.StyleSheet.Add(new XRControlStyle
- {
- Name = "DetailRow",
- BackColor = Color.FromArgb(209, 228, 255),
- Borders = DevExpress.XtraPrinting.BorderSide.All
- });
- report.StyleSheet.Add(new XRControlStyle
- {
- Name = "DetailRowLabel",
- Padding = new PaddingInfo(30, 600),
- Borders = DevExpress.XtraPrinting.BorderSide.Bottom |
- DevExpress.XtraPrinting.BorderSide.Left |
- DevExpress.XtraPrinting.BorderSide.Right
- });
- report.StyleSheet.Add(new XRControlStyle
- {
- Name = "FooterRow",
- Font = new Font(report.Font, FontStyle.Italic)
- });
- }
- private void InitReportTitle(string title)
- {
- report.Bands.Add(new ReportHeaderBand
- {
- HeightF = 32f
- });
- report.Bands[BandKind.ReportHeader].Controls.Add(new XRLabel
- {
- Text = title,
- StyleName = "Title",
- WidthF = (report.PageWidth - (report.Margins.Left + report.Margins.Right))
- });
- }
- private void InitDataSource(object model)
- {
- report.DataSource = model;
- }
- private void InitDetailsAndPageHeader(MVCxGridViewState gridViewState)
- {
- var groupedColumns = gridViewState.GroupedColumns;
- var pagewidth = (report.PageWidth - (report.Margins.Left + report.Margins.Right)) - groupedColumns.Count * subGroupOffset;
- var columns = GetColumnsInfo(gridViewState, pagewidth);
- if (CustomizeColumnsCollection != null)
- CustomizeColumnsCollection(report, new ColumnsCreationEventArgs(pagewidth)
- {
- ColumnsInfo = columns
- });
- report.Bands.Add(new DetailBand()
- {
- HeightF = bandHeight,
- KeepTogetherWithDetailReports = true
- });
- report.Bands.Add(new PageHeaderBand()
- {
- HeightF = bandHeight
- });
- var headerTable = new XRTable()
- {
- StyleName = "HeaderRow"
- };
- var row = new XRTableRow();
- var detailTable = new XRTable
- {
- OddStyleName = "OddRow",
- EvenStyleName = "EvenRow"
- };
- var row2 = new XRTableRow();
- for (var i = 0; i < columns.Count; i++)
- {
- if (columns[i].IsVisible)
- {
- var cell = new XRTableCell
- {
- Width = columns[i].ColumnWidth,
- Text = columns[i].GridViewColumn.Caption,
- Borders = DevExpress.XtraPrinting.BorderSide.All,
- TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleLeft
- };
- row.Cells.Add(cell);
- var cell2 = new XRTableCell
- {
- Width = columns[i].ColumnWidth
- };
- var cc = new ControlCustomizationEventArgs
- {
- FieldName = columns[i].FieldName,
- IsModified = false,
- Owner = cell2,
- Header = cell
- };
- if (CustomizeColumn != null)
- CustomizeColumn(report, cc);
- if (cc.IsModified == false)
- {
- cell2.DataBindings.Add("Text", null, columns[i].FieldName,
- "{0:" + columns[i].GridViewColumn.DisplayFormat + "}");
- }
- detailsInfo.Add(columns[i].GridViewColumn, cell2);
- row2.Cells.Add(cell2);
- detailsWidth.Add(columns[i].GridViewColumn, cell2.WidthF);
- }
- }
- headerTable.Rows.Add(row);
- headerTable.Width = pagewidth;
- headerTable.LocationF = new PointF(groupedColumns.Count * subGroupOffset, 0);
- headerTable.Borders = DevExpress.XtraPrinting.BorderSide.Bottom;
- detailTable.Rows.Add(row2);
- detailTable.LocationF = new PointF(groupedColumns.Count * subGroupOffset, 0);
- detailTable.Width = pagewidth;
- report.Bands[BandKind.PageHeader].Controls.Add(headerTable);
- report.Bands[BandKind.Detail].Controls.Add(detailTable);
- }
- private void InitDetailReports(MVCxGridViewState gridViewState)
- {
- var groupedColumns = gridViewState.GroupedColumns;
- var pagewidth = (report.PageWidth - (report.Margins.Left + report.Margins.Right)) - groupedColumns.Count * subGroupOffset;
- var columns = GetColumnsInfo(gridViewState, pagewidth);
- if (CustomizeColumnsCollection != null)
- CustomizeColumnsCollection(report, new ColumnsCreationEventArgs(pagewidth)
- {
- ColumnsInfo = columns
- });
- var detailColumns = columns
- .Where(c => c.IsDetail)
- .ToList();
- if (!detailColumns.Any()) return;
- var detailReportBand = new DetailReportBand()
- {
- HeightF = bandHeight
- };
- report.Bands.Add(detailReportBand);
- var detailReportTable = new XRTable
- {
- OddStyleName = "DetailRow",
- EvenStyleName = "DetailRow",
- WidthF = pagewidth
- };
- var detailReportRow = new XRTableRow();
- foreach (var detailColumn in detailColumns)
- {
- var cellDetail = new XRTableCell
- {
- WidthF = pagewidth / detailColumns.Count
- };
- var label = new XRLabel
- {
- StyleName = "DetailRowLabel",
- WidthF = pagewidth / detailColumns.Count,
- WordWrap = true
- };
- label.DataBindings.Add("Text", null, detailColumn.FieldName,
- "{0:" + detailColumn.GridViewColumn.DisplayFormat + "}");
- cellDetail.Controls.Add(label);
- detailReportRow.Cells.Add(cellDetail);
- }
- detailReportTable.Rows.Add(detailReportRow);
- detailReportTable.LocationF = new PointF(groupedColumns.Count * subGroupOffset, 0);
- DetailBand detailBand = new DetailBand();
- detailBand.Height = detailReportTable.Height;
- detailReportBand.Bands.Add(detailBand);
- detailBand.Controls.Add(detailReportTable);
- }
- private void InitSortings(MVCxGridViewState gridViewState)
- {
- var columns = gridViewState.Columns;
- var groupedColumns = gridViewState.GroupedColumns;
- for (var i = 0; i < columns.Count; i++)
- {
- if (!groupedColumns.Contains(columns[i]))
- {
- if (columns[i].SortOrder != ColumnSortOrder.None)
- ((DetailBand)report.Bands[BandKind.Detail]).SortFields.Add(new GroupField(columns[i].FieldName, columns[i].SortOrder == ColumnSortOrder.Ascending ? XRColumnSortOrder.Ascending : XRColumnSortOrder.Descending));
- }
- }
- }
- private void InitGroupHeaders(MVCxGridViewState gridViewState)
- {
- var groupedColumns = gridViewState.GroupedColumns;
- for (var i = groupedColumns.Count - 1; i >= 0; i--)
- {
- var groupedColumn = groupedColumns[i];
- var gb = new GroupHeaderBand
- {
- Height = bandHeight
- };
- var groupHeaderTable = new XRTable
- {
- StyleName = "GroupHeader",
- LocationF = new PointF(initialGroupOffset + i * 10, 0),
- SizeF = new SizeF((report.PageWidth - (report.Margins.Left + report.Margins.Right)) - (initialGroupOffset + i * subGroupOffset), bandHeight)
- };
- var groupHeaderRow = new XRTableRow();
- var l = new XRTableCell
- {
- Text = groupedColumn.Caption + ": [" + groupedColumn.FieldName + "]",
- Borders = DevExpress.XtraPrinting.BorderSide.Left |
- DevExpress.XtraPrinting.BorderSide.Top |
- DevExpress.XtraPrinting.BorderSide.Bottom
- };
- groupHeaderRow.Cells.Add(l);
- foreach (MVCxSummaryItemState item in gridViewState.TotalSummary)
- {
- var sum = new XRTableCell
- {
- Borders = DevExpress.XtraPrinting.BorderSide.Top |
- DevExpress.XtraPrinting.BorderSide.Bottom
- };
- sum.Summary = new XRSummary
- {
- Running = SummaryRunning.Group
- };
- sum.Summary.Func = GetSummaryFunc(item.SummaryType);
- sum.DataBindings.Add("Text", null, item.FieldName);
- groupHeaderRow.Cells.Add(sum);
- if (CustomizeGroupColumnSummary != null)
- CustomizeGroupColumnSummary(report, new ColumnSummaryCreationEventArgs(item.FieldName, sum.Summary));
- }
- groupHeaderTable.Rows.Add(groupHeaderRow);
- gb.Controls.Add(groupHeaderTable);
- gb.RepeatEveryPage = shouldRepeatGroupHeadersOnEveryPage;
- var gf = new GroupField(
- groupedColumn.FieldName, groupedColumn.SortOrder == ColumnSortOrder.Ascending
- ? XRColumnSortOrder.Ascending
- : XRColumnSortOrder.Descending);
- gb.GroupFields.Add(gf);
- report.Bands.Add(gb);
- }
- }
- private void InitFilters(MVCxGridViewState gridViewState)
- {
- report.FilterString = gridViewState.FilterExpression;
- }
- private void InitTotalSummaries(MVCxGridViewState gridViewState)
- {
- if (gridViewState.TotalSummary.Count == 0) return;
- report.Bands.Add(new ReportFooterBand
- {
- HeightF = bandHeight,
- StyleName = "SummaryRow"
- });
- foreach (MVCxSummaryItemState item in gridViewState.TotalSummary)
- {
- var col = gridViewState.Columns[item.ShowInColumn == string.Empty
- ? item.FieldName
- : item.ShowInColumn];
- if (col == null) continue;
- if (!detailsInfo.Contains(col)) continue;
- var label = new XRLabel
- {
- LocationF = new PointF(
- ((XRTableCell)detailsInfo[col]).LocationF.X + subGroupOffset * gridViewState.GroupedColumns.Count,
- ((XRTableCell)detailsInfo[col]).LocationF.Y),
- SizeF = ((XRTableCell)detailsInfo[col]).SizeF
- };
- label.DataBindings.Add("Text", null, col.FieldName);
- label.Summary = new XRSummary
- {
- Running = SummaryRunning.Report
- };
- label.Summary.FormatString = item.DisplayFormat;
- label.Summary.Func = GetSummaryFunc(item.SummaryType);
- report.Bands[BandKind.ReportFooter].Controls.Add(label);
- if (CustomizeTotalColumnSummary != null)
- CustomizeTotalColumnSummary(report, new ColumnSummaryCreationEventArgs(col.FieldName, label.Summary));
- }
- }
- private void InitPageFooter()
- {
- var userHelper = Singleton<IContainer>.Instance.Resolve<IUserHelper>();
- var user = userHelper.FromCookies();
- report.Bands.Add(new PageFooterBand
- {
- HeightF = 23f
- });
- var footerTable = new XRTable
- {
- WidthF = (report.PageWidth - (report.Margins.Left + report.Margins.Right)),
- HeightF = 23f,
- Borders = DevExpress.XtraPrinting.BorderSide.Top,
- StyleName = "FooterRow"
- };
- var footerRow = new XRTableRow
- {
- Cells =
- {
- new XRTableCell
- {
- TextAlignment = TextAlignment.MiddleLeft,
- Text = DateTime.Now.ToString("dd.MM.yyyy - HH:mm \"Uhr\" |"),
- WidthF = 145f,
- HeightF = 23f
- },
- new XRTableCell
- {
- Controls =
- {
- new XRPageInfo
- {
- TextAlignment = TextAlignment.MiddleLeft,
- Format = "Seite {0} von {1}",
- PageInfo = PageInfo.NumberOfTotal,
- StartPageNumber = 1,
- Padding = 5,
- HeightF = 23f
- }
- },
- WidthF = 100f,
- HeightF = 23f
- },
- new XRTableCell
- {
- TextAlignment = TextAlignment.MiddleRight,
- Text = String.Format("{0}, {1}", user.Lastname, user.Forename),
- WidthF = (report.PageWidth - (report.Margins.Left + report.Margins.Right)) - 230
- }
- }
- };
- footerTable.Rows.Add(footerRow);
- report.Bands[BandKind.PageFooter].Controls.Add(footerTable);
- }
- private List<MVCxColumnInfo> GetColumnsInfo(MVCxGridViewState gridViewState, int pagewidth)
- {
- var columns = new List<MVCxColumnInfo>();
- var visibleColumns = gridViewState.Columns;
- foreach (var dataColumn in visibleColumns)
- {
- MVCxColumnInfo column = new MVCxColumnInfo(dataColumn)
- {
- ColumnCaption = string.IsNullOrEmpty(dataColumn.Caption) ? dataColumn.FieldName : dataColumn.Caption,
- ColumnWidth = ((int)pagewidth / visibleColumns.Count),
- FieldName = dataColumn.FieldName,
- IsVisible = true
- };
- columns.Add(column);
- }
- return columns;
- }
- private SummaryFunc GetSummaryFunc(SummaryItemType summaryItemType)
- {
- switch (summaryItemType)
- {
- case SummaryItemType.Sum:
- return SummaryFunc.Sum;
- case SummaryItemType.Average:
- return SummaryFunc.Avg;
- case SummaryItemType.Max:
- return SummaryFunc.Max;
- case SummaryItemType.Min:
- return SummaryFunc.Min;
- case SummaryItemType.Count:
- return SummaryFunc.Count;
- default:
- return SummaryFunc.Custom;
- }
- }
- public void WritePdfToResponse(HttpResponseBase Response, string fileName, string type)
- {
- report.CreateDocument(false);
- using (MemoryStream ms = new MemoryStream())
- {
- report.ExportToPdf(ms);
- ms.Seek(0, SeekOrigin.Begin);
- WriteResponse(Response, ms.ToArray(), "application/pdf", type, fileName);
- }
- }
- public void WriteXlsToResponse(HttpResponseBase Response, string fileName, string type)
- {
- report.CreateDocument(false);
- using (MemoryStream ms = new MemoryStream())
- {
- report.ExportToXls(ms);
- ms.Seek(0, SeekOrigin.Begin);
- WriteResponse(Response, ms.ToArray(), "application/vnd.ms-excel", type, fileName);
- }
- }
- public void WriteXlsxToResponse(HttpResponseBase Response, string fileName, string type)
- {
- report.CreateDocument(false);
- using (MemoryStream ms = new MemoryStream())
- {
- report.ExportToXlsx(ms);
- ms.Seek(0, SeekOrigin.Begin);
- WriteResponse(Response, ms.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", type, fileName);
- }
- }
- public static void WriteResponse(HttpResponseBase response, byte[] filearray, string contentType, string type, string fileName)
- {
- response.ClearContent();
- response.Buffer = true;
- response.Cache.SetCacheability(HttpCacheability.Private);
- response.ContentType = contentType;
- ContentDisposition contentDisposition = new ContentDisposition();
- contentDisposition.FileName = fileName;
- contentDisposition.DispositionType = type;
- response.AddHeader("Content-Disposition", contentDisposition.ToString());
- response.BinaryWrite(filearray);
- HttpContext.Current.ApplicationInstance.CompleteRequest();
- try
- {
- response.End();
- }
- catch (System.Threading.ThreadAbortException)
- {
- }
- }
- }
- public class ControlCustomizationEventArgs : EventArgs
- {
- XRControl owner;
- public XRControl Owner
- {
- get
- {
- return owner;
- }
- set
- {
- owner = value;
- }
- }
- XRControl header;
- public XRControl Header
- {
- get
- {
- return header;
- }
- set
- {
- header = value;
- }
- }
- bool isModified;
- public bool IsModified
- {
- get
- {
- return isModified;
- }
- set
- {
- isModified = value;
- }
- }
- string fieldName;
- public string FieldName
- {
- get
- {
- return fieldName;
- }
- set
- {
- fieldName = value;
- }
- }
- }
- public class ColumnSummaryCreationEventArgs : EventArgs
- {
- public ColumnSummaryCreationEventArgs(string fieldName, XRSummary summary)
- {
- this.fieldName = fieldName;
- this.summary = summary;
- }
- XRSummary summary;
- public XRSummary Summary
- {
- get
- {
- return summary;
- }
- set
- {
- summary = value;
- }
- }
- string fieldName;
- public string FieldName
- {
- get
- {
- return fieldName;
- }
- set
- {
- fieldName = value;
- }
- }
- }
- public class ColumnsCreationEventArgs : EventArgs
- {
- int pageWidth;
- public int PageWidth
- {
- get
- {
- return pageWidth;
- }
- }
- public ColumnsCreationEventArgs(int pageWidth)
- {
- this.pageWidth = pageWidth;
- }
- List<MVCxColumnInfo> columnsInfo;
- public List<MVCxColumnInfo> ColumnsInfo
- {
- get
- {
- return columnsInfo;
- }
- set
- {
- columnsInfo = value;
- }
- }
- }
- public class MVCxColumnInfo
- {
- public MVCxColumnInfo(GridViewDataColumnState gridViewColumn)
- {
- this.gridViewColumn = gridViewColumn;
- }
- GridViewDataColumnState gridViewColumn;
- public GridViewDataColumnState GridViewColumn
- {
- get
- {
- return gridViewColumn;
- }
- }
- string columnCaption;
- public string ColumnCaption
- {
- get
- {
- return columnCaption;
- }
- set
- {
- columnCaption = value;
- }
- }
- string fieldName;
- public string FieldName
- {
- get
- {
- return fieldName;
- }
- set
- {
- fieldName = value;
- }
- }
- int columnWidth;
- public int ColumnWidth
- {
- get
- {
- return columnWidth;
- }
- set
- {
- columnWidth = value;
- }
- }
- bool isVisible;
- public bool IsVisible
- {
- get
- {
- return isVisible;
- }
- set
- {
- isVisible = value;
- }
- }
- bool isDetail;
- public bool IsDetail
- {
- get
- {
- return isDetail;
- }
- set
- {
- isDetail = value;
- }
- }
- }
- }
- }
|