Пример #1
0
        public static void Add(string lb, int row, int column, string old, string my, bool myFormula, string their, bool theirFormula, bool conflict, string result)
        {
            MergeEvtData evtData = new MergeEvtData();

            evtData.Label          = lb;
            evtData.Row            = row;
            evtData.Column         = column;
            evtData.OldValue       = old;
            evtData.MyValue        = my;
            evtData.MyFormula      = myFormula;
            evtData.TheirsValue    = their;
            evtData.TheirFormula   = theirFormula;
            evtData.Conflict       = conflict;
            evtData.ConflictResult = result;
            evtData.AutoMerge      = !conflict;
            DataList.Add(evtData);
        }
Пример #2
0
        public static string BeginMerge()
        {
            try
            {
                dtBase   = LoadData(ProArgs.Base, SheetMetaManager.AddBase, false, out _);
                dtTheirs = LoadData(ProArgs.Theirs, SheetMetaManager.AddTheir, false, out _);
                dtMine   = LoadData(ProArgs.Mine, SheetMetaManager.AddMine, true, out epMine); //这一次读文件其实可以省掉,但代码更难写
            }
            catch (Exception e)
            {
                return("文件初始化失败" + e.Message);
            }

            MarkRowState(dtTheirs, dtBase, dtMine);
            MarkRowState(dtMine, dtBase, dtTheirs);

            DetectRowChange();

            var compareResult = SheetMetaManager.CompareBaseAndTheir();

            if (compareResult != "")
            {
                return(compareResult);
            }

            {
                var workbook = epMine.Workbook;

                for (int i = 1; i <= workbook.Worksheets.Count; i++)
                {
                    var sheetIn  = workbook.Worksheets[i];
                    int colCount = 0;
                    if (sheetIn.Dimension != null)
                    {
                        colCount = sheetIn.Dimension.End.Column;
                        for (int col = 2; col <= colCount; col++)
                        {
                            var dt = sheetIn.GetValue(9, col);
                            if (dt == null)
                            {
                                colCount = col;
                                break;
                            }
                        }
                    }

                    bool isRegular = colCount > 0 && sheetIn.Dimension.End.Row >= 13 &&
                                     sheetIn.Cells[13, 1].Text == "BEGIN";
                    if (!isRegular)
                    {
                        continue;
                    }

                    var compareResult2 = SheetMetaManager.AddAndCompareMine(i - 1, sheetIn.Name, colCount);
                    if (compareResult2 != "")
                    {
                        return(compareResult2);
                    }

                    for (int row = 14; row <= sheetIn.Dimension.End.Row; row++)
                    {
                        if (row >= 14 && sheetIn.GetValue(row, 2) == null)
                        {
                            break;
                        }

                        string idStr = "";
                        for (int col = 2; col <= colCount; col++)
                        {
                            var cell = sheetIn.Cells[row, col];
                            if (cell != null)
                            {
                                if (col == 2)
                                {
                                    idStr = cell.Text;
                                }
                                var  myCellContent = cell.Text;
                                bool myFormula     = false;
                                if (!string.IsNullOrEmpty(cell.Formula))
                                {
                                    myCellContent = cell.Formula;
                                    myFormula     = true;
                                }

                                string idKey = string.Format("{0}-key={1}", sheetIn.Name, idStr);
                                if (!dtTheirs.ContainsKey(idKey) || dtTheirs[idKey].Tag == "Add" || dtTheirs[idKey].Tag == "Delete" || dtMine[idKey].Tag == "Add")
                                {
                                    // 增减行的情况,这里不处理
                                    break;
                                }
                                var baseCell   = dtBase[idKey].Datas[col];
                                var theirsCell = dtTheirs[idKey].Datas[col];

                                if (myCellContent != baseCell.Content || myCellContent != theirsCell.Content)
                                {
                                    bool   conflict       = false;
                                    string conflictResult = "";
                                    if (myCellContent == baseCell.Content && baseCell.Content != theirsCell.Content)
                                    {
                                        // 自动解决冲突,用别人的值
                                        // UpdateInner(sheetIn, row, col, theirsCell.Content, theirsCell.IsFormula);
                                        conflictResult = theirsCell.Content;
                                    }
                                    else if (myCellContent != baseCell.Content && baseCell.Content != theirsCell.Content && myCellContent != theirsCell.Content)
                                    {
                                        conflict = true;
                                    }
                                    else
                                    {
                                        // 自动解决冲突,用自己的值
                                        conflictResult = myCellContent;
                                    }
                                    MergeEvtData.Add(idKey, row, col, baseCell.Content, myCellContent, myFormula, theirsCell.Content, theirsCell.IsFormula, conflict, conflictResult);
                                }
                            }
                        }
                    }
                }
            }
            return("");
        }