比较两个excel文件并将差异放在新工作表中

时间:2015-05-12 15:22:54

标签: c# vb.net

我正在比较两张这样的表:

excel sheets

一张纸比另一张更新,可以删除,添加或更新一行。

我的代码检测是否在两者之间删除/添加了一行对我不起作用,这就是我所拥有的:

Private Sub CompareSpreadsheets()
    Dim xlApp1 As Excel.Application = New Microsoft.Office.Interop.Excel.Application()
    Dim xlApp2 As Excel.Application = New Microsoft.Office.Interop.Excel.Application()
    Dim xlApp3 As Excel.Application = New Microsoft.Office.Interop.Excel.Application()
    Dim xlWorkBook1, xlWorkBook2, xlWorkBook3 As Excel.Workbook
    Dim xlWorkSheet1, xlWorkSheet2, xlWorkSheet3 As Excel.Worksheet
    Dim misValue As Object = System.Reflection.Missing.Value
    Dim xlRange1 As Excel.Range
    Dim maxRows As Short
    Dim qtyChange, mtlChange, newItem, delItem As Boolean
    Dim newRowCount As Short = 1
    Dim w3row As Short = 1

    'Check if Excel is installed on system
    If xlApp1 Is Nothing Then
        MessageBox.Show("Excel is missing/not properly installed on your system.", "ERROR")
        Return
    End If

    xlApp1.Workbooks.Open(path1)
    xlWorkBook1 = xlApp1.ActiveWorkbook
    xlWorkSheet1 = xlWorkBook1.ActiveSheet

    xlApp2.Workbooks.Open(path2)
    xlWorkBook2 = xlApp2.ActiveWorkbook
    xlWorkSheet2 = xlWorkBook2.ActiveSheet

    'Get maximum number of columns and rows from new spreadsheet
    maxRows = xlWorkSheet2.UsedRange.Rows.Count

    pbStatus.Minimum = 1
    pbStatus.Maximum = maxRows

    xlWorkBook3 = xlApp3.Workbooks.Add(misValue)
    xlWorkSheet3 = xlWorkBook3.Sheets("sheet1")

    qtyChange = False
    mtlChange = False
    newItem = False
    delItem = False

    Dim counter = 0 '/temp

    For i As Short = 1 To maxRows
        pbStatus.Value = i

        '[-- Green = Quantity change
        'Yellow = Material change
        'Blue = New item
        'Red = Deleted item --]

        If GetCellData(xlWorkSheet1, newRowCount, 3) = GetCellData(xlWorkSheet2, i, 3) And _
            GetCellData(xlWorkSheet1, newRowCount, 4) = GetCellData(xlWorkSheet2, i, 4) And _
            GetCellData(xlWorkSheet1, newRowCount, 5) = GetCellData(xlWorkSheet2, i, 5) And _
            GetCellData(xlWorkSheet1, newRowCount, 6) = GetCellData(xlWorkSheet2, i, 6) And _
            GetCellData(xlWorkSheet1, newRowCount, 7) = GetCellData(xlWorkSheet2, i, 7) And _
            GetCellData(xlWorkSheet1, newRowCount, 8) = GetCellData(xlWorkSheet2, i, 8) Then
            'DONE
            qtyChange = False
            mtlChange = False
            newItem = False
            delItem = False
            newRowCount += 1
        ElseIf GetCellData(xlWorkSheet1, newRowCount, 3) = GetCellData(xlWorkSheet2, i, 3) And _
            GetCellData(xlWorkSheet1, newRowCount, 4) = GetCellData(xlWorkSheet2, i, 4) And _
            GetCellData(xlWorkSheet1, newRowCount, 5) = GetCellData(xlWorkSheet2, i, 5) And _
            GetCellData(xlWorkSheet1, newRowCount, 6) <> GetCellData(xlWorkSheet2, i, 6) And _
            GetCellData(xlWorkSheet1, newRowCount, 7) = GetCellData(xlWorkSheet2, i, 7) And _
            GetCellData(xlWorkSheet1, newRowCount, 8) = GetCellData(xlWorkSheet2, i, 8) Then
            'DONE
            qtyChange = True
        ElseIf GetCellData(xlWorkSheet1, newRowCount, 1) = GetCellData(xlWorkSheet2, i, 1) And _
            GetCellData(xlWorkSheet1, newRowCount, 2) = GetCellData(xlWorkSheet2, i, 2) And _
            GetCellData(xlWorkSheet1, newRowCount, 3) = GetCellData(xlWorkSheet2, i, 3) And _
            GetCellData(xlWorkSheet1, newRowCount, 4) <> GetCellData(xlWorkSheet2, i, 4) And _
            GetCellData(xlWorkSheet1, newRowCount, 5) = GetCellData(xlWorkSheet2, i, 5) And _
            GetCellData(xlWorkSheet1, newRowCount, 6) = GetCellData(xlWorkSheet2, i, 6) And _
            GetCellData(xlWorkSheet1, newRowCount, 7) = GetCellData(xlWorkSheet2, i, 7) And _
            GetCellData(xlWorkSheet1, newRowCount, 8) = GetCellData(xlWorkSheet2, i, 8) Then
            'DONE
            mtlChange = True
        ElseIf GetCellData(xlWorkSheet1, newRowCount, 3) <> GetCellData(xlWorkSheet2, i, 3) And _
            GetCellData(xlWorkSheet1, newRowCount, 4) <> GetCellData(xlWorkSheet2, i, 4) Then

            If GetCellData(xlWorkSheet1, newRowCount + 1, 4) <> GetCellData(xlWorkSheet2, i, 4) Then
                newItem = True
            ElseIf GetCellData(xlWorkSheet1, newRowCount, 4) <> GetCellData(xlWorkSheet2, i + 1, 4) Then
                delItem = True
            End If
        End If

        'Procedure for each case
        If qtyChange Then
            xlWorkSheet3.Cells(w3row, 1) = GetCellData(xlWorkSheet2, i, 1)
            xlRange1 = CType(xlWorkSheet3.Cells(w3row, 1), Excel.Range)
            xlRange1.Font.Color = System.Drawing.Color.Green

            xlWorkSheet3.Cells(w3row, 2) = GetCellData(xlWorkSheet2, i, 2)
            xlRange1 = CType(xlWorkSheet3.Cells(w3row, 2), Excel.Range)
            xlRange1.Font.Color = System.Drawing.Color.Green

            xlWorkSheet3.Cells(w3row, 3) = GetCellData(xlWorkSheet2, i, 3)
            xlRange1 = CType(xlWorkSheet3.Cells(w3row, 3), Excel.Range)
            xlRange1.Font.Color = System.Drawing.Color.Green

            xlWorkSheet3.Cells(w3row, 4) = GetCellData(xlWorkSheet2, i, 4)
            xlRange1 = CType(xlWorkSheet3.Cells(w3row, 4), Excel.Range)
            xlRange1.Font.Color = System.Drawing.Color.Green

            xlWorkSheet3.Cells(w3row, 5) = GetCellData(xlWorkSheet2, i, 5)
            xlRange1 = CType(xlWorkSheet3.Cells(w3row, 5), Excel.Range)
            xlRange1.Font.Color = System.Drawing.Color.Green

            xlWorkSheet3.Cells(w3row, 6) = GetCellData(xlWorkSheet2, i, 6)
            xlRange1 = CType(xlWorkSheet3.Cells(w3row, 6), Excel.Range)
            xlRange1.Font.Color = System.Drawing.Color.Green

            xlWorkSheet3.Cells(w3row, 7) = GetCellData(xlWorkSheet2, i, 7)
            xlRange1 = CType(xlWorkSheet3.Cells(w3row, 7), Excel.Range)
            xlRange1.Font.Color = System.Drawing.Color.Green

            xlWorkSheet3.Cells(w3row, 8) = GetCellData(xlWorkSheet2, i, 8)
            xlRange1 = CType(xlWorkSheet3.Cells(w3row, 8), Excel.Range)
            xlRange1.Font.Color = System.Drawing.Color.Green

            newRowCount += 1
            w3row += 1
        ElseIf mtlChange Then
            xlWorkSheet3.Cells(w3row, 1) = GetCellData(xlWorkSheet2, i, 1)
            xlRange1 = CType(xlWorkSheet3.Cells(w3row, 1), Excel.Range)
            xlRange1.Font.Color = System.Drawing.Color.Yellow

            xlWorkSheet3.Cells(w3row, 2) = GetCellData(xlWorkSheet2, i, 2)
            xlRange1 = CType(xlWorkSheet3.Cells(w3row, 2), Excel.Range)
            xlRange1.Font.Color = System.Drawing.Color.Yellow

            xlWorkSheet3.Cells(w3row, 3) = GetCellData(xlWorkSheet2, i, 3)
            xlRange1 = CType(xlWorkSheet3.Cells(w3row, 3), Excel.Range)
            xlRange1.Font.Color = System.Drawing.Color.Yellow

            xlWorkSheet3.Cells(w3row, 4) = GetCellData(xlWorkSheet2, i, 4)
            xlRange1 = CType(xlWorkSheet3.Cells(w3row, 4), Excel.Range)
            xlRange1.Font.Color = System.Drawing.Color.Yellow

            xlWorkSheet3.Cells(w3row, 5) = GetCellData(xlWorkSheet2, i, 5)
            xlRange1 = CType(xlWorkSheet3.Cells(w3row, 5), Excel.Range)
            xlRange1.Font.Color = System.Drawing.Color.Yellow

            xlWorkSheet3.Cells(w3row, 6) = GetCellData(xlWorkSheet2, i, 6)
            xlRange1 = CType(xlWorkSheet3.Cells(w3row, 6), Excel.Range)
            xlRange1.Font.Color = System.Drawing.Color.Yellow

            xlWorkSheet3.Cells(w3row, 7) = GetCellData(xlWorkSheet2, i, 7)
            xlRange1 = CType(xlWorkSheet3.Cells(w3row, 7), Excel.Range)
            xlRange1.Font.Color = System.Drawing.Color.Yellow

            xlWorkSheet3.Cells(w3row, 8) = GetCellData(xlWorkSheet2, i, 8)
            xlRange1 = CType(xlWorkSheet3.Cells(w3row, 8), Excel.Range)
            xlRange1.Font.Color = System.Drawing.Color.Yellow

            newRowCount += 1
            w3row += 1
        ElseIf delItem Then
            xlWorkSheet3.Cells(w3row, 1) = GetCellData(xlWorkSheet1, newRowCount, 1)
            xlRange1 = CType(xlWorkSheet3.Cells(w3row, 1), Excel.Range)
            xlRange1.Font.Color = System.Drawing.Color.Red

            xlWorkSheet3.Cells(w3row, 2) = GetCellData(xlWorkSheet1, newRowCount, 2)
            xlRange1 = CType(xlWorkSheet3.Cells(w3row, 2), Excel.Range)
            xlRange1.Font.Color = System.Drawing.Color.Red

            xlWorkSheet3.Cells(w3row, 3) = GetCellData(xlWorkSheet1, newRowCount, 3)
            xlRange1 = CType(xlWorkSheet3.Cells(w3row, 3), Excel.Range)
            xlRange1.Font.Color = System.Drawing.Color.Red

            xlWorkSheet3.Cells(w3row, 4) = GetCellData(xlWorkSheet1, newRowCount, 4)
            xlRange1 = CType(xlWorkSheet3.Cells(w3row, 4), Excel.Range)
            xlRange1.Font.Color = System.Drawing.Color.Red

            xlWorkSheet3.Cells(w3row, 5) = GetCellData(xlWorkSheet1, newRowCount, 5)
            xlRange1 = CType(xlWorkSheet3.Cells(w3row, 5), Excel.Range)
            xlRange1.Font.Color = System.Drawing.Color.Red

            xlWorkSheet3.Cells(w3row, 6) = GetCellData(xlWorkSheet1, newRowCount, 6)
            xlRange1 = CType(xlWorkSheet3.Cells(w3row, 6), Excel.Range)
            xlRange1.Font.Color = System.Drawing.Color.Red

            xlWorkSheet3.Cells(w3row, 7) = GetCellData(xlWorkSheet1, newRowCount, 7)
            xlRange1 = CType(xlWorkSheet3.Cells(w3row, 7), Excel.Range)
            xlRange1.Font.Color = System.Drawing.Color.Red

            xlWorkSheet3.Cells(w3row, 8) = GetCellData(xlWorkSheet1, newRowCount, 8)
            xlRange1 = CType(xlWorkSheet3.Cells(w3row, 8), Excel.Range)
            xlRange1.Font.Color = System.Drawing.Color.Red

            newRowCount += 1
            i -= 1
            w3row += 1
        ElseIf newItem Then
            xlWorkSheet3.Cells(w3row, 1) = GetCellData(xlWorkSheet2, i, 1)
            xlRange1 = CType(xlWorkSheet3.Cells(w3row, 1), Excel.Range)
            xlRange1.Font.Color = System.Drawing.Color.Blue

            xlWorkSheet3.Cells(w3row, 2) = GetCellData(xlWorkSheet2, i, 2)
            xlRange1 = CType(xlWorkSheet3.Cells(w3row, 2), Excel.Range)
            xlRange1.Font.Color = System.Drawing.Color.Blue

            xlWorkSheet3.Cells(w3row, 3) = GetCellData(xlWorkSheet2, i, 3)
            xlRange1 = CType(xlWorkSheet3.Cells(w3row, 3), Excel.Range)
            xlRange1.Font.Color = System.Drawing.Color.Blue

            xlWorkSheet3.Cells(w3row, 4) = GetCellData(xlWorkSheet2, i, 4)
            xlRange1 = CType(xlWorkSheet3.Cells(w3row, 4), Excel.Range)
            xlRange1.Font.Color = System.Drawing.Color.Blue

            xlWorkSheet3.Cells(w3row, 5) = GetCellData(xlWorkSheet2, i, 5)
            xlRange1 = CType(xlWorkSheet3.Cells(w3row, 5), Excel.Range)
            xlRange1.Font.Color = System.Drawing.Color.Blue

            xlWorkSheet3.Cells(w3row, 6) = GetCellData(xlWorkSheet2, i, 6)
            xlRange1 = CType(xlWorkSheet3.Cells(w3row, 6), Excel.Range)
            xlRange1.Font.Color = System.Drawing.Color.Blue

            xlWorkSheet3.Cells(w3row, 7) = GetCellData(xlWorkSheet2, i, 7)
            xlRange1 = CType(xlWorkSheet3.Cells(w3row, 7), Excel.Range)
            xlRange1.Font.Color = System.Drawing.Color.Blue

            xlWorkSheet3.Cells(w3row, 8) = GetCellData(xlWorkSheet2, i, 8)
            xlRange1 = CType(xlWorkSheet3.Cells(w3row, 8), Excel.Range)
            xlRange1.Font.Color = System.Drawing.Color.Blue

            w3row += 1
        End If

        qtyChange = False
        mtlChange = False
        newItem = False
        delItem = False
    Next

    'Save new spreadsheet [TEMP NAME]
    xlWorkBook3.SaveAs("C:\Users\Windows\Documents\Visual Studio 2013\Projects\test.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, _
                Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue)
    xlWorkBook3.Close(True, misValue, misValue)
    xlApp3.Quit()

    '----------------------------------
    'End of process, cleaning up CPU---
    xlWorkBook1.Close(True, misValue, misValue)
    xlApp1.Quit()

    xlWorkBook2.Close(True, misValue, misValue)
    xlApp2.Quit()

    ReleaseObject(xlWorkSheet1)
    ReleaseObject(xlWorkBook1)
    ReleaseObject(xlApp1)

    ReleaseObject(xlWorkSheet2)
    ReleaseObject(xlWorkBook2)
    ReleaseObject(xlApp2)

    ReleaseObject(xlWorkSheet3)
    ReleaseObject(xlWorkBook3)
    ReleaseObject(xlApp3)

    cmdFormat.Text = "Process complete. Compare new files?"
End Sub
编辑:什么不行?它将跳过相同的行并检测单元格的变化(我有第4列和第6列设置将它们返回为绿色/黄色)但是一旦我的代码在新工作表中找到了已删除的条目/新工作表中的新条目,它就会混乱

我认为这是搞乱的地方:

ElseIf GetCellData(xlWorkSheet1, newRowCount, 3) <> GetCellData(xlWorkSheet2, i, 3) And _
        GetCellData(xlWorkSheet1, newRowCount, 4) <> GetCellData(xlWorkSheet2, i, 4) Then

        If GetCellData(xlWorkSheet1, newRowCount + 1, 4) <> GetCellData(xlWorkSheet2, i, 4) Then
            newItem = True
        ElseIf GetCellData(xlWorkSheet1, newRowCount, 4) <> GetCellData(xlWorkSheet2, i + 1, 4) Then
            delItem = True
        End If
    End If

如果有人能看到这个问题,请告诉我。或者,如果你有另一个解决方案。谢谢!

1 个答案:

答案 0 :(得分:1)

对不起,我没有更多的帮助。这绝对有点棘手。我本可以亲自将每张纸张拉成数据表并在那里进行比较。这就是我用来确定我的程序在两组数据之间的新/完成QA的原因:

http://i.imgur.com/iuI1dGL.png

我最终做的事情与你的有点不同。

我使用此方法将我需要的数据从excel文件中提取到数据表中。

比使用excel互操作一次读取每一行快得多。

抓取Excel数据并使用它来填充DataTable:

//////////////////////////////////////////////////////////////////////////////////
//This function is absolute magic >.> - Fill DataTable with excel spreadsheet
//HDR=YES means "Spreadsheet has headers" Change to NO if not.
//name = "Log"; - This is the Sheet name to pull the data from
//////////////////////////////////////////////////////////////////////////////////
//oconn takes an SQL like command (Select Everything from name sheet) using con
//HDR=YES means that our data has headers :)
//////////////////////////////////////////////////////////////////////////////////
String constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + copyToPath + ";Extended Properties='Excel 12.0 XML;HDR=YES;';";
OleDbConnection con = new OleDbConnection(constr);
OleDbCommand oconn = new OleDbCommand("Select * From [Log$]", con);
con.Open();
OleDbDataAdapter sda = new OleDbDataAdapter(oconn);
DataTable data = new DataTable();
sda.Fill(data);
con.Close();
//////////////////////////////////////////////////////////////////////////////////

这为我们留下了一个DataTable,其中包含excel电子表格的所有行/标题。

然后,我们可以使用相同的方法拉入第二个。

此时,我们将能够比较C#中的数据,这在我的经验中要快得多。

最后,您可以使用Excel Interop导出到Excel,并且您只需要打开一个实例。