我有两个带有一些联系号码的Excel文件, [英] I Have two Excel files having some contact numbers,

查看:68
本文介绍了我有两个带有一些联系号码的Excel文件,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个带有一些联系号码的Excel文件,两个excel表中的名称相似。我想要两个excel表中具有相同名称的号码,C#中的程序。请任何人回复此帖。

I Have two Excel files having some contact numbers,Some names are similar in both excel sheets.i want the number which is having same name in both excel sheets ,program in C#..pls anybody reply to this.

推荐答案

下面的方法比较同一工作簿中的2个工作表并显示相似的数据。



The below method compares 2 worksheets in the same workbook and displays the data which are similar.

void CompareExcel()
        {

            string filePath = @"F:\Book2.xlsx";
 
            Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbook xlWorkBook;

            try
            {
                xlWorkBook = xlApp.Workbooks.Open(filePath, 0, true, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", true, false, 0, true, 0);

                Microsoft.Office.Interop.Excel.Worksheet worksheet1 = (Microsoft.Office.Interop.Excel.Worksheet)xlApp.Worksheets["Sheet1"];
                Microsoft.Office.Interop.Excel.Worksheet worksheet2 = (Microsoft.Office.Interop.Excel.Worksheet)xlApp.Worksheets["Sheet2"];

                int sheet1LastRowCount = worksheet1.UsedRange.Rows.Count;
                int sheet2LastRowCount = worksheet2.UsedRange.Rows.Count;

                for (int i = 2; i < sheet1LastRowCount; i++)
                {
                    for (int j = 2; j < sheet2LastRowCount; j++)
                    {
                        if (worksheet1.Range["B" + i, "B" + i].Value2 == worksheet2.Range["B" + j, "B" + j].Value2)
                        {
                            Console.WriteLine(worksheet1.Range["A" + i, "A" + i].Value2);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                xlApp.Quit();
            }

            
        }


这篇关于我有两个带有一些联系号码的Excel文件,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆