예제 #1
0
        private void Button_GetDiploma_Click(object sender, RoutedEventArgs e)
        {
            Word._Application application = null;
            Word._Document    document    = null;
            Object            missingObj  = Missing.Value;
            Object            falseObj    = false;

            try
            {
                string         richText = new TextRange(StudentsTextBox.Document.ContentStart, StudentsTextBox.Document.ContentEnd).Text;
                List <Student> students = JsonConvert.DeserializeObject <List <Student> >(richText);

                //создаем обьект приложения word
                application = new Word.Application();
                // создаем путь к файлу
                Object templatePathObj = AppDomain.CurrentDomain.BaseDirectory + @"Diploma.dotx";

                int i = 1;
                foreach (var student in students)
                {
                    // если вылетим на этом этапе, приложение останется открытым
                    document = application.Documents.Add(ref templatePathObj, ref missingObj, ref missingObj, ref missingObj);

                    StringReplace(document, "%NAME%", student.Name);
                    StringReplace(document, "%STATUS%", student.Status);
                    StringReplace(document, "%GraduationDate%", student.GraduationDate.ToString("dd.MM.yyyy"));
                    StringReplace(document, "%GraduationPlace%", student.GraduationPlace);
                    Object pathToSaveObj = AppDomain.CurrentDomain.BaseDirectory + $@"student{i.ToString()}.docx";
                    document.SaveAs(ref pathToSaveObj, Word.WdSaveFormat.wdFormatDocumentDefault, ref missingObj,
                                    ref missingObj, ref missingObj, ref missingObj, ref missingObj, ref missingObj, ref missingObj,
                                    ref missingObj, ref missingObj, ref missingObj, ref missingObj, ref missingObj, ref missingObj,
                                    ref missingObj);
                    i++;


                    document.Close(ref falseObj, ref missingObj, ref missingObj);
                }

                application.Quit(ref missingObj, ref missingObj, ref missingObj);

                document    = null;
                application = null;
                StudentsTextBox.Document.Blocks.Clear();
                StudentsTextBox.AppendText("Done");
            }
            catch (Exception exception)
            {
                document?.Close(ref falseObj, ref missingObj, ref missingObj);
                application?.Quit(ref missingObj, ref missingObj, ref missingObj);
                document    = null;
                application = null;
                StudentsTextBox.Document.Blocks.Clear();
                StudentsTextBox.AppendText(exception.Message);
            }
        }
예제 #2
0
        private void AddStudent()
        {
            var studentName = GetStudentTextBoxTrimValue();
            var platoonName = GetSelectedPlatoonName();

            _platoonWorker.AddStudentInPlatoon(platoonName, studentName);

            StudentsListBox.Items.Add(studentName);
            StudentsTextBox.Clear();

            ClearStudentButton.Enabled = true;
        }
예제 #3
0
        private void Button_GetStudents_Click(object sender, RoutedEventArgs e)
        {
            Excel.Application objExcel = null;
            try
            {
                StudentsTextBox.Document.Blocks.Clear();
                objExcel = new Excel.Application();
                //Открываем книгу.
                Excel.Workbook objWorkBook = objExcel.Workbooks.Open(ExcelPathTextBox.Text, 0, true, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
                //Выбираем таблицу(лист).
                var objWorkSheet = (Excel.Worksheet)objWorkBook.Sheets[1];

                int            i        = 1;
                List <Student> students = new List <Student>();
                //Выбираем записи из столбца.
                while (!String.IsNullOrEmpty(objWorkSheet.Range["A" + i.ToString(), "A" + i.ToString()].Text.ToString()))
                {
                    students.Add(new Student
                    {
                        Name           = objWorkSheet.Range["A" + i.ToString(), "A" + i.ToString()].Text.ToString(),
                        Status         = objWorkSheet.Range["B" + i.ToString(), "B" + i.ToString()].Text.ToString(),
                        GraduationDate =
                            DateTime.Parse(objWorkSheet.Range["C" + i.ToString(), "C" + i.ToString()].Text.ToString()),
                        GraduationPlace = objWorkSheet.Range["D" + i.ToString(), "D" + i.ToString()].Text
                                          .ToString()
                    });
                    i++;
                }
                objExcel.Workbooks.Close();
                objExcel.Quit();

                StudentsTextBox.AppendText(JsonConvert.SerializeObject(students));
            }
            catch (Exception exception)
            {
                objExcel?.Workbooks.Close();
                objExcel?.Quit();
                StudentsTextBox.Document.Blocks.Clear();
                StudentsTextBox.AppendText(exception.Message);
            }
        }