English 中文(简体)
如何做到这一点 我在我下面的VBA代码的帮助下,用C#做两张前餐?
原标题:How Can I comapre Two excel sheet using C# With the help of VBA code that I have below?
  • 时间:2010-05-20 14:51:16
  •  标签:
  • vb.net
Dim LastRow_1 as Integer
Dim LastCol_1 as Integer
Dim Data_1 as Range
Dim LastRow_2 as Integer
Dim LastCol_2 as Integer
Dim Data_2 as Range

Dim Sh_1 as Worksheet
Dim Sh_2 as Worksheet

Dim X As Long
Dim Y As Long
Dim C_1 as Range
Dim C_2 as Range

Set Sh_1  = Activeworkbook.Sheets("Sheet1")
Set Sh_2  = Activeworkbook.Sheets("Sheet2")

LastRow_1 = Sh_1.Range("A65535").End(xlup).Row
LastCol_1 = Sh_1.Range("IV1").End(xltoLeft).Column
Set Data_1 = Sh_1.range("A1").Resize(LastRow_1,LastCol_1)

LastRow_2 = Sh_2.Range("A65535").End(xlup).Row
LastCol_2 = Sh_2.Range("IV1").End(xltoLeft).Column
Set Data_2 = Sh_2.range("A1").Resize(LastRow_2,LastCol_2)

For Each C_1 in Data_1
   For Each C_2 In Data_2
       If C_2 = C_1 then
          found a cell on sheet2 that matched cell in sheet1
          now do what you need to do
       End if
   Next C_2
Next C_1  
问题回答

select the content of the worksheet to a datatable and you may use the DataRelations object to compare the two data tables. You may also use merging of the two data tables to identify the differences.

你们可以这样做:

Excel.Application app = new Excel.Application();
app.Visible = true;
Excel.Workbooks workbooks = m_app.Workbooks;
Excel.Workbook workbook = workbooks.Open(fileName);
workbook.Activate();
Excel.Sheets sheets = workbook.Sheets;
Excel.Worksheet sheet = sheets(1);
Excel.Range range = sheet.UsedRange;

// values will be an multi dimensional array of objects
object values = range.Value;

Marshal.ReleaseComObject(range);
Marshal.ReleaseComObject(sheet);
Marshal.ReleaseComObject(sheets);
workbook.Close(false);
Marshal.ReleaseComObject(workbook);
workbooks.Close();
Marshal.ReleaseComObject(workbooks);
app.Quit();
Marshal.ReleaseComObject(app);

这片片插图,仅指其他表格的同一代码,然后,你可以比较以下数值:

AreEqual(values(1,1), values2(1, 1));

如<代码>AreEqual是你书写的一种方法,对正确核算无效和数据型等情况进行比较。

在Btw,我把上述法典写成C#,因为问题标题中所说的话,但我看到标签是VB。 净额,但如果你希望成为VB。 网上仅使用一些转换器自动转换,例如。 http://www.developerfusion.com/tools/convert/vb-to-csharp/。 或相似。





相关问题
Is Shared ReadOnly lazyloaded?

I was wondering when I write Shared ReadOnly Variable As DataType = New DataType() Or alternatively Shared ReadOnly Variable As New DataType() Is it lazy loaded or as the instance initializes? ...

Entertaining a baby with VB.NET

I would like to write a little application in VB.NET that will detect a baby s cry. How would I get started with such an application?

Choose Enter Rather than Pressing Ok button

I have many fields in the page and the last field is a dropdown with list of values. When I select an item in a dropdown and press Enter, it doesn t do the "Ok". Instead I have to manually click on Ok ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

Set Select command in code

On button Click I want to Set the Select command of a Gridview. I do this and then databind the grid but it doesn t work. What am i doing wrong? protected void bttnView_Click(object sender, ...

Hover tooltip on specific words in rich text box?

I m trying to create something like a tooltip suddenly hoovering over the mouse pointer when specific words in the richt text box is hovered over. How can this be done?

热门标签