Advertisement
Guest User

Untitled

a guest
Aug 14th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.91 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using AccountingCounters2.Database;
  9. using AccountingСounters.Database;
  10. using ClosedXML.Excel;
  11. using Data2.Enums;
  12. using Data2.Extensions;
  13. using Data2.Models;
  14.  
  15. namespace AccountingCounters2.Helpers.ExcelHelpers
  16. {
  17.     public static class IndicationsReportGenerator
  18.     {
  19.         public static void GenerateReport(FileStream fileStream, MaintainedFacility Facility)
  20.         {
  21.             var workbook = new XLWorkbook(XLEventTracking.Disabled);
  22.             var worksheet = workbook.AddWorksheet("Indications");
  23.  
  24.             worksheet.Protect("132") // On this sheet we will only allow:
  25.                 .SetFormatCells() // Cell Formatting
  26.                 .SetInsertColumns() // Inserting Columns
  27.                 .SetDeleteColumns() // Deleting Columns
  28.                 .SetDeleteRows(); // Deleting Rows
  29.             worksheet.Style.Alignment.SetHorizontal(XLAlignmentHorizontalValues.Left);
  30.             worksheet.Style.Font.FontFamilyNumbering = XLFontFamilyNumberingValues.Roman;
  31.             worksheet.Style.Font.FontSize = 11;
  32.             worksheet.Style.Fill.BackgroundColor = XLColor.White;
  33.  
  34.             worksheet.Column(1).Style.Font.FontColor = XLColor.White;
  35.             var cell = worksheet.Cell(1, 2);
  36.             var range = worksheet.Range("B1:E1");
  37.             var row = worksheet.Row(1);
  38.             worksheet.Cell(1,1).Value = Facility.ID;
  39.  
  40.             //Загловок
  41.             range.Merge();
  42.             cell.Value = "ПОКАЗАНИЯ ПРИБОРОВ УЧЕТА";
  43.             cell.Style.Font.FontSize = 16;
  44.             cell.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
  45.             range.Style.Border.BottomBorder = XLBorderStyleValues.Double;
  46.  
  47.             //Название объекта
  48.             row = row.RowBelow();
  49.             cell = row.Cell(2);
  50.             range = worksheet.Range(2, 2, 2, 3);
  51.             range.Merge();
  52.             cell.Value = "Наименование объекта:";
  53.             range = worksheet.Range(row.Cell(2), row.Cell(5));
  54.             range.Style.Border.BottomBorder = XLBorderStyleValues.Thin;
  55.             cell.Style.Font.FontSize = 12;
  56.             cell = row.Cell(4);
  57.             cell.Value = Facility.Name;
  58.  
  59.             //Адрес объекта
  60.             row = row.RowBelow();
  61.             cell = row.Cell(2);
  62.             cell.Value = "Адрес:";
  63.             cell.Style.Font.FontSize = 12;
  64.             cell = row.Cell(4);
  65.             cell.Value = Facility.Address;
  66.             range = worksheet.Range(row.Cell(2), row.Cell(5));
  67.             range.Style.Border.BottomBorder = XLBorderStyleValues.Thin;
  68.  
  69.             //Дата
  70.             row = row.RowBelow();
  71.             range = worksheet.Range(row.Cell(2), row.Cell(3));
  72.             range.Merge();
  73.             cell = row.Cell(2);
  74.             cell.Value = "Показания приборов за:";
  75.             cell.Style.Font.FontSize = 12;
  76.             cell = row.Cell(4);
  77.             cell.Value = DateTime.Now.Date;
  78.             cell.Style.Font.FontSize = 12;
  79.             //cell.Style.NumberFormat.NumberFormatId = 14;
  80.             cell.Style.NumberFormat.Format = "mmmm yyyy";
  81.             cell.UnlockCell();
  82.             range = worksheet.Range(row.Cell(2), row.Cell(5));
  83.             range.Style.Border.BottomBorder = XLBorderStyleValues.Double;
  84.  
  85.             //Шапка таблицы
  86.             row = row.RowBelow().RowBelow();
  87.             row.Height = 30;
  88.             range = worksheet.Range(row.Cell(2), row.Cell(5));
  89.             range.Style.Fill.BackgroundColor = XLColor.Gray;
  90.             range.SetAllBorders(XLBorderStyleValues.Thin);
  91.             //range.Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
  92.             //range.Style.Border.InsideBorder = XLBorderStyleValues.Thin;
  93.             range.Style.Font.FontColor = XLColor.White;
  94.             row.Cell(2).Value = "ТИП УЗЛА";
  95.             row.Cell(3).Value = "МОДЕЛЬ ПРИБОРА";
  96.             row.Cell(4).Value = "НОМЕР";
  97.             row.Cell(5).Value = "ПОКАЗАНИЯ";
  98.             range.Style.Alignment.SetHorizontal(XLAlignmentHorizontalValues.Center);
  99.             range.Style.Alignment.SetVertical(XLAlignmentVerticalValues.Center);
  100.             //int startingrow = row.RowNumber();
  101.  
  102.             if (!ContextFactory.Instance.Entry(Facility).Collection(a => a.AccountingNodes).IsLoaded)
  103.             {
  104.                 ContextFactory.Instance.Entry(Facility).Collection(a => a.AccountingNodes).Load();
  105.             }
  106.             foreach (AccountingNode node in Facility.AccountingNodes.Where(n => n.DeviceGroupID != null))
  107.             {
  108.                 if (!ContextFactory.Instance.Entry(node).Reference(a => a.DeviceGroup).IsLoaded)
  109.                 {
  110.                     ContextFactory.Instance.Entry(node).Reference(a => a.DeviceGroup).Load();
  111.                 }
  112.                 row = worksheet.LastRowUsed().RowBelow();
  113.                 row.Cell(1).InsertData(GetNodeData(node));
  114.                 range = worksheet.Range(row.Cell(2), worksheet.LastRowUsed().Cell(5));
  115.                 range.CenterText();
  116.                 range.SetAllBorders(XLBorderStyleValues.Thin);
  117.                 switch (node.DeviceGroup.DeviceGroupType)
  118.                 {
  119.                     case DeviceGroupType.ColdWater:
  120.                     case DeviceGroupType.HotWater:
  121.                     case DeviceGroupType.Electricity:
  122.                         row.Cell(5).UnlockCell();
  123.                         break;
  124.                     case DeviceGroupType.HotWaterCirculation:
  125.                         row.Cell(5).UnlockCell();
  126.                         row.RowBelow().Cell(5).UnlockCell();
  127.                         break;
  128.                     case DeviceGroupType.HotWaterDigital:
  129.                         row.RowBelow().Cell(5).UnlockCell();
  130.                         row.RowBelow().Cell(4).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;
  131.                         row.RowBelow().RowBelow().Cell(5).UnlockCell();
  132.                         row.RowBelow().RowBelow().Cell(4).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;
  133.                         break;
  134.                     case DeviceGroupType.HeatEnergy:
  135.                     case DeviceGroupType.HotWaterDigitalCirculation:
  136.                         row.RowBelow().Cell(5).UnlockCell();
  137.                         row.RowBelow().Cell(4).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;
  138.                         row.RowBelow().RowBelow().Cell(5).UnlockCell();
  139.                         row.RowBelow().RowBelow().Cell(4).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;
  140.                         row.RowBelow().RowBelow().RowBelow().Cell(5).UnlockCell();
  141.                         row.RowBelow().RowBelow().RowBelow().Cell(4).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;
  142.                         break;
  143.                 }
  144.                 range = worksheet.Range(worksheet.LastRowUsed().Cell(2), worksheet.LastRowUsed().Cell(5));
  145.                 range.Style.Border.BottomBorder = XLBorderStyleValues.Medium;
  146.             }
  147.  
  148.             //Дата подачи ФИО подавшего показания
  149.             row = worksheet.LastRowUsed().RowBelow().RowBelow();
  150.             cell = row.Cell(2);
  151.             cell.Value = "дата подачи:";
  152.             cell.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;
  153.             cell = row.Cell(3);
  154.             cell.Style.NumberFormat.NumberFormatId = 14;
  155.             cell.Value = DateTime.Now.Date;
  156.             cell.UnlockCell();
  157.             cell = row.Cell(4);
  158.             cell.Value = "сведения подал Ф.И.О.";
  159.             cell.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;
  160.             cell = row.Cell(5);
  161.             cell.UnlockCell();
  162.  
  163.             //Окончательное форматирование
  164.             worksheet.Rows(1, worksheet.LastRowUsed().RowNumber()).Height = 21;
  165.             worksheet.Rows(1, worksheet.LastRowUsed().RowNumber()).Style.Alignment.SetVertical(XLAlignmentVerticalValues.Center);
  166.             worksheet.Column(1).Width = 2;
  167.             worksheet.Column(1).Style.Alignment.SetHorizontal(XLAlignmentHorizontalValues.Right);
  168.             worksheet.Column(2).Width = 12;
  169.             worksheet.Column(3).Width = 18;
  170.             worksheet.Columns(4, 5).Width = 25;
  171.             //range = worksheet.Range(startingrow, 2, worksheet.LastRowUsed().RowNumber(), worksheet.LastColumnUsed().ColumnNumber());
  172.             //range.SetAllBorders(XLBorderStyleValues.Thin);
  173.  
  174.             workbook.SaveAs(fileStream);
  175.         }
  176.  
  177.         public static DataTable GetNodeData(AccountingNode node)
  178.         {
  179.             DataTable table = new DataTable();
  180.             table.Columns.Add("ID", typeof(string));
  181.             table.Columns.Add("ТИП УЗЛА", typeof(string));
  182.             table.Columns.Add("МОДЕЛЬ ПРИБОРА", typeof(string));
  183.             table.Columns.Add("НОМЕР", typeof(string));
  184.             table.Columns.Add("ПОКАЗАНИЯ", typeof(int));
  185.             ConsumptionCounter Device;
  186.             ConsumptionCounter Device2;
  187.             var number = "";
  188.             switch (node.DeviceGroup.DeviceGroupType)
  189.             {
  190.                     //Device = node.DeviceGroup.ConsumptionCounters.Single(c => c.Type == CounterType.ComputationalModule);
  191.                     //table.Rows.Add(Device.ID, node.TypeShortName, Device.Model, Device.Number);
  192.                     //table.Rows.Add(Device.ID, "", "", "Q, Гкл:");
  193.                     //table.Rows.Add(Device.ID, "", "", "V1, куб.м");
  194.                     //table.Rows.Add(Device.ID, "", "", "V2, куб.м");
  195.                     //break;
  196.                 case DeviceGroupType.ColdWater:
  197.                     Device = node.DeviceGroup.ConsumptionCounters.Single(c => c.Type == CounterType.ColdWater);
  198.                     table.Rows.Add(node.DeviceGroupID, node.TypeShortName, Device.Model, Device.Number);
  199.                     break;
  200.                 case DeviceGroupType.HotWater:
  201.                     Device = node.DeviceGroup.ConsumptionCounters.Single(c => c.Type == CounterType.HotWater);
  202.                     table.Rows.Add(node.DeviceGroupID, node.TypeShortName, Device.Model, Device.Number);
  203.                     break;
  204.                 case DeviceGroupType.HotWaterCirculation:
  205.                     Device = node.DeviceGroup.ConsumptionCounters.Single(c => c.Type == CounterType.HotWater && c.SubType == CounterSubType.Suply);
  206.                     table.Rows.Add(node.DeviceGroupID, node.TypeShortName, Device.Model, Device.Number);
  207.                     Device2 = node.DeviceGroup.ConsumptionCounters.Single(c => c.Type == CounterType.HotWater && c.SubType == CounterSubType.Return);
  208.                     table.Rows.Add(node.DeviceGroupID, node.TypeShortName, Device2.Model, Device2.Number);
  209.                     break;
  210.                 case DeviceGroupType.HotWaterDigital:
  211.                     //Device = node.DeviceGroup.ConsumptionCounters.Single(c => c.Type == CounterType.ComputationalModule);
  212.                     Device = node.DeviceGroup.ComputationalModule;
  213.                     number = node.DeviceGroup.ComputationalModuleInput != string.Empty ? $"{Device.Number}-{node.DeviceGroup.ComputationalModuleInput}" : Device.Number;
  214.                     table.Rows.Add(node.DeviceGroupID, node.TypeShortName, Device.Model, number);
  215.                     //table.Rows.Add(node.DeviceGroupID, node.TypeShortName, Device.Model, Device.Number);
  216.                     table.Rows.Add(node.DeviceGroupID, "", "", "Q, Гкл:");
  217.                     table.Rows.Add(node.DeviceGroupID, "", "", "V, куб.м");
  218.                     break;
  219.                 case DeviceGroupType.HeatEnergy:
  220.                     //Device = node.DeviceGroup.ConsumptionCounters.Single(c => c.Type == CounterType.ComputationalModule);
  221.                     Device = node.DeviceGroup.ComputationalModule;
  222.                     number = node.DeviceGroup.ComputationalModuleInput != string.Empty ? $"{Device.Number}-{node.DeviceGroup.ComputationalModuleInput}" : Device.Number;
  223.                     table.Rows.Add(node.DeviceGroupID, node.TypeShortName, Device.Model, number);
  224.                     //table.Rows.Add(node.DeviceGroupID, node.TypeShortName, Device.Model, Device.Number);
  225.                     table.Rows.Add(node.DeviceGroupID, "", "", "Q, Гкл:");
  226.                     table.Rows.Add(node.DeviceGroupID, "", "", "M1, куб.м");
  227.                     table.Rows.Add(node.DeviceGroupID, "", "", "M2, куб.м");
  228.                     break;
  229.                 case DeviceGroupType.HotWaterDigitalCirculation:
  230.                     //Device = node.DeviceGroup.ConsumptionCounters.Single(c => c.Type == CounterType.ComputationalModule);
  231.                     Device = node.DeviceGroup.ComputationalModule;
  232.                     number = node.DeviceGroup.ComputationalModuleInput != string.Empty ? $"{Device.Number}-{node.DeviceGroup.ComputationalModuleInput}" : Device.Number;
  233.                     table.Rows.Add(node.DeviceGroupID, node.TypeShortName, Device.Model, number);
  234.                     //table.Rows.Add(node.DeviceGroupID, node.TypeShortName, Device.Model, Device.Number);
  235.                     table.Rows.Add(node.DeviceGroupID, "", "", "Q, Гкл:");
  236.                     table.Rows.Add(node.DeviceGroupID, "", "", "V1, куб.м");
  237.                     table.Rows.Add(node.DeviceGroupID, "", "", "V2, куб.м");
  238.                     break;
  239.                 case DeviceGroupType.Electricity:
  240.                     Device = node.DeviceGroup.ConsumptionCounters.Single(c => c.Type == CounterType.Electricity);
  241.                     table.Rows.Add(node.DeviceGroupID, node.TypeShortName, Device.Model, Device.Number);
  242.                     break;
  243.                 default:
  244.                     throw new ArgumentOutOfRangeException();
  245.             }
  246.  
  247.             return table;
  248.         }
  249.     }
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement