Ejemplo n.º 1
0
        // Create Method
        public void Create(SPTypes SPType)
        {
            try
            {
                // Declare specification component
                Component specComponent = null;

                // Check whether the component exist
                if (!IsComponentExist(SPType, out specComponent))
                {
                    System.Windows.MessageBox.Show("Компонент для данного типа спецификации отсутствует в модели.", "Компонент отсутствует в модели", MessageBoxButton.OK);
                    return;
                }
                // Open Workbook
                Excel.Application excelApp;
                Excel.Workbook    workBook = ExcelClass.OpenExcelWorkBook(FilePath, out excelApp, true);
                // Get appropriate work sheet
                Excel.Worksheet workSheet = GetWorkSheet(workBook, SPType);
                // Get specification header
                string specHeader = GetSpecificationHeader(SPType);
                // Format worksheet columns
                FormatWorkSheet(workSheet, specHeader);
                // Load Data
                table = GetDataTable(specComponent);
                // Refine and Merge table rows
                RefineAndMerge();
                // Sort table
                SortData(SPType);
                // Write data to Excel sheet
                WriteData(workSheet);
                // Match VP and SP data
                MatchData(workSheet, excelApp);

                // Close Excel
                ExcelClass.CloseExcel(workBook, excelApp);
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message, System.Reflection.MethodBase.GetCurrentMethod().Name, MessageBoxButton.OK);
            }
        }
Ejemplo n.º 2
0
        private void SearchSPFile()
        {
            try
            {
                // Search for SP file in project directory
                List <string> foundFiles = Directory.GetFiles(projectDirectory, "СП " + mainComponent.PartNumber + "*.*")
                                           .Where(file => file.ToLower().EndsWith("xls") || file.ToLower().EndsWith("xlsx"))
                                           .ToList();

                if (foundFiles.Count > 0)
                {
                    FilePath = foundFiles[0];
                }
                else
                {
                    // Set file path
                    FilePath = Path.Combine(projectDirectory, "СП " + mainComponent.PartNumber + ".xlsx");

                    // Create SP file
                    Excel.Application excelApp = new Excel.Application();
                    if (excelApp == null)
                    {
                        Console.WriteLine("EXCEL could not be started. Check that your office installation and project references are correct.");
                        return;
                    }
                    excelApp.Visible = false;
                    Excel.Workbook excelWorkBook = excelApp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
                    excelWorkBook.SaveAs(FilePath);
                    excelApp.Quit();
                    ExcelClass.KillExcelProcess(excelApp);
                }
                FileName = Path.GetFileName(FilePath);
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message, System.Reflection.MethodBase.GetCurrentMethod().Name, MessageBoxButton.OK);
            }
        }