123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Windows;
- using Comal.Classes;
- using InABox.Configuration;
- using InABox.Wpf;
- using Microsoft.Win32;
- using NPOI.SS.UserModel;
- using NPOI.SS.Util;
- using NPOI.XSSF.UserModel;
- namespace PRSDesktop
- {
- /// <summary>
- /// Interaction logic for SetoutTemplate.xaml
- /// </summary>
- public partial class SetoutTemplate : ThemableWindow
- {
- private readonly GlobalConfiguration<FactorySetup> config = new();
- private FactoryTemplate currentTemplate;
- private readonly FactorySetup settings;
- public SetoutTemplate()
- {
- InitializeComponent();
- settings = config.Load();
- Templates.Load(settings);
- Stages.SetStages(new List<StageTemplate>());
- //Attributes.Attributes = new List<FactoryTemplateAttribute>();
- //Attributes.Refresh(true, false);
- currentTemplate = null;
- }
- private void OKButton_Click(object sender, RoutedEventArgs e)
- {
- if (currentTemplate != null)
- {
- currentTemplate.Stages.Clear();
- currentTemplate.Stages.AddRange(Stages.GetStages<StageTemplate>());
- currentTemplate.Items.Clear();
- //currentTemplate.Attributes.AddRange(Items.GetItems<FactoryTemplateItem>());
- }
- settings.Templates = Templates.Templates;
- config.Save(settings);
- Close();
- }
- private void CancelButton_Click(object sender, RoutedEventArgs e)
- {
- Close();
- }
- private void Templates_TemplateSelected(object sender, TempateSelectedArgs e)
- {
- if (currentTemplate != null)
- {
- currentTemplate.Stages.Clear();
- currentTemplate.Stages.AddRange(Stages.GetStages<StageTemplate>());
- currentTemplate.Items.Clear();
- //currentTemplate.Items.AddRange(Items.GetItems<FactoryTemplateItem>());
- }
- currentTemplate = e.Template;
- if (e.Template != null) Stages.SetStages(e.Template.Stages);
- //Attributes.Attributes = e.Template.Attributes;
- //Attributes.Refresh(false,true);
- }
- private ICell EnsureCell(ISheet sheet, int row, int col, ICellStyle style)
- {
- var _row = sheet.GetRow(row);
- if (_row == null)
- _row = sheet.CreateRow(row);
- var _cell = _row.GetCell(col);
- if (_cell == null)
- _cell = _row.CreateCell(col);
- _cell.CellStyle = style;
- return _cell;
- }
- private ICellStyle CreateStyle(IWorkbook book, IndexedColors color)
- {
- var style = book.CreateCellStyle();
- style.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
- style.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
- style.BorderLeft = BorderStyle.Medium;
- style.BorderRight = BorderStyle.Medium;
- style.BorderTop = BorderStyle.Medium;
- style.BorderBottom = BorderStyle.Medium;
- style.FillForegroundColor = color.Index;
- style.FillPattern = FillPattern.SolidForeground;
- return style;
- }
- private void ExportButton_Click(object sender, RoutedEventArgs e)
- {
- if (currentTemplate != null)
- {
- currentTemplate.Stages.Clear();
- currentTemplate.Stages.AddRange(Stages.GetStages<StageTemplate>());
- currentTemplate.Items.Clear();
- //currentTemplate.Items.AddRange(Items.GetItems<FactoryTemplateItem>());
- }
- var dlg = new SaveFileDialog();
- dlg.DefaultExt = "xlsx";
- dlg.Filter = "Excel Files|*.xlsx";
- if (dlg.ShowDialog() != true)
- return;
- using (var stream = new FileStream(dlg.FileName, FileMode.Create, FileAccess.Write))
- {
- IWorkbook book = new XSSFWorkbook();
- var sheet = book.CreateSheet("Sheet1");
- var style = CreateStyle(book, IndexedColors.Grey50Percent);
- EnsureCell(sheet, 0, 0, style).SetCellValue("Code");
- EnsureCell(sheet, 0, 1, style).SetCellValue("Description");
- EnsureCell(sheet, 1, 0, style).SetCellValue("");
- EnsureCell(sheet, 1, 1, style).SetCellValue("");
- sheet.AddMergedRegion(new CellRangeAddress(0, 1, 0, 0));
- sheet.AddMergedRegion(new CellRangeAddress(0, 1, 1, 1));
- var sections = new List<Guid>();
- var config = new GlobalConfiguration<FactorySetup>();
- var settings = config.Load();
- var iGrp = 0;
- var sGrp = "";
- for (var i = 0; i < settings.Sections.Count; i++)
- {
- var section = settings.Sections[i];
- sections.Add(section.ID);
- if (!section.Group.Equals(sGrp))
- {
- if (!string.IsNullOrEmpty(sGrp))
- sheet.AddMergedRegion(new CellRangeAddress(0, 0, iGrp, i + 1));
- iGrp = i + 2;
- sGrp = section.Group;
- }
- EnsureCell(sheet, 0, i + 2, style).SetCellValue(section.Group);
- EnsureCell(sheet, 1, i + 2, style).SetCellValue(section.Name);
- }
- if (iGrp < settings.Sections.Count + 2)
- sheet.AddMergedRegion(new CellRangeAddress(0, 0, iGrp, settings.Sections.Count + 1));
- style = CreateStyle(book, IndexedColors.White);
- for (var j = 0; j < settings.Templates.Count; j++)
- {
- var template = settings.Templates[j];
- EnsureCell(sheet, j + 2, 0, style).SetCellValue(template.Code);
- EnsureCell(sheet, j + 2, 1, style).SetCellValue(template.Name);
- for (var k = 0; k < sections.Count; k++)
- EnsureCell(sheet, j + 2, k + 2, style).SetCellValue(" ");
- foreach (var stage in template.Stages)
- {
- var col = sections.IndexOf(stage.SectionID);
- EnsureCell(sheet, j + 2, col + 2, style).SetCellValue(stage.Minutes);
- }
- }
- for (var i = 0; i < settings.Sections.Count + 2; i++)
- sheet.AutoSizeColumn(i);
- book.Write(stream);
- }
- Process.Start(dlg.FileName);
- }
- }
- }
|