感谢您的帮助@Steve 和 @Antonio Bakula
这是我的后期绑定代码,适用于所有版本的 excel ..
Try
Dim app As Object
Dim xlbook As Object
Dim xlsheet As Object
app = CreateObject("Excel.Application")
xlbook = app.Workbooks.Add()
xlsheet = xlbook.ActiveSheet
app.Visible = True
Dim iX As Integer
Dim iY As Integer
Dim iC As Integer
Dim iz As Integer
For iC = 0 To DataGridView1.Columns.Count - 1
xlsheet.Cells(1, iC + 1).Value = DataGridView1.Columns(iC).HeaderText
Next
iz = 1
For iX = 0 To DataGridView1.Rows.Count - 1
For iY = 0 To DataGridView1.Columns.Count - 1
Dim a As String = DataGridView1(iY, iX).Value
If a <> Nothing Then xlsheet.Cells(iz + 1, iY + 1).value = DataGridView1(iY, iX).Value.ToString
Next
iz = iz + 1
Next
MsgBox("Export Done", MsgBoxStyle.Information, "MODEL AND WARRANTY")
app.Visible = True
app.UserControl = True
releaseobject(app)
releaseobject(xlbook)
releaseobject(xlsheet)
Catch ex As Exception
MessageBox.Show(ex.ToString())
End Try
提取完成后释放 excel 对象。
Sub releaseobject(ByVal obj As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
obj = Nothing
Catch ex As Exception
obj = Nothing
End Try
End Sub