Esempio n. 1
0
        private void RefreshSheetNames()
        {
            if (!File.Exists(SelectedComp.SourceWorkbookName) ||
                System.IO.Path.GetExtension(SelectedComp.SourceWorkbookName) == GlobalDefines.MAIN_WBK_EXTENSION ||
                System.IO.Path.GetExtension(SelectedComp.SourceWorkbookName) == GlobalDefines.XLS_EXTENSION ||
                System.IO.Path.GetExtension(SelectedComp.SourceWorkbookName) == GlobalDefines.XLSX_EXTENSION)
            {
                SheetNames.Clear();

                using (var wrapper = new DisposableWrapper <ShowAsyncResult>(CWaitingWnd.ShowAsync(Title,
                                                                                                   Properties.Resources.resGettingSheetNames,
                                                                                                   this,
                                                                                                   CheckAccess()),
                                                                             asyncResult =>
                {
                    if (asyncResult?.hFinishedSearchEvent != null)
                    {
                        asyncResult.hFinishedSearchEvent.Set();
                    }
                }))
                {
                    try
                    {
                        using (var excelApp = new DisposableWrapper <ExcelApplicationEx>(GlobalDefines.StartExcel(),
                                                                                         app =>
                        {
                            if (app != null)
                            {
                                app.App.DisplayAlerts = true;

                                if (app.NewAppCreated)
                                {
                                    app.App.Quit();
                                }

                                app = null;
                            }
                        }))
                        {
                            bool WbkOpened;
                            var  wbk = WorkbookGenerator.OpenWbk(excelApp, SelectedComp.SourceWorkbookName, out WbkOpened);

                            if (wbk != null)
                            {
                                foreach (MSExcel.Worksheet wsh in wbk.Worksheets)
                                {
                                    SheetNames.Add(wsh.Name);
                                }
                            }
                            else
                            {
                                MessageBox.Show(this,
                                                string.Format(Properties.Resources.resfmtSourceWorkbookCouldNotBeOpened, SelectedComp.SourceWorkbookName),
                                                Title,
                                                MessageBoxButton.OK,
                                                MessageBoxImage.Error);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(this,
                                        string.Format(Properties.Resources.resfmtErrorDurExcelOperation, ex.Message),
                                        Title,
                                        MessageBoxButton.OK,
                                        MessageBoxImage.Error);
                    }
                }
            }
            else
            {
                MessageBox.Show(this, Properties.Resources.resInvalidSourceWorkbookName, Title, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Esempio n. 2
0
        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            if (!CheckSettings())
            {
                return;
            }

            var dataExtractor = new LocalWorkbookDataExtractor();
            var generator     = new WorkbookGenerator(dataExtractor);

            using (var wrapper = new DisposableWrapper <ShowAsyncResult>(CWaitingWnd.ShowAsync(Title,
                                                                                               string.Format(Properties.Resources.resImportingCompetitions,
                                                                                                             SelectedComp.Name,
                                                                                                             SelectedComp.DestCompFolder),
                                                                                               this,
                                                                                               CheckAccess()),
                                                                         asyncResult =>
            {
                if (asyncResult?.hFinishedSearchEvent != null)
                {
                    asyncResult.hFinishedSearchEvent.Set();
                }
            }))
            {
                string errorMessage = null;
                if (!dataExtractor.Extract(SelectedComp, CompGroups, out errorMessage))
                {
                    MessageBox.Show(this,
                                    string.Format(Properties.Resources.resfmtCouldNotExtractDataFromRemoteDB, errorMessage),
                                    AppAttributes.Title,
                                    MessageBoxButton.OK,
                                    MessageBoxImage.Error);
                    return;
                }

                if (!generator.Generate(out errorMessage,
                                        arg =>
                                        CWaitingWnd.SetPrompt(((ShowAsyncResult)wrapper).WndID,
                                                              string.Format(Properties.Resources.resImportingCompetitionsWithGroupName,
                                                                            SelectedComp.Name,
                                                                            SelectedComp.DestCompFolder,
                                                                            arg.Key.Name))))
                {
                    MessageBox.Show(this,
                                    string.Format(Properties.Resources.resfmtCouldNotExtractDataToWbks, errorMessage),
                                    AppAttributes.Title,
                                    MessageBoxButton.OK,
                                    MessageBoxImage.Error);
                    return;
                }

                // Показываем книгу в проводнике
                Process.Start(SelectedComp.DestCompFolder);

                MessageBox.Show(this,
                                Properties.Resources.resDataIsExtractedToWbksSuccessfully,
                                AppAttributes.Title,
                                MessageBoxButton.OK,
                                MessageBoxImage.Information);
            }
        }
        public bool Extract(ICompDesc compDesc, IEnumerable <IGroupItem> compGroups, out string message)
        {
            CompDescLocalWorkbook compDescLocal = compDesc as CompDescLocalWorkbook;

            message = null;

            GroupsMembers = new List <KeyValuePair <IGroupItem, IEnumerable <CFullMemberInfo> > >();

            try
            {
                using (var excelApp = new DisposableWrapper <ExcelApplicationEx>(GlobalDefines.StartExcel(),
                                                                                 app =>
                {
                    if (app != null)
                    {
                        app.App.DisplayAlerts = true;

                        if (app.NewAppCreated)
                        {
                            app.App.Quit();
                        }

                        app = null;
                    }
                }))
                {
                    bool WbkOpened;
                    var  wbk = WorkbookGenerator.OpenWbk(excelApp, compDescLocal.SourceWorkbookName, out WbkOpened);

                    if (wbk == null)
                    {
                        message = string.Format(Properties.Resources.resfmtSourceWorkbookCouldNotBeOpened,
                                                compDescLocal.SourceWorkbookName);
                        return(false);
                    }

                    foreach (var @group in compGroups.Cast <GroupItemLocalWorkbook>())
                    {
                        MSExcel.Worksheet wsh = wbk.Worksheets[@group.SheetName];
                        MSExcel.Range     rng = wsh.Range[$"{@group.TLCell}:{@group.BRCell}"];

                        List <CFullMemberInfo> members = new List <CFullMemberInfo>();
                        for (int row = 0; row < rng.Rows.Count; row++)
                        {
                            string[] NameAndSurname;
                            GlobalDefines.CorrectSurnameAndName(rng[row + 1, @group.PersonalDataColumnIndex].Value, out NameAndSurname);

                            enGrade grade;
                            GlobalDefines.ParseGrade(rng[row + 1, @group.GradeColumnIndex].Value?.ToString(), out grade);

                            short?yearOfBirth = rng[row + 1, @group.YoBColumnIndex].Value == null
                                                    ? null
                                                    : (short?)Convert.ToUInt16(rng[row + 1, @group.YoBColumnIndex].Value);

                            members.Add(new CFullMemberInfo()
                            {
                                Surname     = NameAndSurname[0],
                                Name        = NameAndSurname[1],
                                YearOfBirth = yearOfBirth,
                                SecondCol   = rng[row + 1, @group.TeamColumnIndex].Value,
                                InitGrade   = grade == enGrade.None ? null : (byte?)grade
                            });
                        }

                        GroupsMembers.Add(new KeyValuePair <IGroupItem, IEnumerable <CFullMemberInfo> >(@group, members));
                    }
                }

                CompDesc = compDesc;
                return(true);
            }
            catch (Exception ex)
            {
                message = ex.Message;
                return(false);
            }
        }