我如何在.Net2.0中比较两个Excel工作表?

发布于 2024-11-15 04:54:46 字数 1476 浏览 1 评论 0原文

我有两个文本文件(这是一个逗号分隔的文件)。这两个文件的模板如下。

            SD,CurrentDate,RecordCount
    NI,FirstName,LastName,Place,Language
    EQ,Degree,University,Year,Aggregate
    ED,CurrentDate,RecordCount

第一个文件中的数据 - one.txt

          SD,13/06/2010,6
          NI,Rajesh,kumar,xxxx,english
          EQ,X,Stateboard,2004,75
          EQ,XII,Stateboard,2006,85
          EQ,B.E,Oxford,2008,79
             ED,13/06/2010,6

第二个文件中的数据 - Second.txt

          SD,13/06/2010,6
          NI,Rajesh,kumar,,english
          EQ,X,,2004,75
          EQ,XII,Stateboard,2006,
          EQ,,Oxford,2008,79
          ED,13/06/2010,6

现在我已将 one.txt 的值填充到 Excel 工作表(Output.xls)的“sheet1”中,然后我填充

了使用.Net代码将Second.txt的值添加到Excel工作表(Output.xls)的“sheet2”中。

现在我想比较两个工作表数据并填充“sheet3”中的差异。

“Sheet3”的 o/p 将会有。

    Cell1       Cell2           Cell3           Cell4       Cell5
    SD:True     CurrentDate:True    RecordCount:True
    NI:True     FirstName:True      LastName:True       Place:False   Language:True
    EQ:True     Degree:True     University:False    Year:True   Aggregate:True
    EQ:True     Degree:True     University:True     Year:True   Aggregate:False
    EQ:True     Degree:False        University:True     Year:True   Aggregate:True
    SD:True     CurrentDate:True    RecordCount:True

我如何比较这两张纸?通过VBA可以吗?我可以在.Net中调用VBA代码吗?请有人给我解决方案吗?

I am having two text files ( which is a comma separated file).The template of the two file is given below.

            SD,CurrentDate,RecordCount
    NI,FirstName,LastName,Place,Language
    EQ,Degree,University,Year,Aggregate
    ED,CurrentDate,RecordCount

The Data in the first file - one.txt

          SD,13/06/2010,6
          NI,Rajesh,kumar,xxxx,english
          EQ,X,Stateboard,2004,75
          EQ,XII,Stateboard,2006,85
          EQ,B.E,Oxford,2008,79
             ED,13/06/2010,6

The Data in the Second file - Second.txt

          SD,13/06/2010,6
          NI,Rajesh,kumar,,english
          EQ,X,,2004,75
          EQ,XII,Stateboard,2006,
          EQ,,Oxford,2008,79
          ED,13/06/2010,6

Now I have populted the value of the one.txt to the "sheet1" of Excel sheet(Output.xls) and then i have populated

the value of the Second.txt to the "sheet2" of Excel sheet(Output.xls) by using the .Net code.

Now i want to compare two sheets data and populte the difference in the "sheet3".

The o/p of the "Sheet3" will have.

    Cell1       Cell2           Cell3           Cell4       Cell5
    SD:True     CurrentDate:True    RecordCount:True
    NI:True     FirstName:True      LastName:True       Place:False   Language:True
    EQ:True     Degree:True     University:False    Year:True   Aggregate:True
    EQ:True     Degree:True     University:True     Year:True   Aggregate:False
    EQ:True     Degree:False        University:True     Year:True   Aggregate:True
    SD:True     CurrentDate:True    RecordCount:True

How can i compare this two sheets? Is it possible through VBA? Can i able to call the VBA code in .Net?Please anyone provide me the solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

莳間冲淡了誓言ζ 2024-11-22 04:54:46

您似乎想在手动或夸张地填充值后比较 2 张纸。在这种情况下,以下代码将完成这项工作,但您必须修改将在布尔值之前插入列名称的部分

Dim objEX as new Excel.Application
    objEX.Visible = True
    ' Optional if you want to see what is
    ' going on in EXCEL while your code is being executed.


objEX.Workbooks.Open "C:\My Files\Filename.xls"
'Make sure you put the right path of the excel workbook you want to open

With objEX
    .Sheets(1).Activate
    .Range("a1").Select

    .Sheets(2).Activate
    .Range("a1").Select

    .Sheets(3).Activate
    .Range("a1").Select


    'assuming the populated data starts at Cell A1
    For i = 0 To 6      'because you have 6 rows
        For j = 0 To 5  'because you have 5 columns
            If .Sheets(1).ActiveCell.Offset(i, j).Value = .Sheets(2).ActiveCell.Offset(i, j).Value Then
                .Sheets(3).ActiveCell.Offset(i, j).Value = "True"
            Else
                .Sheets(3).ActiveCell.Offset(i, j).Value = "False"
            End If
        Next
    Next


    .ActiveWorkbook.Save
    'Saves the changes you have done

    .ActiveWorkbook.Close
    'Closes the workbook


    .Quit
    'Quits excel instance and releases the process from task manager

End With

    Set objEX = Nothing
    'Garbage Collection and making sure memory is released to other processes.

It seems that you would like to compare 2 sheets after you have manually or melodramatically populated the values to. In this case the following code will do the job, but you have to modify the parts that will insert the column name before the boolean value

Dim objEX as new Excel.Application
    objEX.Visible = True
    ' Optional if you want to see what is
    ' going on in EXCEL while your code is being executed.


objEX.Workbooks.Open "C:\My Files\Filename.xls"
'Make sure you put the right path of the excel workbook you want to open

With objEX
    .Sheets(1).Activate
    .Range("a1").Select

    .Sheets(2).Activate
    .Range("a1").Select

    .Sheets(3).Activate
    .Range("a1").Select


    'assuming the populated data starts at Cell A1
    For i = 0 To 6      'because you have 6 rows
        For j = 0 To 5  'because you have 5 columns
            If .Sheets(1).ActiveCell.Offset(i, j).Value = .Sheets(2).ActiveCell.Offset(i, j).Value Then
                .Sheets(3).ActiveCell.Offset(i, j).Value = "True"
            Else
                .Sheets(3).ActiveCell.Offset(i, j).Value = "False"
            End If
        Next
    Next


    .ActiveWorkbook.Save
    'Saves the changes you have done

    .ActiveWorkbook.Close
    'Closes the workbook


    .Quit
    'Quits excel instance and releases the process from task manager

End With

    Set objEX = Nothing
    'Garbage Collection and making sure memory is released to other processes.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文