Exemple #1
0
        public DataTable techBaseTable = new DataTable(); // Cюда записана техническая база
        public bool Optimize()
        {
            try // код помещён в блок try, потому что возможно исключение типа OpenXmlPackageException - Invalid Hyperlink
            {
                using (SpreadsheetDocument spreadSheetDocument = SpreadsheetDocument.Open(techBasePath, true))
                {
                    techBaseTable = DataTableFromExcel.GetDataTableFromSpreadSheet(spreadSheetDocument);
                }
            }
            catch (OpenXmlPackageException e)
            {
                if (e.ToString().Contains("Invalid Hyperlink")) // если исключение сожержит "Invalid Hyperlink"
                {
                    using (FileStream fs = new FileStream(techBasePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                    {
                        UriFixer.FixInvalidUri(fs, brokenUri => UriFixer.FixUri(brokenUri)); // чиним Uri
                    }
                    using (SpreadsheetDocument spreadSheetDocument = SpreadsheetDocument.Open(techBasePath, true))
                    {
                        techBaseTable = DataTableFromExcel.GetDataTableFromSpreadSheet(spreadSheetDocument);
                    }
                }
            }
            catch (Exception)
            {
                return(false);
            }

            foreach (DataRow row in techBaseTable.Rows)           // Оптимизация тех. базы - убираем цифры
            {
                string product = System.Convert.ToString(row[1]); // Название продукта
                string productWithoutDigits = "";
                for (int i = 0; i < product.Length; i++)          // Убираем числа
                {
                    if (!char.IsDigit(product[i]))
                    {
                        productWithoutDigits += product[i];
                    }
                }
                row[1] = productWithoutDigits;
            }
            return(true);
        }
        static string[] stringSeparators = new string[] { ", " }; // разделитель строк, содержащих интересуюие продукты

        public static string Fill(ref List <ProductsOfInterest> products, List <string> sheets_names, List <ExcelFile> files, Report report)
        {
            products = new List <ProductsOfInterest>();
            foreach (string sheetFromListBox in sheets_names)
            {
                foreach (ExcelFile file in files)
                {
                    foreach (string sheetFromFile in file.Sheets)
                    {
                        if (sheetFromListBox.Equals(sheetFromFile))
                        {
                            try // код помещён в блок try, потому что возможно исключение типа OpenXmlPackageException - Invalid Hyperlink
                            {
                                using (SpreadsheetDocument spreadSheetDocument = SpreadsheetDocument.Open(file.FilePath, true))
                                {
                                    HelpFill(spreadSheetDocument, sheetFromFile, file, ref products, report);
                                }
                            }
                            catch (OpenXmlPackageException e)
                            {
                                if (e.ToString().Contains("Invalid Hyperlink")) // если исключение сожержит "Invalid Hyperlink"
                                {
                                    using (FileStream fs = new FileStream(file.FilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                                    {
                                        UriFixer.FixInvalidUri(fs, brokenUri => UriFixer.FixUri(brokenUri)); // чиним Uri
                                    }
                                    using (SpreadsheetDocument spreadSheetDocument = SpreadsheetDocument.Open(file.FilePath, true))
                                    {
                                        HelpFill(spreadSheetDocument, sheetFromFile, file, ref products, report);
                                    }
                                }
                            }
                            catch (Exception e)
                            {
                                return(file.FilePath);
                            }
                        }
                    }
                }
            }
            return(null);
        }