从vb.net App导出时需要合并Excel文件的Column Data

时间:2017-08-30 07:28:05

标签: excel vb.net export-to-excel

我的工作表中包含由WinForms Application导出的数据。请检查屏幕截图,以便更好地了解工作表Here中的数据。现在我想要的是合并具有Ex的公共值的单元格。我的一个列中有一个InvoiceID,因此InvoicID中会有一些项目。我想合并没有等于项的行,它只能为那些记录显示一次。为了更好地理解,请检查我手动执行的屏幕截图Here。 有什么方法可以使用代码执行相同的操作。 以下是我用于将数据导出到Excel的代码。

`Dim excelConverter As New GridGroupingExcelConverterControl

Dim exportsOptions As New ExcelExportingOptions excelConverter.ExportToExcel(GridGroupingControl1,“Sample.xlsx”,exportsOptions)

的Process.Start( “Sample.xlsx”)

    Dim xlApp As Excel.Application
    Dim xlWorkBook As Excel.Workbook
    Dim xlWorkSheet As Excel.Worksheet
    Dim misValue As Object = System.Reflection.Missing.Value
    Dim i As Integer
    Dim j As Integer

    xlApp = New Excel.Application
    xlWorkBook = xlApp.Workbooks.Add(misValue)
    xlWorkSheet = xlWorkBook.Sheets("sheet1")

    For i = 0 To dgvSearch.RowCount - 2
        For j = 0 To dgvSearch.ColumnCount - 1
            xlWorkSheet.Cells(i + 1, j + 1) = _
                dgvSearch(j, i).Value.ToString()
        Next
    Next
    xlWorkBook.SaveAs("\vbexcel.xlsx")
    xlWorkBook.Close()
    xlApp.Quit()
    releaseObject(xlApp)
    releaseObject(xlWorkBook)
    releaseObject(xlWorkSheet)
    MsgBox("You can find the file C:\vbexcel.xlsx")`

我是将数据导出到excel的新手,因此非常有帮助。 谢谢,

1 个答案:

答案 0 :(得分:0)

你可以创建一个循环。因此,通过每个单元格进行迭代。我开始写它,但最终不得不编码。解释工作要比编写工作困难得多:)

下面是代码,您需要在For-Loop之后和SaveAs之前将其添加到您的代码中。我对i(x轴)的+1和-2感到有点困惑我没有改变你的任何代码,如果那些都正确的话,我不想打扰它。

希望这有帮助

    For i = 1 To dgvSearch.RowCount - 2
        For j = 0 To dgvSearch.ColumnCount - 1
            'I dont understand why +1 etc? But we need to check the current cell against the one above, to see if they are the same
            Dim cellAbove As String = xlWorkSheet.Cells(i + 1, j)
            Dim cellActive As String = xlWorkSheet.Cells(i + 1, j + 1)
            Dim cellBelow As String = xlWorkSheet.Cells(i + 1, j + 2)

            If cellActive = cellAbove Then
                If cellBelow = cellActive Then
                    'There could be more below, so we leave for the moment..
                Else
                    'The cell above (and possibly more above that) are the same
                    'So we need to start itterating through the cells above to remove any duplicates
                    For y = j + 2 To 2 Step -1
                        xlWorkSheet.Cells(i + 1, y) = ""
                        'As we step up the column, we check the one above and two above - so we remove this one,
                        'if they both are the same, we carry on, if not - it means the one above is different
                        'to two above (hence the one above must be the one we need to keep.. :S - bear with me :)
                        If xlWorkSheet.Cells(i + 1, y - 1) = cellActive And xlWorkSheet.Cells(i + 1, y - 2) = cellActive Then
                        Else
                            'If they dont match, we exit - you can add a If NOT xlworksheet.... if you wanted, or keep the else here.
                            Exit For
                        End If
                    Next
                End If
            Else
                'Its not the same as above, so we need to keep this cell!
            End If
        Next
    Next