获取数据网格视图数据并将其放入Excel

时间:2014-06-18 01:42:14

标签: vb.net

你好这是我想要做的。我有一个Tab控件,大约有10个选项卡,每个选项卡都有自己的DataGridView,所有都与6列相同,行将保存数据。每个选项卡的行数会有所不同。我需要获取行中的所有信息并将它们放在excel中。我有10个DataGridViews都叫做dgvS,dgvHRDF,dgvHRDF,dgvR,dgvRT,dgvP我需要将数据放在每个数据表的某些单元格中。如果该表中没有数据则跳过并转到下一个表。

Dim i As Integer
        Dim j As Integer
        'Open a blank Excel File.
        oXL = CType(CreateObject("Excel.Application"), Excel.Application)
        'Sets oxl visible off so you can't see it open.
        oXL.Visible = False
        ' Open sheet this is just a blank sheet.
        oWB = oXL.Workbooks.Open("C:\Template.xlsm")
        'Set oSheet to active sheet.
        oSheet = CType(oWB.ActiveSheet, Excel.Worksheet)

        'Here We will set up the Excel Sheet.


        On Error Resume Next
        For i = 0 To dgvS.RowCount - 1
            For j = 0 To dgvS.ColumnCount - 1
                For k As Integer = 1 To dgvS.Columns.Count
                    oSheet.Cells(54, k) = dgvS.Columns(k + 1).HeaderText
                    oSheet.Cells(i + 53, j + 1) = dgvS(j, i).Value.ToString()
                Next
            Next
        Next

    End Sub

0 个答案:

没有答案