SetoutTemplate.xaml.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Windows;
  6. using Comal.Classes;
  7. using InABox.Configuration;
  8. using InABox.Wpf;
  9. using Microsoft.Win32;
  10. using NPOI.SS.UserModel;
  11. using NPOI.SS.Util;
  12. using NPOI.XSSF.UserModel;
  13. namespace PRSDesktop
  14. {
  15. /// <summary>
  16. /// Interaction logic for SetoutTemplate.xaml
  17. /// </summary>
  18. public partial class SetoutTemplate : ThemableWindow
  19. {
  20. private readonly GlobalConfiguration<FactorySetup> config = new();
  21. private FactoryTemplate currentTemplate;
  22. private readonly FactorySetup settings;
  23. public SetoutTemplate()
  24. {
  25. InitializeComponent();
  26. settings = config.Load();
  27. Templates.Load(settings);
  28. Stages.SetStages(new List<StageTemplate>());
  29. //Attributes.Attributes = new List<FactoryTemplateAttribute>();
  30. //Attributes.Refresh(true, false);
  31. currentTemplate = null;
  32. }
  33. private void OKButton_Click(object sender, RoutedEventArgs e)
  34. {
  35. if (currentTemplate != null)
  36. {
  37. currentTemplate.Stages.Clear();
  38. currentTemplate.Stages.AddRange(Stages.GetStages<StageTemplate>());
  39. currentTemplate.Items.Clear();
  40. //currentTemplate.Attributes.AddRange(Items.GetItems<FactoryTemplateItem>());
  41. }
  42. settings.Templates = Templates.Templates;
  43. config.Save(settings);
  44. Close();
  45. }
  46. private void CancelButton_Click(object sender, RoutedEventArgs e)
  47. {
  48. Close();
  49. }
  50. private void Templates_TemplateSelected(object sender, TempateSelectedArgs e)
  51. {
  52. if (currentTemplate != null)
  53. {
  54. currentTemplate.Stages.Clear();
  55. currentTemplate.Stages.AddRange(Stages.GetStages<StageTemplate>());
  56. currentTemplate.Items.Clear();
  57. //currentTemplate.Items.AddRange(Items.GetItems<FactoryTemplateItem>());
  58. }
  59. currentTemplate = e.Template;
  60. if (e.Template != null) Stages.SetStages(e.Template.Stages);
  61. //Attributes.Attributes = e.Template.Attributes;
  62. //Attributes.Refresh(false,true);
  63. }
  64. private ICell EnsureCell(ISheet sheet, int row, int col, ICellStyle style)
  65. {
  66. var _row = sheet.GetRow(row);
  67. if (_row == null)
  68. _row = sheet.CreateRow(row);
  69. var _cell = _row.GetCell(col);
  70. if (_cell == null)
  71. _cell = _row.CreateCell(col);
  72. _cell.CellStyle = style;
  73. return _cell;
  74. }
  75. private ICellStyle CreateStyle(IWorkbook book, IndexedColors color)
  76. {
  77. var style = book.CreateCellStyle();
  78. style.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
  79. style.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
  80. style.BorderLeft = BorderStyle.Medium;
  81. style.BorderRight = BorderStyle.Medium;
  82. style.BorderTop = BorderStyle.Medium;
  83. style.BorderBottom = BorderStyle.Medium;
  84. style.FillForegroundColor = color.Index;
  85. style.FillPattern = FillPattern.SolidForeground;
  86. return style;
  87. }
  88. private void ExportButton_Click(object sender, RoutedEventArgs e)
  89. {
  90. if (currentTemplate != null)
  91. {
  92. currentTemplate.Stages.Clear();
  93. currentTemplate.Stages.AddRange(Stages.GetStages<StageTemplate>());
  94. currentTemplate.Items.Clear();
  95. //currentTemplate.Items.AddRange(Items.GetItems<FactoryTemplateItem>());
  96. }
  97. var dlg = new SaveFileDialog();
  98. dlg.DefaultExt = "xlsx";
  99. dlg.Filter = "Excel Files|*.xlsx";
  100. if (dlg.ShowDialog() != true)
  101. return;
  102. using (var stream = new FileStream(dlg.FileName, FileMode.Create, FileAccess.Write))
  103. {
  104. IWorkbook book = new XSSFWorkbook();
  105. var sheet = book.CreateSheet("Sheet1");
  106. var style = CreateStyle(book, IndexedColors.Grey50Percent);
  107. EnsureCell(sheet, 0, 0, style).SetCellValue("Code");
  108. EnsureCell(sheet, 0, 1, style).SetCellValue("Description");
  109. EnsureCell(sheet, 1, 0, style).SetCellValue("");
  110. EnsureCell(sheet, 1, 1, style).SetCellValue("");
  111. sheet.AddMergedRegion(new CellRangeAddress(0, 1, 0, 0));
  112. sheet.AddMergedRegion(new CellRangeAddress(0, 1, 1, 1));
  113. var sections = new List<Guid>();
  114. var config = new GlobalConfiguration<FactorySetup>();
  115. var settings = config.Load();
  116. var iGrp = 0;
  117. var sGrp = "";
  118. for (var i = 0; i < settings.Sections.Count; i++)
  119. {
  120. var section = settings.Sections[i];
  121. sections.Add(section.ID);
  122. if (!section.Group.Equals(sGrp))
  123. {
  124. if (!string.IsNullOrEmpty(sGrp))
  125. sheet.AddMergedRegion(new CellRangeAddress(0, 0, iGrp, i + 1));
  126. iGrp = i + 2;
  127. sGrp = section.Group;
  128. }
  129. EnsureCell(sheet, 0, i + 2, style).SetCellValue(section.Group);
  130. EnsureCell(sheet, 1, i + 2, style).SetCellValue(section.Name);
  131. }
  132. if (iGrp < settings.Sections.Count + 2)
  133. sheet.AddMergedRegion(new CellRangeAddress(0, 0, iGrp, settings.Sections.Count + 1));
  134. style = CreateStyle(book, IndexedColors.White);
  135. for (var j = 0; j < settings.Templates.Count; j++)
  136. {
  137. var template = settings.Templates[j];
  138. EnsureCell(sheet, j + 2, 0, style).SetCellValue(template.Code);
  139. EnsureCell(sheet, j + 2, 1, style).SetCellValue(template.Name);
  140. for (var k = 0; k < sections.Count; k++)
  141. EnsureCell(sheet, j + 2, k + 2, style).SetCellValue(" ");
  142. foreach (var stage in template.Stages)
  143. {
  144. var col = sections.IndexOf(stage.SectionID);
  145. EnsureCell(sheet, j + 2, col + 2, style).SetCellValue(stage.Minutes);
  146. }
  147. }
  148. for (var i = 0; i < settings.Sections.Count + 2; i++)
  149. sheet.AutoSizeColumn(i);
  150. book.Write(stream);
  151. }
  152. Process.Start(dlg.FileName);
  153. }
  154. }
  155. }