Exemplo n.º 1
0
        /// <summary>
        /// Заполнение DataTable из базы данных.
        /// </summary>
        /// <param name="pathToDB">Путь к базе данных.</param>
        /// <returns>DataTable.</returns>
        public DataTable FillDB(string pathToDB)
        {
            var dataTable = this.dbProvider.FillDB(pathToDB);

            string name = "", subject = "", group = "", date = "", time = "", place = "", addition = "";

            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                for (int j = 0; j < dataTable.Columns.Count; j++)
                {
                    if (j == 0)
                    {
                        name = dataTable.Rows[i][j].ToString();

                        if (!lecturers.Contains(name))
                        {
                            lecturers.Add(name);
                        }
                    }
                    if (j == 1)
                    {
                        subject = dataTable.Rows[i][j].ToString();

                        if (!subjects.Contains(subject))
                        {
                            subjects.Add(subject);
                        }
                    }
                    if (j == 2)
                    {
                        group = dataTable.Rows[i][j].ToString();

                        if (!groups.Contains(group))
                        {
                            groups.Add(group);
                        }
                    }

                    if (j == 3)
                    {
                        date = dataTable.Rows[i][j].ToString();
                    }
                    if (j == 4)
                    {
                        time = dataTable.Rows[i][j].ToString();
                    }
                    if (j == 5)
                    {
                        place = dataTable.Rows[i][j].ToString();
                    }
                    if (j == 6)
                    {
                        addition = dataTable.Rows[i][j].ToString();
                    }
                }
                Consultation cons = new Consultation(name, subject, group, date,
                                                     time, place, addition);
                //this.mainController.Consultations.A
                consultations.Add(cons);
            }

            return(dataTable);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Функция чтения документа.
        /// </summary>
        /// <param name="selectedDocument">Путь к документу для парсинга.</param>
        /// <param name="lecturers">Список считанных лекторов.</param>
        /// <param name="subjects">Список считанных предметов.</param>
        /// <param name="groups">Список считанных групп.</param>
        /// <param name="consultations">Список с консультациями.</param>
        /// <returns></returns>
        public string ReadDoc(string selectedDocument, List <string> lecturers, List <string> subjects,
                              List <string> groups, List <Consultation> consultations)
        {
            TablesRanges = new List <Word.Range>();

            Word.Application word     = new Word.Application();
            object           missing  = Type.Missing;
            object           filename = selectedDocument;

            Word.Document doc = word.Documents.Open(ref filename, ref missing, ref missing,
                                                    ref missing, ref missing, ref missing,
                                                    ref missing, ref missing, ref missing,
                                                    ref missing, ref missing, ref missing,
                                                    ref missing, ref missing, ref missing,
                                                    ref missing);

            var wordApp = new Microsoft.Office.Interop.Word.Application();

            try
            {
                for (int i = 1; i <= doc.Tables.Count; i++)
                {
                    Word.Range TRange = doc.Tables[i].Range;
                    TablesRanges.Add(TRange);
                }

                int    colNumber = 0;
                string name = "", subject = "", group = "", date = "", time = "", place = "", addition = "";

                for (int par = 1; par <= doc.Paragraphs.Count; par++)
                {
                    Word.Range r = doc.Paragraphs[par].Range;

                    foreach (Word.Range range in TablesRanges)
                    {
                        if (r.Start >= range.Start && r.Start <= range.End)
                        {
                            colNumber++;

                            if (colNumber == (int)tableColumns.name && r.Text.ToString() != "\r\a")
                            {
                                name = r.Text.ToString().Trim(new Char[] { '\r', '\a' });

                                if (!lecturers.Contains(name))
                                {
                                    lecturers.Add(name);
                                }
                            }

                            if (colNumber == (int)tableColumns.subject && r.Text.ToString() != "\r\a")
                            {
                                subject = r.Text.ToString().Trim(new Char[] { '\r', '\a' });

                                if (!subjects.Contains(subject))
                                {
                                    subjects.Add(subject);
                                }
                            }

                            if (colNumber == (int)tableColumns.group && r.Text.ToString() != "\r\a")
                            {
                                group = r.Text.ToString().Trim(new Char[] { '\r', '\a' });

                                if (!groups.Contains(group))
                                {
                                    groups.Add(group);
                                }
                            }

                            if (colNumber == (int)tableColumns.date)
                            {
                                date = "";
                                if (r.Text.ToString() == "\r\a")
                                {
                                    date = "-";
                                }
                                else
                                {
                                    date = r.Text.ToString().Trim(new Char[] { '\r', '\a' });
                                }
                            }

                            if (colNumber == (int)tableColumns.time)
                            {
                                time = "";

                                if (r.Text.ToString() == "\r\a")
                                {
                                    time = "-";
                                }
                                else
                                {
                                    time = r.Text.ToString().Trim(new Char[] { '\r', '\a' });
                                }
                            }

                            if (colNumber == (int)tableColumns.place)
                            {
                                place = "";
                                if (r.Text.ToString() == "\r\a")
                                {
                                    place = "-";
                                }
                                else
                                {
                                    place = r.Text.ToString().Trim(new Char[] { '\r', '\a' });
                                }
                            }

                            if (colNumber == (int)tableColumns.addition)
                            {
                                addition = "";
                                if (r.Text.ToString() == "\r\a")
                                {
                                    addition = "-";
                                }
                                else
                                {
                                    addition = r.Text.ToString().Trim(new Char[] { '\r', '\a' });
                                }
                            }
                            if (colNumber == (int)tableColumns.end)
                            {
                                Consultation cons = new Consultation(name, subject, group, date,
                                                                     time, place, addition);
                                consultations.Add(cons);
                                colNumber = 0;
                            }
                        }
                    }
                }

                //Удаление первой записи в коллекции
                //где хранятся заголовки столбцов таблицы.
                consultations.RemoveAt(0);

                return("OK");
            }
            catch (System.Runtime.InteropServices.COMException ex)
            {
                return(ex.Message);
            }
            finally
            {
                doc.Close(false);
                word.Quit(false);
                wordApp.Quit(false);

                if (word != null)
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(word);
                }
                if (doc != null)
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(doc);
                }
                if (wordApp != null)
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp);
                }

                doc     = null;
                word    = null;
                wordApp = null;
                GC.Collect();
            }
        }