Excel VSTO 查詢重複項

一、需求描述:EXCEL原有的重複項識別功能,在識別身份證號碼上存在識別錯誤。非重複項也識別爲重複項。

 

二、編寫Excel VSTO外接程序

1.創建新項目-Excel VSTO 外接程序,項目名:Dedupe

 

 

2.右鍵項目-添加-新建項-Office/SharePoint-功能區-添加

 

 

3.添加功能按鈕

 

4編寫功能代碼:

using Microsoft.Office.Tools.Ribbon;

using System;

using System.Collections.Generic;

using System.Drawing;

using System.Linq;

using System.Text.RegularExpressions;

using System.Windows.Forms;

using Excel = Microsoft.Office.Interop.Excel;

 

namespace Dedupe

{

    public partial class Ribbon1

    {

        public static Excel.Application App => Globals.ThisAddIn.Application;

        public static Excel.Workbook Workbook => App.ActiveWorkbook;

        public static Excel.Worksheet Worksheet => Workbook.ActiveSheet;

        public static Excel.Range SelectRange => Worksheet.Application.Selection as Excel.Range;

        private void Ribbon1_Load(object sender, RibbonUIEventArgs e)

        {

 

        }

 

        private void button1_Click(object sender, RibbonControlEventArgs e)

        {

            var range = SelectRange;

            if (range != null && range.Count > 1)

            {

                if (range.Count>1000000) {

                    MessageBox.Show("選中單元格數量過大,請分批處理") ;

                    return;

                }

                object[,] o = range.SpecialCells(Excel.XlCellType.xlCellTypeVisible).get_Value();

 

                List<string> listAll = new List<string>();

                List<string> listRepeat = new List<string>();

                foreach (var cell in o)

                {

                    if (cell != null)

                    {

                        listAll.Add(cell.ToString().Trim());

                    }

                }

                foreach (var cell in o)

                {

                    if (cell != null)

                    {

                        List<string> list = listAll.Where(q => q == cell.ToString().Trim()).ToList();

                        if (list != null && list.Count > 1)

                        {

                            if (!listRepeat.Contains(cell.ToString().Trim()))

                            {

                                listRepeat.Add(cell.ToString());

                            }

                        }

                    }

                }

                //標記重複數據

                if (listRepeat != null && listRepeat.Count > 0)

                {

                    foreach (var item in listRepeat)

                    {

                        foreach (Excel.Range cell in range.SpecialCells(Excel.XlCellType.xlCellTypeVisible))

                        {

                            int i = cell.Row;

                            int j = cell.Column;

                            if (cell.Value != null && (Convert.ToString(cell.Value)).Trim() == item)

                            {

                                cell.Interior.Color = ColorTranslator.ToOle(Color.OrangeRed);

                            }

                        }

 

                    }

                }

            }

            else

            {

                MessageBox.Show("請選擇一個以上的單元格");

                return;

            }

        }

 

        private void button2_Click(object sender, RibbonControlEventArgs e)

        {

            var range = SelectRange;

 

            object[,] o = range.SpecialCells(Excel.XlCellType.xlCellTypeVisible).get_Value();

            if (o != null)

            {

                foreach (Excel.Range cell in range.SpecialCells(Excel.XlCellType.xlCellTypeVisible))

                {

                    cell.Interior.ColorIndex = 0;

                }

            }

            else

            {

                MessageBox.Show("請選擇一個或以上的單元格");

                return;

            }

        }

 

        private void button3_Click(object sender, RibbonControlEventArgs e)

        {

            var range = SelectRange;

            if (range != null && range.Count > 0)

            {

                object[,] o = range.SpecialCells(Excel.XlCellType.xlCellTypeVisible).get_Value();

                if (o != null)

                {

                    List<string> listAll = new List<string>();

                    List<string> listError = new List<string>();

                    foreach (var cell in o)

                    {

                        if (cell != null)

                        {

                            listAll.Add(cell.ToString().Trim());

                        }

                    }

                    foreach (var cell in listAll)

                    {

 

                        if (!listError.Contains(cell))

                        {

                            if (!Regex.Match(cell, "^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{4}$").Success)

                            {

                                listError.Add(cell);

                            }

                        }

 

                    }

                    //標記錯誤數據

                    if (listError != null && listError.Count > 0)

                    {

                        foreach (var item in listError)

                        {

                            foreach (Excel.Range cell in range.SpecialCells(Excel.XlCellType.xlCellTypeVisible))

                            {

                                int i = cell.Row;

                                int j = cell.Column;

                                if (cell.Value != null && (Convert.ToString(cell.Value)).Trim() == item)

                                {

                                    cell.Interior.Color = ColorTranslator.ToOle(Color.Yellow);

                                }

                            }

 

                        }

                    }

                }

                else

                {

                    MessageBox.Show("請選擇一個或以上的單元格");

                }

            }

            else

            {

                MessageBox.Show("請選擇一個或以上的單元格");

            }

        }

    }

}

 

4.右鍵項目發佈(F5可以直接調試)

5.安裝setup.exe

 

6.打開EXCEL,先打開EXCEL功能,勾選確認:文件-選項-自定義功能區-開發工具/加載項

 

 

7.加載項查看功能

 

8.開發工具裏面卸載

 

 

 

三、實際效果

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章