コード例 #1
0
        public double Similarity(ExelRow exelRow, int maxColumnsCount = 0)
        {
            double result     = 0.0;
            bool   wasError   = false;
            var    logSession = Helpers.Old.Log.SessionStart("ExelRow.Similarity()", true);

            try
            {
                if (maxColumnsCount == 0)
                {
                    maxColumnsCount = int.MaxValue;
                }

                //if (exelRow.Cells.Count == this.Cells.Count)
                //{

                int maxValue = Math.Min(Math.Min(exelRow.Cells.Count, this.Cells.Count), maxColumnsCount);

                if (maxValue > 0)
                {
                    for (int i = 0; i < maxValue; i++)
                    {
                        result += exelRow.Cells[i].IsMerged == this.Cells[i].IsMerged ? 0.09 : 0;
                        result += string.IsNullOrEmpty(exelRow.Cells[i].Value) == string.IsNullOrEmpty(this.Cells[i].Value) ? 0.44 : 0.0;
                        result += Math.Abs(exelRow.Cells[i].UniqueWeight - this.Cells[i].UniqueWeight) < 0.07 ? 0.15 : 0.0;
                        result += AsyncDocumentLoader.ColorsEqual(exelRow.Cells[i].CellStyle.ForegroundColor, this.Cells[i].CellStyle.ForegroundColor) ? 0.16 : 0.0;
                        result += AsyncDocumentLoader.ColorsEqual(exelRow.Cells[i].CellStyle.BackgroundColor, this.Cells[i].CellStyle.BackgroundColor) ? 0.16 : 0.0;
                    }
                }
                result = result / maxValue;


                //}

                if (maxColumnsCount == int.MaxValue)
                {
                    result  = result * 0.55;
                    result += ((double)Math.Min(this.NotEmptyCells.Count(), exelRow.NotEmptyCells.Count()) / (double)Math.Max(this.NotEmptyCells.Count(), exelRow.NotEmptyCells.Count())) * 0.4;

                    //result += (this.NotEmptyCells.Count() == exelRow.NotEmptyCells.Count()) ? 0.4 : 0;
                    result += (this.UniqueNotEmptyCells.Count() == exelRow.UniqueNotEmptyCells.Count()) ? 0.05 : 0;
                }
            }
            catch (Exception ex)
            {
                wasError = true;
                Helpers.Old.Log.Add(logSession, ex);
                throw ex;
            }
            finally
            {
                Helpers.Old.Log.SessionEnd(logSession, wasError);
            }
            return(result);
        }
コード例 #2
0
ファイル: ExelDocument.cs プロジェクト: radtek/ExcelConverter
        private void Load()
        {
            if (loadWorker != null)
            {
                try
                {
                    loadWorker.CancelAsync();
                }
                catch { }
            }

            LoadingProgress  = 0;
            IsDocumentLoaded = false;
            Error            = string.Empty;

            if (string.IsNullOrWhiteSpace(Path))
            {
                return;
            }

            IsBusy     = true;
            loadWorker = new BackgroundWorker()
            {
                WorkerReportsProgress = true, WorkerSupportsCancellation = true
            };

            var init = new AsyncLoadInit()
            {
                DeleteEmptyRows = this.DeleteEmptyRows, WorkBookFilePath = this.Path, PreloadCount = Settings.SettingsProvider.CurrentSettings.PreloadedRowsCount
            };

            var applyRes = new Action <Workbook, ExelSheet[]>((w, s) =>
            {
                WorkBook = w;
                DocumentSheets.Clear();
                var oldSheetName = (SelectedSheet == null ? string.Empty : SelectedSheet.Name) ?? string.Empty;
                foreach (var s2 in s)
                {
                    DocumentSheets.Add(s2);
                }
                SelectedSheet = DocumentSheets.FirstOrDefault(ss => ss.Name == oldSheetName) ?? DocumentSheets.FirstOrDefault();
            });

            loadWorker.DoWork += (s, e) =>
            {
                Thread.CurrentThread.CurrentCulture   = CultureInfo.GetCultureInfo("ru-RU");
                Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;

                var bw   = (BackgroundWorker)s;
                var prms = (AsyncLoadInit)e.Argument;
                var res  = new AsyncLoadResult()
                {
                    StartSettings = prms
                };

                var pp = new Helpers.PercentageProgress();
                pp.Change += (sP, eP) => { bw.ReportProgress((int)eP.Value); };

                var pp0 = pp.GetChild();
                pp0.Weight = 1;
                var pp1 = pp.GetChild();
                pp1.Weight = 3;
                var pp2 = pp.GetChild();
                pp2.Weight = 6;
                //var pp3 = pp.GetChild();
                //pp3.Weight = 3;

                bw.ReportProgress((int)pp.Value, "Открытие документа...");

                res.WorkBook = new Workbook(prms.WorkBookFilePath);
                pp0.Value    = 100;

                bw.ReportProgress((int)pp.Value, "Чтение первых записей на страницах...");

                var subRes = AsyncDocumentLoader.LoadSheets(res.WorkBook, prms.PreloadCount, (i) => { pp1.Value = i; }, prms.DeleteEmptyRows).ToArray();
                bw.ReportProgress((int)pp.Value, new AsyncLoadResult()
                {
                    WorkBook = res.WorkBook, WorkSheets = subRes
                });

                bw.ReportProgress((int)pp.Value, "Чтение всех записей на страницах...");

                res.WorkSheets = AsyncDocumentLoader.LoadSheets(res.WorkBook, 0, (i) => { pp2.Value = i; }, prms.DeleteEmptyRows).ToArray();

                //foreach (var ws in res.WorkSheets.Select(w => new { WorkSheet = w, Progress = pp3.GetChild() }).ToArray())
                //{
                //    ws.WorkSheet.UpdateHeaders();

                //}

                bw.ReportProgress((int)pp.Value, "Применение результата...");

                e.Result = res;
            };
            loadWorker.ProgressChanged += (s, e) =>
            {
                if (loadWorker != s)
                {
                    return;
                }

                LoadingProgress = e.ProgressPercentage;
                var res = e.UserState as AsyncLoadResult;
                if (res != null)
                {
                    applyRes(res.WorkBook, res.WorkSheets);
                }
                else
                {
                    var status = e.UserState as string;
                    if (status != null)
                    {
                        Status = status;
                    }
                }
            };
            loadWorker.RunWorkerCompleted += (s, e) =>
            {
                if (loadWorker == s)
                {
                    if (e.Cancelled)
                    {
                        Error = "Задание отменено пользователем";
                    }

                    if (e.Error != null)
                    {
                        Error = e.Error.GetExceptionText();
                    }
                    else
                    {
                        var res = e.Result as AsyncLoadResult;
                        if (res != null)
                        {
                            if (res.WorkSheets.Count() == 0)
                            {
                                Error = "В документе не найдено ни одного листа";
                            }
                            Status = "Применение результатов (подсчёт заголовков и прочее)...";
                            applyRes(res.WorkBook, res.WorkSheets);
                        }
                        else
                        {
                            Error = "Внутренняя ошибка приложения";
                        }
                    }
                    Status = "Загрузка документа завершена" + (string.IsNullOrWhiteSpace(Error) ? string.Empty : " с ошибками");

                    LoadingProgress  = 100;
                    IsDocumentLoaded = true;
                    IsBusy           = false;
                    loadWorker       = null;
                }
                ((BackgroundWorker)s).Dispose();
            };
            loadWorker.RunWorkerAsync(init);
        }