【问题标题】:Grid exporting excel file without headers网格导出没有标题的excel文件
【发布时间】:2017-09-06 22:55:27
【问题描述】:

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

尝试导出的记录 m 如下所示,

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

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

我的目标和这件事有些相似。

如果这部分代码的 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

如果有人能帮我解决这个问题,我会非常高兴。

最好的问候

SMHasnain

【问题讨论】:

    标签: .net vb.net


    【解决方案1】:

    您应该修改您的代码重新设置标题行(它的 row()=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
    

    【讨论】:

    • 我应该在哪一部分添加重置代码,你能指导我更多吗。
    • 这将为第二和第三部分设置新的标题行:headerRow = cellRowIndex + 1(它在我的答案中的代码 sn-p 中)
    • @MHHasnain 不要忘记投票并接受有帮助的答案!
    • 如果我添加 headerRow = cellRowIndex +1 那么它仍然只显示第一个,它的作用是从 excel 文件中清除记录。我不知道如何在此评论部分显示图像,否则我会在这里上传。等等,让我更新上面的帖子。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多