using DevExpress.XtraReports.UI; using iBemsDataService.Controllers; using System; using System.Collections.Generic; using System.Diagnostics; using System.Drawing; using System.Linq; using System.Net; using System.Reflection; using System.Web; namespace iBemsDataService.Util { public class ReportTableHandler { public static XRTable CreateTableFromEnergyWithWidth(List list, float[] columnWidth) { XRTable table = CreateTableFromEnergy(list); AdjustTextSizeEnergy(table, columnWidth); return table; } public static XRTable CreateTableFromEnergy(List list) { XRTable table = new XRTable(); table.LocationF = new PointF(0, 0); table.Font = new System.Drawing.Font("맑은 고딕", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); table.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleRight; List rowListtop = ConvertEnergyToTableRowstop(list); for (var i = 0; i < rowListtop.Count; i++) { table.Rows.Add(rowListtop[i]); } table.AdjustSize(); table.EndInit(); table.WidthF = 839f; return table; } private static List ConvertEnergyToTableRowstop(List list) { List rowList = new List(); String[] Text = { "TextResultDate", "TextResult" }; Type type = typeof(T); foreach (T item in list) { XRTableRow newRow = MakeXrTableRowTemplate(); foreach (PropertyInfo propertyInfo in type.GetProperties()) { var obj = propertyInfo.GetValue(item); String cellText = ""; bool colorchekck = false; if (propertyInfo.Name.ToString() == "Name") { if (obj != null) { cellText = (String)obj; if (cellText.IndexOf("합계") != -1) { colorchekck = true; } } } else if (propertyInfo.Name.ToString() == "Today") { if (obj != null) { cellText = obj.ToString(); } } else if (propertyInfo.Name.ToString() == "Daybefore") { if (obj != null) { cellText = obj.ToString(); } } else if (propertyInfo.Name.ToString() == "Dailyusage") { if (obj != null) { cellText = obj.ToString(); } } else { if (obj != null) { cellText = obj.ToString(); } } XRTableCell newcell = new XRTableCell(); newcell.Text = cellText; if (propertyInfo.Name.ToString() == "Name") { newcell.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter; } newRow.Cells.Add(newcell); if (colorchekck) { newRow.BackColor = Color.FromArgb(255, 255, 204); colorchekck = false; } } rowList.Add(newRow); } return rowList; } public static XRTable CreateTableFromListWithColumnWidth(List list, float[] columnWidth, String[] exceptions = null) { XRTable table = CreateTableFromList(list, exceptions); AdjustTextSize(table, columnWidth); return table; } public static XRTable CreateTableFromListWithColumnWidthPatrol(List list, float[] columnWidth, String[] exceptions = null) { XRTable table = CreateTableFromListPatrol(list, exceptions); AdjustTextSizePatrol(table, columnWidth); return table; } public static XRTable CreateTableFromList(List list, String[] exceptions = null) { XRTable table = new XRTable(); table.LocationF = new PointF(0, 0); table.Font = new System.Drawing.Font("맑은 고딕", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); table.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter; table.BeginInit(); List rowListtop = ConvertListToTableRowstop(list, exceptions); List rowListbottom = ConvertListToTableRowsbottom(list, exceptions); for (var i = 0; i < rowListtop.Count; i++) { table.Rows.Add(rowListtop[i]); table.Rows.Add(rowListbottom[i]); } for (var i = 1; i < rowListtop.Count * 2; i += 2) { table.Rows[i].TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleLeft; } table.AdjustSize(); table.EndInit(); table.WidthF = 839f; return table; } public static XRTable CreateTableFromListPatrol(List list, String[] exceptions = null) { XRTable table = new XRTable(); table.LocationF = new PointF(0, 0); table.Font = new System.Drawing.Font("맑은 고딕", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); table.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter; table.BeginInit(); List rowListtop = ConvertListToTableRowstop(list, exceptions); List rowListbottom = ConvertListToTableRowsbottom(list, exceptions); for (var i = 0; i < rowListtop.Count; i++) { table.Rows.Add(rowListtop[i]); //table.Rows.Add(rowListbottom[i]); } //for (var i = 1; i < rowListtop.Count; i += 2) //{ // table.Rows[i].TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleLeft; //} table.AdjustSize(); table.EndInit(); table.WidthF = 800f; return table; } public static XRTable CreateTableFromListWithColumnWidthMaterial(List list, float[] columnWidth, String[] exceptions = null) { XRTable table = CreateTableFromListMaterial(list, exceptions); AdjustTextSize(table, columnWidth); return table; } public static XRTable CreateTableFromListMaterial(List list, String[] exceptions = null) { XRTable table = new XRTable(); table.LocationF = new PointF(0, 0); table.Font = new System.Drawing.Font("맑은 고딕", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); table.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter; table.BeginInit(); List rowListtop = ConvertListToTableRowstop(list, exceptions); for (var i = 0; i < rowListtop.Count; i++) { table.Rows.Add(rowListtop[i]); } table.AdjustSize(); table.EndInit(); table.WidthF = 839f; return table; } private static void AdjustTextSize(XRTable table, float[] columnWidth) { float[] columnWidthbottom = { 230f, 609f, 839f }; foreach (XRTableRow row in table.Rows) { for (int i = 0; i < row.Cells.Count; i++) { if (row.Cells.Count > 2) { XRTableCell cell = row.Cells[i]; if (columnWidth != null) { cell.SizeF = new SizeF(columnWidth[i], cell.SizeF.Height); } } else { XRTableCell cell = row.Cells[i]; if (columnWidth != null) { cell.SizeF = new SizeF(columnWidthbottom[2], cell.SizeF.Height); } } } } } private static void AdjustTextSizePatrol(XRTable table, float[] columnWidth) { float[] columnWidthbottom = { 230f, 609f, 800f }; foreach (XRTableRow row in table.Rows) { for (int i = 0; i < row.Cells.Count; i++) { if (row.Cells.Count > 2) { XRTableCell cell = row.Cells[i]; if (columnWidth != null) { cell.SizeF = new SizeF(columnWidth[i], cell.SizeF.Height); } } else { XRTableCell cell = row.Cells[i]; if (columnWidth != null) { cell.SizeF = new SizeF(columnWidthbottom[2], cell.SizeF.Height); } } } } } private static void AdjustTextSizeEnergy(XRTable table, float[] columnWidth) { foreach (XRTableRow row in table.Rows) { for (int i = 0; i < row.Cells.Count; i++) { XRTableCell cell = row.Cells[i]; if (columnWidth != null) { cell.SizeF = new SizeF(columnWidth[i], cell.SizeF.Height); } } } } private static List ConvertListToTableRowstop(List list, String[] exceptions) { List rowList = new List(); Type type = typeof(T); String[] Text = { "TextResultDate", "TextResult" }; foreach (T item in list) { XRTableRow newRow = MakeXrTableRowTemplate(); foreach (PropertyInfo propertyInfo in type.GetProperties()) { if (exceptions != null && exceptions.Contains(propertyInfo.Name)) continue; if (Text.Contains(propertyInfo.Name)) continue; var obj = propertyInfo.GetValue(item); String cellText = null; if (obj != null) { cellText = obj.ToString(); } XRTableCell newCell = new XRTableCell(); newCell.Text = cellText; newRow.Cells.Add(newCell); } rowList.Add(newRow); } return rowList; } private static List ConvertListToTableRowsbottom(List list, String[] exceptions) { List rowList = new List(); Type type = typeof(T); String[] Text = { "TextResultDate", "TextResult" }; foreach (T item in list) { XRTableRow newRow = MakeXrTableRowTemplate(); //String cellText = null; String[] temptext = new string[1]; foreach (PropertyInfo propertyInfo in type.GetProperties()) { if (exceptions != null && exceptions.Contains(propertyInfo.Name)) continue; if (!Text.Contains(propertyInfo.Name)) continue; var obj = propertyInfo.GetValue(item); if (propertyInfo.Name.ToString() == "TextResultDate") { if (obj != null) { DateTime[] TextResultDate = (DateTime[])obj; temptext = new string[TextResultDate.Count()]; } else { temptext = null; } } if (propertyInfo.Name.ToString() == "TextResultDate") { DateTime[] TextResultDate = (DateTime[])obj; if (TextResultDate != null) { for (var i = 0; i < TextResultDate.Count(); i++) { temptext[i] = " " + TextResultDate[i].ToString("yyyy-MM-dd"); } } else { //for (var i = 0; i < TextResultDate.Count(); i++) //{ // temptext[i] = ""; //} } } else if (propertyInfo.Name.ToString() == "TextResult") { String[] TextResult = (String[])obj; //var seven = 50; if (TextResult != null) { for (var i = 0; i < TextResult.Count(); i++) { //var result = TextResult[i].Replace("\n", " "); //.replace(/\n/gi, '

      '); //temptext[i] += " - " + result + "\r\n"; var result = TextResult[i].Replace("\n", "\n "); result = result.Replace("?-", " -"); temptext[i] += " - " + result + "\r\n"; //if (temptext[i].Length > seven) //{ // temptext[i] = factorial(temptext[i]); //} } } else { //for (var i = 0; i < TextResult.Count(); i++) //{ // temptext[i] += ""; //} } } } XRTableCell newCell = new XRTableCell(); string finaltempstring = ""; if (temptext != null) { for (var i = 0; i < temptext.Length; i++) { finaltempstring += temptext[i]; } } else { finaltempstring = ""; } newCell.Text = finaltempstring; newCell.Multiline = true; newRow.Cells.Add(newCell); rowList.Add(newRow); } return rowList; } public static string factorial(string temp) { var seven = 50; var forcnt = temp.Length / seven; //몫 var forcnt1 = temp.Length % seven; //나머지 if (forcnt == 0) { return temp; } else if (forcnt != 0 && forcnt1 != 0) { return temp.Substring(0, seven) + "\r\n" + factorial(temp.Substring(seven)); } else { return temp.Substring(0, seven); } } private static XRTableRow MakeXrTableRowTemplate() { XRTableRow newRow = new XRTableRow(); newRow.BorderColor = Color.Black; newRow.BorderWidth = 1; newRow.Borders = DevExpress.XtraPrinting.BorderSide.All; newRow.HeightF = 30f; return newRow; } } }