コード例 #1
0
ファイル: ProccesingExcel.cs プロジェクト: Vormaint/IBA
        public override void ImportFromDataBase(DateTime date, string Name, string LastName,
                                                string SurName, string City, string Country)
        {
            string path = @"C:\Users\User\source\repos\IBA_Tasks\Task-1\DataInteraction\ExcelFile.xlsx";

            Excel.Application excelapp  = new Excel.Application();
            Excel.Workbook    workbook  = excelapp.Workbooks.Add();
            Excel.Worksheet   worksheet = (Excel.Worksheet)workbook.ActiveSheet;

            int CurrentRow = 0;

            using (ConnectionDataBase db = new ConnectionDataBase())
            {
                var Humen = db.HumenDataSearch(date, Name, LastName, SurName, City, Country);

                foreach (Human human in Humen)
                {
                    CurrentRow++;

                    worksheet.Rows[CurrentRow, 1] = human.Date;
                    worksheet.Rows[CurrentRow, 2] = human.FirstName;
                    worksheet.Rows[CurrentRow, 3] = human.LastName;
                    worksheet.Rows[CurrentRow, 4] = human.SurName;
                    worksheet.Rows[CurrentRow, 5] = human.City;
                    worksheet.Rows[CurrentRow, 6] = human.Country;
                }
            }

            excelapp.AlertBeforeOverwriting = false;
            workbook.SaveAs(path);
            excelapp.Quit();
        }
コード例 #2
0
ファイル: ProccesingXML.cs プロジェクト: Vormaint/IBA
        public override void ImportFromDataBase(DateTime date, string Name, string LastName,
                                                string SurName, string City, string Country)
        {
            XmlDocument xDoc = new XmlDocument();

            xDoc.Load(@"C:\Users\User\source\repos\IBA_Tasks\Task-1\DataInteraction\XML.xml");

            //Открытие соединения с БД
            using (ConnectionDataBase db = new ConnectionDataBase())
            {
                var Humen = db.HumenDataSearch(date, Name, LastName, SurName, City, Country);

                foreach (Human human in Humen)
                {
                    XmlElement xRoot = xDoc.DocumentElement;
                    // Создаем новую запись.
                    XmlElement RecordElem = xDoc.CreateElement("Record");
                    // Создаем атрибут ID.
                    XmlAttribute idAttr = xDoc.CreateAttribute("ID");
                    // Создаем элементы Date, FirstName, LastName, SurName, City, Country.
                    XmlElement DateElem      = xDoc.CreateElement("Date");
                    XmlElement FirstNameElem = xDoc.CreateElement("FirstName");
                    XmlElement LastNameElem  = xDoc.CreateElement("LastName");
                    XmlElement SurNameElem   = xDoc.CreateElement("SurName");
                    XmlElement CityElem      = xDoc.CreateElement("City");
                    XmlElement CountryElem   = xDoc.CreateElement("Country");

                    // Создаем текстовые значения для элементов и атрибута.
                    XmlText idText        = xDoc.CreateTextNode(human.HumanID.ToString());
                    XmlText DateText      = xDoc.CreateTextNode(human.Date.ToString("d"));
                    XmlText FirstNameText = xDoc.CreateTextNode(human.FirstName);
                    XmlText LastNameText  = xDoc.CreateTextNode(human.LastName);
                    XmlText SurNameText   = xDoc.CreateTextNode(human.SurName);
                    XmlText CityText      = xDoc.CreateTextNode(human.City);
                    XmlText CountryText   = xDoc.CreateTextNode(human.Country);

                    // Добавляем узлы.
                    idAttr.AppendChild(idText);
                    DateElem.AppendChild(DateText);
                    FirstNameElem.AppendChild(FirstNameText);
                    LastNameElem.AppendChild(LastNameText);
                    SurNameElem.AppendChild(SurNameText);
                    CityElem.AppendChild(CityText);
                    CountryElem.AppendChild(CountryText);

                    RecordElem.Attributes.Append(idAttr);
                    RecordElem.AppendChild(DateElem);
                    RecordElem.AppendChild(FirstNameElem);
                    RecordElem.AppendChild(LastNameElem);
                    RecordElem.AppendChild(SurNameElem);
                    RecordElem.AppendChild(CityElem);
                    RecordElem.AppendChild(CountryElem);

                    xRoot.AppendChild(RecordElem);
                }
            }

            xDoc.Save(@"C:\Users\User\source\repos\IBA_Tasks\Task-1\DataInteraction\XML.xml");
        }