网格导出excel文件没有标题

时间:2017-09-06 22:55:28

标签: .net vb.net

我正在尝试构建一个在一个Excel工作表中导出多个网格记录的小程序,下面的代码保存来自所有三个gridview的数据并导出它们但问题是它只显示第一个gridview的标题并添加了两个网格其余部分的记录就像在一张桌子上一样。

尝试导出的记录看起来像这样,

enter image description here

Private Sub ExportToExcel()
    ' Creating a Excel object.
    Dim excel As Microsoft.Office.Interop.Excel._Application = New Microsoft.Office.Interop.Excel.Application()
    Dim workbook As Microsoft.Office.Interop.Excel._Workbook = excel.Workbooks.Add(Type.Missing)
    Dim worksheet As Microsoft.Office.Interop.Excel._Worksheet = Nothing

    Try

        worksheet = workbook.ActiveSheet

        worksheet.Name = "Roster Details"

        Dim cellRowIndex As Integer = 1
        Dim cellColumnIndex As Integer = 1

        'Loop through each row and read value from each column.
        For i As Integer = 0 To dgvPlayed.Rows.Count - 2
            For j As Integer = 0 To dgvPlayed.Columns.Count - 1
                ' Excel index starts from 1,1. As first Row would have the Column headers, adding a condition check.
                If cellRowIndex = 1 Then
                    worksheet.Cells(cellRowIndex, cellColumnIndex) = dgvPlayed.Columns(j).HeaderText
                Else
                    worksheet.Cells(cellRowIndex, cellColumnIndex) = dgvPlayed.Rows(i).Cells(j).Value.ToString()
                End If
                cellColumnIndex += 1
            Next
            cellColumnIndex = 1
            cellRowIndex += 1
        Next

        For i As Integer = 0 To dgvNotPlayed.Rows.Count - 2
            For j As Integer = 0 To dgvNotPlayed.Columns.Count - 1
                ' Excel index starts from 1,1. As first Row would have the Column headers, adding a condition check.
                If cellRowIndex <> 1 Then
                    worksheet.Cells(cellRowIndex, cellColumnIndex) = dgvNotPlayed.Columns(j).HeaderText
                    'Else
                    worksheet.Cells(cellRowIndex, cellColumnIndex) = dgvNotPlayed.Rows(i).Cells(j).Value.ToString()
                End If
                cellColumnIndex += 1
            Next
            cellColumnIndex = 1
            cellRowIndex += 1
        Next

        For i As Integer = 0 To dgvPlayerPlayedSchedule.Rows.Count - 2
            For j As Integer = 0 To dgvPlayerPlayedSchedule.Columns.Count - 1
                ' Excel index starts from 1,1. As first Row would have the Column headers, adding a condition check.
                If cellRowIndex = 1 Then
                    worksheet.Cells(cellRowIndex, cellColumnIndex) = dgvPlayerPlayedSchedule.Columns(j).HeaderText
                Else
                    worksheet.Cells(cellRowIndex, cellColumnIndex) = dgvPlayerPlayedSchedule.Rows(i).Cells(j).Value.ToString()
                End If
                cellColumnIndex += 1
            Next
            cellColumnIndex = 1
            cellRowIndex += 1
        Next

        For i As Integer = 0 To dgvPlayerNotPlayedSchedule.Rows.Count - 2
            For j As Integer = 0 To dgvPlayerNotPlayedSchedule.Columns.Count - 1
                ' Excel index starts from 1,1. As first Row would have the Column headers, adding a condition check.
                If cellRowIndex = 1 Then
                    worksheet.Cells(cellRowIndex, cellColumnIndex) = dgvPlayerNotPlayedSchedule.Columns(j).HeaderText
                Else
                    worksheet.Cells(cellRowIndex, cellColumnIndex) = dgvPlayerNotPlayedSchedule.Rows(i).Cells(j).Value.ToString()
                End If
                cellColumnIndex += 1
            Next
            cellColumnIndex = 1
            cellRowIndex += 1
        Next


        'Getting the location and file name of the excel to save from user.
        Dim saveDialog As New SaveFileDialog()
        saveDialog.Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*"
        saveDialog.FilterIndex = 2

        If saveDialog.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            workbook.SaveAs(saveDialog.FileName)
            MessageBox.Show("Export Successful")
        End If
    Catch ex As System.Exception
        MessageBox.Show(ex.Message)
    Finally
        excel.Quit()
        workbook = Nothing
        excel = Nothing
    End Try

End Sub

它产生的结果有点像这样,

enter image description here

我的目标与此事有些相似。

enter image description here

如果我对此部分代码使用headerRow = cellRowIndex + 1,则结果如下所示。

 'next  section header row
        headerRow = cellRowIndex + 1

        For i As Integer = 0 To dgvNotPlayed.Rows.Count - 2
            For j As Integer = 0 To dgvNotPlayed.Columns.Count - 1
                ' Excel index starts from 1,1. As first Row would have the Column headers, adding a condition check.
                If cellRowIndex <> headerRow Then
                    worksheet.Cells(cellRowIndex, cellColumnIndex) = dgvNotPlayed.Columns(j).HeaderText
                    'Else
                    worksheet.Cells(cellRowIndex, cellColumnIndex) = dgvNotPlayed.Rows(i).Cells(j).Value.ToString()
                End If
                cellColumnIndex += 1
            Next
            cellColumnIndex = 1
            cellRowIndex += 1
        Next

enter image description here 如果有人能帮助我解决这个问题,我真的很高兴。

最好的问候

SMHasnain

1 个答案:

答案 0 :(得分:0)

您应修改代码重新设置标题行(仅在第一个数据集之前它的行()= 1):

Private Sub ExportToExcel()
    ' Creating a Excel object.
    Dim excel As Microsoft.Office.Interop.Excel._Application = New Microsoft.Office.Interop.Excel.Application()
    Dim workbook As Microsoft.Office.Interop.Excel._Workbook = excel.Workbooks.Add(Type.Missing)
    Dim worksheet As Microsoft.Office.Interop.Excel._Worksheet = Nothing

    Try

        worksheet = workbook.ActiveSheet

        worksheet.Name = "Roster Details"

        Dim cellRowIndex As Integer = 1
        Dim cellColumnIndex As Integer = 1

        'Loop through each row and read value from each column.
        For i As Integer = 0 To dgvPlayed.Rows.Count - 2
            For j As Integer = 0 To dgvPlayed.Columns.Count - 1
                ' Excel index starts from 1,1. As first Row would have the Column headers, adding a condition check.
                If cellRowIndex = 1 Then
                    worksheet.Cells(cellRowIndex, cellColumnIndex) = dgvPlayed.Columns(j).HeaderText
                Else
                    worksheet.Cells(cellRowIndex, cellColumnIndex) = dgvPlayed.Rows(i).Cells(j).Value.ToString()
                End If
                cellColumnIndex += 1
            Next
            cellColumnIndex = 1
            cellRowIndex += 1
        Next

'next  section header row
headerRow = cellRowIndex + 1

        For i As Integer = 0 To dgvNotPlayed.Rows.Count - 2
            For j As Integer = 0 To dgvNotPlayed.Columns.Count - 1
                ' Excel index starts from 1,1. As first Row would have the Column headers, adding a condition check.
                If cellRowIndex <> headerRow  Then
                    worksheet.Cells(cellRowIndex, cellColumnIndex) = dgvNotPlayed.Columns(j).HeaderText
                    'Else
                    worksheet.Cells(cellRowIndex, cellColumnIndex) = dgvNotPlayed.Rows(i).Cells(j).Value.ToString()
                End If
                cellColumnIndex += 1
            Next
            cellColumnIndex = 1
            cellRowIndex += 1
        Next

'next  section header row
headerRow = cellRowIndex + 1

        For i As Integer = 0 To dgvPlayerPlayedSchedule.Rows.Count - 2
            For j As Integer = 0 To dgvPlayerPlayedSchedule.Columns.Count - 1
                ' Excel index starts from 1,1. As first Row would have the Column headers, adding a condition check.
                If cellRowIndex = headerRow  Then
                    worksheet.Cells(cellRowIndex, cellColumnIndex) = dgvPlayerPlayedSchedule.Columns(j).HeaderText
                Else
                    worksheet.Cells(cellRowIndex, cellColumnIndex) = dgvPlayerPlayedSchedule.Rows(i).Cells(j).Value.ToString()
                End If
                cellColumnIndex += 1
            Next
            cellColumnIndex = 1
            cellRowIndex += 1
        Next

 'next  section header row
headerRow = cellRowIndex + 1 

        For i As Integer = 0 To dgvPlayerNotPlayedSchedule.Rows.Count - 2
            For j As Integer = 0 To dgvPlayerNotPlayedSchedule.Columns.Count - 1
                ' Excel index starts from 1,1. As first Row would have the Column headers, adding a condition check.
                If cellRowIndex = headerRow  Then
                    worksheet.Cells(cellRowIndex, cellColumnIndex) = dgvPlayerNotPlayedSchedule.Columns(j).HeaderText
                Else
                    worksheet.Cells(cellRowIndex, cellColumnIndex) = dgvPlayerNotPlayedSchedule.Rows(i).Cells(j).Value.ToString()
                End If
                cellColumnIndex += 1
            Next
            cellColumnIndex = 1
            cellRowIndex += 1
        Next


        'Getting the location and file name of the excel to save from user.
        Dim saveDialog As New SaveFileDialog()
        saveDialog.Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*"
        saveDialog.FilterIndex = 2

        If saveDialog.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            workbook.SaveAs(saveDialog.FileName)
            MessageBox.Show("Export Successful")
        End If
    Catch ex As System.Exception
        MessageBox.Show(ex.Message)
    Finally
        excel.Quit()
        workbook = Nothing
        excel = Nothing
    End Try

End Sub