Exemplo n.º 1
0
 private void comboBoxSSheetName_SelectedIndexChanged(object sender, EventArgs e)
 {
     if (checkBoxCompareSingle.Checked)
     {
         ExcelBook E = new ExcelBook(m_cfg.XlsName);
         DataTable S = null, L = null;
         try
         {
             if (!checkBoxRedTitle.Checked)
             {
                 ExcelSingleSheet ES = new ExcelSingleSheet(E.ExcelSheet(comboBoxSSheetName.SelectedItem.ToString()));
                 if (!ES.CanCheckSingleSheet(ref S, ref L))
                 {
                     MessageBox.Show(ES.Msg);
                     return;
                 }
             }
             else
             {
                 ExcelSingleRedSheet ES = new ExcelSingleRedSheet(E.ExcelSheet(comboBoxSSheetName.SelectedItem.ToString()));
                 if (!ES.CanCheckSingleSheet(ref S, ref L, checkBoxMultiBlackSkip.Checked))
                 {
                     MessageBox.Show(ES.Msg);
                     return;
                 }
             }
             dgvL.DataSource = L;
             dgvS.DataSource = S;
         }
         finally
         {
             E = null;
             ExcelBook.excel.Workbooks.Close();
         }
     }
 }
Exemplo n.º 2
0
        private void CompareSingleSheetClick()
        {
            DataTable DL = (DataTable)dgvL.DataSource;
            DataTable DS = (DataTable)dgvS.DataSource;

            if (DL == null || DS == null || DL.Columns.Count != DS.Columns.Count)
            {
                return;
            }
            for (int i = 0; i < DL.Columns.Count; i++)
            {
                if (DL.Columns[i].ColumnName != DS.Columns[i].ColumnName)
                {
                    MessageBox.Show("两边待比较的表头不一致");
                    return;
                }
            }

            ExcelBook        E = new ExcelBook(m_cfg.XlsName);
            ExcelSingleSheet ES = new ExcelSingleSheet(E.ExcelSheet(comboBoxSSheetName.SelectedItem.ToString()));
            ExcelSheetTable  S = null, L = null;


            try
            {
                if (!ES.CanCheckSingleSheet(ref S, ref L))
                {
                    return;
                }

                if (checkBoxClearBackColor.Checked)
                {
                    S.ClearBackColor();
                    L.ClearBackColor();
                }
                CompareTable(S, L);
                if (checkBoxTagL.Checked || checkBoxTagS.Checked)
                {
                    if (LSpecial.Count > 0 || SSpecial.Count > 0)
                    {
                        ES.FormatExcelRange(SSpecial, _colorSpe, "特有", S.TagCol);
                        ES.FormatExcelRange(LSpecial, _colorSpe, "特有", L.TagCol);
                    }
                }
                if (checkBoxCopyDuplicationL.Checked || checkBoxCopyDuplicationS.Checked)
                {
                    if (LDicDuplication.Count > 0 || SDicDuplication.Count > 0)
                    {
                        List <int> LDuplication = new List <int>();
                        List <int> SDuplication = new List <int>();
                        foreach (List <int> l in LDicDuplication.Values)
                        {
                            LDuplication.AddRange(l);
                        }
                        foreach (List <int> l in SDicDuplication.Values)
                        {
                            SDuplication.AddRange(l);
                        }

                        List <Point> SD = SDuplication.Select(r => new Point(r, S.NameCol)).ToList();
                        List <Point> LD = LDuplication.Select(r => new Point(r, L.NameCol)).ToList();
                        ES.FormatExcelRange(SD, _colorDup, "重复", S.TagCol);
                        ES.FormatExcelRange(LD, _colorDup, "重复", L.TagCol);
                    }
                }
                if (checkBoxDifL.Checked || checkBoxDifS.Checked)
                {
                    if (LP.Count > 0 || SP.Count > 0)
                    {
                        ES.FormatExcelRange(SP, _colorDif, "不同", S.TagCol + 1);
                        ES.FormatExcelRange(LP, _colorDif, "不同", L.TagCol + 1);
                    }
                }

                for (int i = 0; i < _Same.Count; i++)
                {
                    if (_Same[i].Count == 2)
                    {
                        Cells c = _Same[i][0];
                        ES.SortExcelRange(new Point(c.Row, c.Col), i, S.TagCol);
                        c = _Same[i][1];
                        ES.SortExcelRange(new Point(c.Row, c.Col), i, L.TagCol);
                    }
                }
                /////
                ES.SetExcelRangeTag(1, "比较结果", S.TagCol);
                ES.SetExcelRangeTag(1, "比较结果", L.TagCol);
            }
            catch (Exception ex)
            {
                MessageBox.Show("程序有点小问题\r\n" + ex.Message);
            }
            finally
            {
                S = null;
                L = null;
                ExcelBook.excel.ActiveWorkbook.Save();
                ExcelBook.excel.Workbooks.Close();
            }
        }