// Экспорт расписания в файл Excel public static void ExportTimetable(DataGridView Timetable, string GroupName) { // Открытие документа Excel Excel.Application ExcelApp = new Excel.Application(); Excel.Workbook ExcelWb; Excel.Worksheet ExcelWs; ExcelWb = ExcelApp.Workbooks.Add(1); ExcelWs = (Excel.Worksheet)ExcelWb.Sheets[1]; ExcelApp.DisplayAlerts = false; Excel.Range rg = (Excel.Range)ExcelWs.Cells[1, 2]; rg.EntireColumn.NumberFormat = "hh:mm"; int[] DayRows = new int[] { 3, 3 }; string Day = Timetable.Rows[0].Cells[0].Value.ToString(); // Заголовок таблицы ExcelApp.Cells[2, 1] = "День"; ExcelApp.Cells[2, 2] = "Начало занятия"; ExcelApp.Cells[2, 3] = "Занятие"; for (int i = 0; i < Timetable.Rows.Count; i++) { ExcelApp.Cells[i + 3, 1] = Timetable.Rows[i].Cells[0].Value; ExcelApp.Cells[i + 3, 2] = Timetable.Rows[i].Cells[1].Value; ExcelApp.Cells[i + 3, 3] = Timetable.Rows[i].Cells[3].Value; // Объединить ячейки для дня недели if (Timetable.Rows[i].Cells[3].Value.ToString() == "Конец дня") { DayRows[1] = i + 3; Excel.Range ColumnRange = ExcelWs.Range["A" + DayRows[0], "A" + DayRows[1]]; ColumnRange.Merge(); ColumnRange.VerticalAlignment = Excel.XlVAlign.xlVAlignTop; ColumnRange.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter; DayRows[0] = i + 4; DayRows[1] = i + 4; } //ExcelApp.Cells[i + 1, 2] = Timetable.Rows[i].Cells[1].Value; //ExcelApp.Cells[i + 1, 3] = Timetable.Rows[i].Cells[3].Value; } ExcelWs.Columns["A:C"].AutoFit(); ExcelApp.Cells[1, 1] = "Расписание занятий в группе продленного дня для группы \"" + GroupName + "\""; // Сохранить документ и Отправить по почте string SavedFile = SaveTimetable(ExcelWb, GroupName); ExcelWb.Close(0); ExcelApp.Quit(); MailOperations.SendTimetable(SavedFile, GroupName); }
private async void OpenButton_Click(object sender, EventArgs e) { var OpenDialog = new OpenFileDialog(); if (OpenDialog.ShowDialog() == DialogResult.OK) { string BallisticsPath = OpenDialog.FileName; Excel.Application ExcelApp = new Excel.Application(); ExcelApp.Visible = false; ExcelApp.DisplayAlerts = false; Excel.Workbook ExcelWb; try { ExcelWb = ExcelApp.Workbooks.Open(BallisticsPath); } catch { try { ExcelWb = ExcelApp.Workbooks.Open(Directory.GetCurrentDirectory() + "\\" + BallisticsPath); } catch { throw new Exception("Файл не найден"); } } Excel.Worksheet ExcelWs = (Excel.Worksheet)ExcelWb.ActiveSheet; List <double> TimeList = new List <double>(); List <double> ThrustList = new List <double>(); List <double> HeightList = new List <double>(); List <double> DistanceList = new List <double>(); int CurrentPosition = 2; Grid.Rows.Clear(); await Task.Run(() => { try { while (ExcelWs.Cells[CurrentPosition, 1].Value != null) { this.Invoke(new Action(() => { Grid.Rows.Add(); Grid.Rows[CurrentPosition - 2].Cells[0].Value = ExcelWs.Cells[CurrentPosition, 1].Value; Grid.Rows[CurrentPosition - 2].Cells[1].Value = ExcelWs.Cells[CurrentPosition, 2].Value; Grid.Rows[CurrentPosition - 2].Cells[2].Value = ExcelWs.Cells[CurrentPosition, 3].Value; Grid.Rows[CurrentPosition - 2].Cells[3].Value = ExcelWs.Cells[CurrentPosition, 4].Value; CurrentPosition++; })); } } catch { throw new Exception("Неверный формат данных"); } finally { ExcelWb.Close(); ExcelApp.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelApp); System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelWs); System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelWb); ExcelApp = null; ExcelWs = null; ExcelWb = null; GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); } }); } }