2011-06-13 26 views
0

我有兩個文本文件(這是一個逗號分隔文件)。這兩個文件的模板如下。如何比較.Net2.0中的兩個Excel表單?

  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 

現在我已經populted了一個價值.txt添加到Excel工作表(Output.xls)的「sheet1」中,然後我填充了

Second.txt的值通過使用.Net代碼的Excel工作表(Output.xls)的「工作表2」。

現在我想比較兩張數據,並在「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代碼嗎?請有人給我提供解決方案嗎?

+0

當然,它會更容易/效率比較從加載的原始數據的部分2個文本文件而不是迭代2個電子表格? – 2011-06-13 12:17:22

+0

@ Alex.K:通過使用.Net,我已經填充了兩張紙的值。可以通過使用.Net – 2011-06-13 12:21:00

+0

@ Alex.K使用VBA代碼:U – 2011-06-13 12:29:27

回答

0

看起來你想比較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.