我已经创建了一个datagridveiw来显示访问数据库。我需要创建一个从datagrideview到MS Excel的导出功能。下面是我将所有数据从datagrideview导出到MS Excel的编码。
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Try
Button3.Text = "Please Wait..."
Button3.Enabled = False
SaveFileDialog1.Filter = "Excel Document (*.xlsx)|*.xlsx"
If SaveFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Dim xlApp As Microsoft.Office.Interop.Excel.Application
Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook
Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
Dim i As Integer
Dim j As Integer
xlApp = New Microsoft.Office.Interop.Excel.Application
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
For i = 0 To DataGridView1.RowCount - 2
For j = 0 To DataGridView1.ColumnCount - 1
For k As Integer = 1 To DataGridView1.Columns.Count
xlWorkSheet.Cells(1, k) = DataGridView1.Columns(k - 1).HeaderText
xlWorkSheet.Cells(i + 2, j + 1) = DataGridView1(j, i).Value.ToString()
Next
Next
Next
xlWorkSheet.SaveAs(SaveFileDialog1.FileName)
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
MsgBox("Successfully saved" & vbCrLf & "File are saved at : " & SaveFileDialog1.FileName, MsgBoxStyle.Information, "Information")
Button3.Text = "Export To MS Excel"
Button3.Enabled = True
End If
Catch ex As Exception
MessageBox.Show("Failed to save !!!", "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End Try
如何使用复选框将仅数据行从Datagrideview导出到MS Excel?