Пример #1
0
        static void Main(string[] args)
        {
            try
            {
                Stopwatch swTotal = new Stopwatch();
                swTotal.Start();
                DataTable dt = new DataTable();
                Model.NewDT(ref dt);
                string directory = AppDomain.CurrentDomain.BaseDirectory;
                // Обработка реестров excel
                MyExcel.DirSearchEx(directory, ref dt);
                // Обработка реестров word
                MyWord.DirSearchWord(directory, ref dt);

                swTotal.Stop();
                Console.WriteLine("Reading (new): " + swTotal.ElapsedMilliseconds + " ms");
                Logger.WriteLine("Reading (new): " + swTotal.ElapsedMilliseconds + " ms");
                swTotal.Reset();
                Console.WriteLine("Будет вставленно строк : " + dt.Rows.Count);
                Logger.WriteLine("Будет вставленно строк : " + dt.Rows.Count);
                swTotal.Start();
                // Заполнение таблицы
                ExportData.ExportDT(dt);
                swTotal.Stop();
                Console.WriteLine("Writing (new): " + swTotal.ElapsedMilliseconds + " ms");
                Logger.WriteLine("Writing (new): " + swTotal.ElapsedMilliseconds + " ms");
                Console.WriteLine("----------ВСЁ!-------------");
                Console.ReadKey();
            }
            catch (Exception excpt)
            {
                Console.WriteLine(excpt.Message);
                Logger.WriteLine(excpt.Message);
            }
        }
Пример #2
0
        // Обработка excel (Выгрузка из него данных)
        private static void ReestrParse(FileInfo _file, ref DataTable dt)
        {
            if (_file == null)
            {
                throw new ArgumentNullException(nameof(_file));
            }
            DataSet ds = new DataSet();
            string  connectionString = GetConnectionString(_file);

            using (OleDbConnection conn = new OleDbConnection(connectionString))
            {
                conn.Open();
                try
                {
                    OleDbCommand cmd = new OleDbCommand();
                    cmd.Connection = conn;
                    // Get all Sheets in Excel File
                    DataTable dtSheet = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                    // Loop through all Sheets to get data
                    foreach (DataRow dr in dtSheet.Rows)
                    {
                        string sheetName = dr["TABLE_NAME"].ToString();
                        // Get all rows from the Sheet
                        cmd.CommandText = "SELECT * FROM [" + sheetName + "]";
                        DataTable dts = new DataTable();
                        dts.TableName = sheetName;
                        OleDbDataAdapter da = new OleDbDataAdapter(cmd);
                        da.Fill(dts);
                        ds.Tables.Add(dts);
                    }

                    string[,] table = new string[ds.Tables[0].Rows.Count, ds.Tables[0].Columns.Count];
                    int   shifrdoc  = 1;                            // индекс столбца шифрдокумента
                    int   sooruz    = 2;                            // индекс столбца сооружение
                    int   template  = 0;
                    Regex regExOboz = new Regex(@"^Шифр.*$");       // находим индекс столбца
                    Regex regExNaim = new Regex(@"^Сооружение.*$"); // находим индекс столбца
                    for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
                    {
                        for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
                        {
                            table[j, i] = ds.Tables[0].Rows[j].ItemArray[i].ToString();
                            if (regExOboz.Match(table[j, i]).Success)
                            {
                                shifrdoc = i; // находим индекс столбца обозначение
                            }
                            if (regExNaim.Match(table[j, i]).Success)
                            {
                                sooruz = i; // находим индекс столбца наименование
                            }
                        }
                    }

                    bool  state             = false;
                    Regex regEx_marksLS     = new Regex("^.*ЛС.*$");
                    Regex regExProject      = new Regex("^Шифр..*$");                 // находим шифр проекта по этой маске
                    Regex regExStage        = new Regex("^..*[Э,э][Т,т][А,а][П,п]$"); // находим этап по этой маске
                    Regex regExStage2       = new Regex("^[Э,э][Т,т][А,а][П,п]..*$"); // находим этап по этой маске
                    Regex regExStage_number = new Regex("^/d*$");                     // находим цифру по этой маске
                    Row   row = new Row();
                    row.STAGE           = "1";
                    row.NAIMPROJE       = table[0, 0];
                    row.Directory       = _file.FullName;
                    row.DATEOFLASTWRITE = _file.LastWriteTime.ToShortDateString();
                    string directory = _file.DirectoryName;
                    for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
                    {
                        switch (template)
                        {
                        case 0:
                            row.SHFRDOC = table[j, shifrdoc];
                            row.NAIMOBJ = table[j, sooruz];
                            for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
                            {
                                if (regExProject.Match(table[j, i]).Success)
                                {
                                    row.SHFR = table[j, i];
                                    row.SHFR = row.SHFR.Replace("Шифр ", "");
                                }

                                if (regExStage.Match(table[j, i]).Success)
                                {
                                    foreach (var item in table[j, 1].Split(' '))
                                    {
                                        if (regExStage_number.Match(item).Success)
                                        {
                                            row.STAGE = item;
                                        }
                                    }
                                }

                                if (regExStage2.Match(table[j, i]).Success)
                                {
                                    foreach (var item in table[j, 1].Split(' '))
                                    {
                                        if (regExStage_number.Match(item).Success)
                                        {
                                            row.STAGE = item;
                                        }
                                    }
                                }

                                if (i > 1 && table[j, i] != "")
                                {
                                    row.MARKA = table[j, i];
                                    if (row.SHFRDOC != "" && row.SHFRDOC != "Шифр" &&
                                        row.SHFRDOC != "Договор №")
                                    {
                                        if (row.MARKA != "")
                                        {
                                            row.OBOSDOC = row.SHFRDOC + "-" + row.MARKA;
                                            bool         done       = true;
                                            List <Model> FilesNames = new List <Model>();
                                            MyWord.Searchfiles_izobrlist(directory, row.SHFRDOC + "*" + row.MARKA, ref done,
                                                                         ref dt,
                                                                         ref FilesNames);
                                            MyWord.ChooseList(FilesNames, ref row, ref dt);
                                            if (done)
                                            {
                                                if (regEx_marksLS.Match(row.SHFRDOC + "*" + row.MARKA).Success)
                                                {
                                                    row.NAIMIZOBR = "";
                                                    Model.DtAdd(ref dt, ref row);
                                                }
                                                else
                                                {
                                                    row.NAIMIZOBR = "Не удалось найти вспомогательный файл";
                                                    Model.DtAdd(ref dt, ref row);
                                                }
                                            }
                                        }
                                    }
                                }
                            }

                            break;

                        case 1:
                            if (table[j, 0] != "")
                            {
                                row.SHFRDOC = table[j, 0];
                            }
                            if (table[j, 1] != "")
                            {
                                row.NAIMOBJ = table[j, 1];
                            }
                            if (table[j, 2] != "")
                            {
                                //row.STAGE = table[j, 2];
                                foreach (var item in table[j, 2].Split(' '))
                                {
                                    if (regExStage_number.Match(item).Success)
                                    {
                                        row.STAGE = item;
                                    }
                                }
                            }

                            if (row.SHFRDOC != "Шифр" && table[j, 3] != "")
                            {
                                //row.MARKA = table[j, 3];
                                foreach (var item in table[j, 3].Split(','))
                                {
                                    row.OBOSDOC = row.SHFRDOC + "-" + item.Replace(" ", "");
                                    bool         done       = true;
                                    List <Model> FilesNames = new List <Model>();
                                    MyWord.Searchfiles_izobrlist(directory, row.SHFRDOC + "*" + item.Replace(" ", ""), ref done,
                                                                 ref dt, ref FilesNames);
                                    MyWord.ChooseList(FilesNames, ref row, ref dt);
                                    if (done)
                                    {
                                        if (regEx_marksLS.Match(row.SHFRDOC + "*" + item.Replace(" ", "")).Success)
                                        {
                                            row.NAIMIZOBR = "";
                                            Model.DtAdd(ref dt, ref row);
                                        }
                                        else
                                        {
                                            row.NAIMIZOBR = "Не удалось найти вспомогательный файл";
                                            Model.DtAdd(ref dt, ref row);
                                        }
                                    }
                                }
                            }

                            break;
                        }

                        if (table[j, 0] == "Шифр" && table[j, 1] == "Сооружение (площадка, трасса, система)" &&
                            table[j, 3] == "Марка" && table[j, 4] == "Изменения")
                        {
                            template = 1;
                        }
                    }
                }
                catch (Exception excpt)
                {
                    Console.WriteLine(excpt.Message);
                    Logger.WriteLine(excpt.Message);
                }
                finally
                {
                    conn.Close();
                }
            }
        }