private void Button_Click(object sender, RoutedEventArgs e) { app = new Application.Application { DisplayAlerts = false }; try { wrbks = app.Workbooks; wrbk = wrbks.Open(Path.Combine(Environment.CurrentDirectory, FFile.Text)); wrsh = wrbk.ActiveSheet as Application.Worksheet; wrsh.Range["A1"].Value = "Hello World"; app.Visible = true; Topmost = true; wrbk.Save(); } finally { app.ActiveWorkbook.Close(); app.Quit(); Marshal.ReleaseComObject(wrsh); Marshal.ReleaseComObject(wrbk); Marshal.ReleaseComObject(wrbks); } }
private void Button_Click(object sender, RoutedEventArgs e) { app = new Application.Application { DisplayAlerts = true }; var Lo = new Logic(); if (Sfile.Text == null) { System.Windows.MessageBox.Show("Введите путь сохранения"); } else { Lo.Read(FFile.Text, Fio.Text, semestr.SelectedIndex, Sfile.Text); } }
public void FillTemplate(string SaveWay) { Application.Application exl = new Application.Application(); SaveWay += "\\" + selectedTeacher + ".xls"; if (exl == null) { System.Windows.MessageBox.Show("Проверьте инсталляцию MS Excel"); return; } Application.Worksheet xlSheet; object misValue = System.Reflection.Missing.Value; Application.Workbook xlBook = exl.Workbooks.Add(misValue); xlSheet = (Application.Worksheet)xlBook.Sheets[1]; xlSheet.Cells[1, 1] = "Предмет"; xlSheet.Cells[1, 2] = "Группа"; xlSheet.Cells[1, 3] = "Буджет"; xlSheet.Cells[1, 4] = "ВнеБ"; xlSheet.Cells[1, 5] = "Всего"; int i = 2; foreach (var disciplineName in hoursPerDiscipline.Keys) { foreach (var groupname in hoursPerDiscipline[disciplineName].Keys) { xlSheet.Cells[i, 1] = hoursPerDisciplineNoSpace[disciplineName]; //"Часов по предмету " + disciplineName + xlSheet.Cells[i, 2] = groupname; xlSheet.Cells[i, 3] = hoursPerDiscipline[disciplineName][groupname].budget; // ": Бюджет: " + hoursPerDiscipline[disciplineName].budget + xlSheet.Cells[i, 4] = hoursPerDiscipline[disciplineName][groupname].nonBudget; //", Не бюджет: " + hoursPerDiscipline[disciplineName].nonBudget + xlSheet.Cells[i, 5] = hoursPerDiscipline[disciplineName][groupname].TotalHours; // ", Всего: " + hoursPerDiscipline[disciplineName].TotalHours + "\n"; i++; } } xlBook.SaveAs(SaveWay, Application.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Application.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); xlBook.Close(true, misValue, misValue); exl.Quit(); System.Windows.MessageBox.Show("Файл Готов. Путь к файлу" + SaveWay); Marshal.ReleaseComObject(xlSheet); Marshal.ReleaseComObject(xlBook); Marshal.ReleaseComObject(exl); }
public void Read(string FirstFile, string TeacherFio, int SemestrInsert, string SaveWay) { Stopwatch stopwatch = Stopwatch.StartNew(); selectedTeacher = TeacherFio.Replace(" ", string.Empty).Replace(".", string.Empty); selectedSemester = SemestrInsert; Application.Application app = new Application.Application { DisplayAlerts = true }; Application.Workbooks wrbks = app.Workbooks; wrbk = app.Workbooks.Open(Path.Combine(Environment.CurrentDirectory, FirstFile)); try { for (int sheetIndex = 1; sheetIndex <= wrbk.Worksheets.Count; sheetIndex++) { ProcessSheet(sheetIndex); } } catch (Exception E) { System.Windows.MessageBox.Show("Error: " + E.Message + ". Elapsed time: " + stopwatch.Elapsed); } finally { app.Workbooks.Close(); app.Quit(); Marshal.ReleaseComObject(wrbk); Marshal.ReleaseComObject(wrbks); FillTemplate(SaveWay); //System.Windows.MessageBox.Show(output + "\nElapsed time: " + stopwatch.Elapsed + "\nDiscipline count = " + hoursPerDiscipline.Count); } }