/// <summary>СОБЫТИЕ Нажали на кнопку "Загрузка шаблона из Excel"</summary> protected virtual void PRI_ButtonFromExcel_1_Click(object sender, RoutedEventArgs e) { DataRowView _DataRowView = (DataRowView)PART_DataGrid.SelectedItem; int _CodShablon = Convert.ToInt16(_DataRowView.Row["Cod"]); string _TipDoc = PRI_ComboBox_1.SelectedValue.ToString(); eTipDocum _eTip = eTipDocum.Null; switch (_TipDoc) { case "apaN": _eTip = eTipDocum.Pol; break; case "ast": _eTip = eTipDocum.Stac; break; case "par": _eTip = eTipDocum.Paracl; break; case "kdl": _eTip = eTipDocum.Kdl; break; } var _TipProtokol = new MyTipProtokol(_eTip); string _NameFile = $"{_TipDoc}_{_CodShablon}"; string _PathFile = $@"C:\Shablons\{_NameFile}.xlsx"; Excel.Workbook _WorkBook; Excel.Application _ExcelApp; _ExcelApp = new Excel.Application(); FileInfo _FileInfo = new FileInfo(_PathFile); // Если нашли файл с текущим шаблоном if (!_FileInfo.Exists) { MessageBox.Show($"Не найден файл: {_PathFile}", "Алё, чо грузить то?!"); return; } _WorkBook = _ExcelApp.Workbooks.Open(_PathFile); Excel.Worksheet _Sheet = (Excel.Worksheet)_WorkBook.Worksheets.Item[1]; // Заполняем данные List <UserShablon> _ListShablons = new List <UserShablon>(); int _y = 2; int _x = 1; try { while (_Sheet.Cells[_y, 1].Value2 is double) { _x = 1; UserShablon _Shablon = new UserShablon(); _Shablon.PROP_Cod = (int)_Sheet.Cells[_y, _x++].Value2; _Shablon.PROP_ID = (int)_Sheet.Cells[_y, _x++].Value2; _Shablon.PROP_Nomer = (byte)_Sheet.Cells[_y, _x++].Value2; _Shablon.PROP_VarId = (int)_Sheet.Cells[_y, _x++].Value2; _Shablon.PROP_Maska = _Sheet.Cells[_y, _x++].Text ?? ""; _Shablon.PROP_Type = (byte)_Sheet.Cells[_y, _x++].Value2; _Shablon.PROP_Razdel = _Sheet.Cells[_y, _x++].Value2 ?? ""; _Shablon.PROP_Name = _Sheet.Cells[_y, _x++].Value2 ?? ""; _Shablon.PROP_ValueStart = _Sheet.Cells[_y, _x++].Value2 ?? ""; _Shablon.PROP_OutText = _Sheet.Cells[_y, _x++].Value2 ?? ""; _Shablon.PROP_InText = _Sheet.Cells[_y, _x++].Value2 ?? ""; _Shablon.PROP_xFormat = _Sheet.Cells[_y, _x++].Value2 ?? ""; _Shablon.PROP_xLua = _Sheet.Cells[_y, _x++].Value2 ?? ""; _Shablon.PROP_xInfo = _Sheet.Cells[_y, _x].Value2 ?? ""; _Shablon.PROP_TipProtokol = _TipProtokol; _Shablon.PROP_FlagEdit = false; _y++; _ListShablons.Add(_Shablon); } } catch (Exception) { _WorkBook.Close(); _ExcelApp.Quit(); MessageBox.Show($"Ошибка загрузки в строке: {_y}, в столбце: {--_x}", "Ошибка"); return; } UserShablon.MET_FactoryListShablon(_eTip, _CodShablon); int _Remov = ((VirtualModul)MyGlo.Modul).PUB_Shablon.RemoveAll(p => p.PROP_ID == _CodShablon); ((VirtualModul)MyGlo.Modul).PUB_Shablon.AddRange(_ListShablons); _WorkBook.Close(); _ExcelApp.Quit(); // Если загружаем в SQL if (PRI_CheckBox_1.IsChecked == true) { UserShablon.MET_SaveExcelToSQL(_eTip, _CodShablon); MessageBox.Show($"Удалено: {_Remov} строк. Загружено {_ListShablons.Count} строк!", "Загружено в SQL"); } else { MessageBox.Show($"Удалено: {_Remov} строк. Загружено {_ListShablons.Count} строк!", "Загружено в Память"); } }
/// <summary>СОБЫТИЕ Нажали на кнопку "Выгрузка шаблона в Excel"</summary> protected virtual void PRI_ButtonToExcel_1_Click(object sender, RoutedEventArgs e) { DataRowView _DataRowView = (DataRowView)PART_DataGrid.SelectedItem; int _CodShablon = Convert.ToInt16(_DataRowView.Row["Cod"]); string _TipDoc = PRI_ComboBox_1.SelectedValue.ToString(); eTipDocum _eTip = eTipDocum.Null; switch (_TipDoc) { case "apaN": _eTip = eTipDocum.Pol; break; case "ast": _eTip = eTipDocum.Stac; break; case "par": _eTip = eTipDocum.Paracl; break; case "kdl": _eTip = eTipDocum.Kdl; break; } string _NameFile = $"{_TipDoc}_{_CodShablon}"; string _PathFile = $@"C:\Shablons\{_NameFile}.xlsx"; Excel.Workbook _WorkBook; Excel.Application _ExcelApp; Process[] _Processes = Process.GetProcesses(); // Выбираем только наши IEnumerable <Process> _ProcsBazis = _Processes.Where(p => p.ProcessName == "EXCEL" && p.MainWindowTitle == $"{_NameFile}.xlsx - Excel"); foreach (var _Process in _ProcsBazis) { // Активирует окно процесса SetForegroundWindow(_Process.MainWindowHandle); // Отображает данное окно на перед, даже если было свернуто ShowWindow(_Process.MainWindowHandle, 1); return; } _ExcelApp = new Excel.Application(); bool _FileNew; FileInfo _FileInfo = new FileInfo(_PathFile); // Если нашли файл с текущим шаблоном if (_FileInfo.Exists) { _ExcelApp.Workbooks.Open(_PathFile); _FileNew = false; } else { _ExcelApp.SheetsInNewWorkbook = 1; _ExcelApp.Workbooks.Add(Type.Missing); _FileNew = true; } PRI_Shablons = UserShablon.MET_FactoryListShablon(_eTip, _CodShablon); _WorkBook = _ExcelApp.Workbooks[1]; _WorkBook.Saved = true; Excel.Worksheet _Sheet = (Excel.Worksheet)_WorkBook.Worksheets.Item[1]; // Рисуем заголовки int _y = 1; int _x = 1; _Sheet.Cells[_y, _x++].Value = "Cod"; _Sheet.Columns[_x].ColumnWidth = 6; _Sheet.Cells[_y, _x++].Value = "ID"; _Sheet.Columns[_x].ColumnWidth = 7; _Sheet.Cells[_y, _x++].Value = "Nomer"; _Sheet.Columns[_x].ColumnWidth = 5; _Sheet.Cells[_y, _x++].Value = "VarId"; _Sheet.Columns[_x].ColumnWidth = 6; _Sheet.Cells[_y, _x++].Value = "Maska"; _Sheet.Columns[_x].ColumnWidth = 5; _Sheet.Cells[_y, _x++].Value = "Type"; _Sheet.Columns[_x].ColumnWidth = 20; _Sheet.Cells[_y, _x++].Value = "Razdel"; _Sheet.Columns[_x].ColumnWidth = 24; _Sheet.Cells[_y, _x++].Value = "Name"; _Sheet.Columns[_x].ColumnWidth = 28; _Sheet.Cells[_y, _x++].Value = "ValueStart"; _Sheet.Columns[_x].ColumnWidth = 19; _Sheet.Cells[_y, _x++].Value = "OutText"; _Sheet.Cells[_y, _x++].Value = "InText"; _Sheet.Columns[_x].ColumnWidth = 27; _Sheet.Cells[_y, _x++].Value = "xFormat"; _Sheet.Columns[_x].ColumnWidth = 45; _Sheet.Cells[_y, _x++].Value = "xLua"; _Sheet.Columns[_x].ColumnWidth = 22; _Sheet.Cells[_y, _x].Value = "xInfo"; // Заполняем данные _y = 2; foreach (var _Shablon in PRI_Shablons) { _x = 1; _Sheet.Cells[_y, _x++].Value = _Shablon.PROP_Cod; _Sheet.Cells[_y, _x++].Value = _Shablon.PROP_ID; _Sheet.Cells[_y, _x++].Value = _Shablon.PROP_Nomer; _Sheet.Cells[_y, _x++].Value = _Shablon.PROP_VarId; _Sheet.Cells[_y, _x++].Value = _Shablon.PROP_Maska; _Sheet.Cells[_y, _x++].Value = _Shablon.PROP_Type; _Sheet.Cells[_y, _x++].Value = _Shablon.PROP_Razdel; _Sheet.Cells[_y, _x++].Value = _Shablon.PROP_Name; _Sheet.Cells[_y, _x++].Value = _Shablon.PROP_ValueStart; _Sheet.Cells[_y, _x++].Value = _Shablon.PROP_OutText; _Sheet.Cells[_y, _x++].Value = _Shablon.PROP_InText; _Sheet.Cells[_y, _x++].Value = _Shablon.PROP_xFormat; _Sheet.Cells[_y, _x++].Value = _Shablon.PROP_xLua; _Sheet.Cells[_y, _x].Value = _Shablon.PROP_xInfo; if (_Shablon.PROP_xLua != "") { _Sheet.Cells[_y, _x].Rows.RowHeight = 100; } _y++; } _Sheet.Name = DateTime.Now.ToString().Replace(':', '.'); _Sheet.Copy(Before: _WorkBook.Worksheets[1]); _Sheet = (Excel.Worksheet)_WorkBook.Worksheets.Item[1]; _Sheet.Name = "Текущий"; _ExcelApp.DisplayAlerts = true; if (_FileNew) { _WorkBook.SaveAs(_PathFile); } else { _WorkBook.Save(); } _WorkBook.Activate(); IntPtr _Handler1 = FindWindow(null, _ExcelApp.Caption); SetForegroundWindow(_Handler1); _ExcelApp.Visible = true; }