[VB.NET]救助:将datagrid中的数据导入到excel中时遇到错误??

前端之家收集整理的这篇文章主要介绍了[VB.NET]救助:将datagrid中的数据导入到excel中时遇到错误??前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
救助:将datagrid中的数据导入到excel中时遇到错误?? Public Sub ExportToExcel(ByVal tb As DataTable) If datagrid1.VisibleRowCount > 0 Then Try Dim datav As New DataView datav = tb.DefaultView Dim i,j As Integer Dim rows As Integer = datav.Table.Rows.Count Dim cols As Integer = datav.Table.Columns.Count Dim DataArray(rows - 1,cols - 1) As String For i = 0 To rows - 1 For j = 0 To cols - 1 If datav.Table.Rows(i).Item(j) Is System.DBNull.Value Then Else DataArray(i,j) = datav.Table.Rows(i).Item(j) End If Next Next Dim myExcel As Excel.Application = New Excel.Application myExcel.Application.Workbooks.Add(True) myExcel.Visible = True For j = 0 To cols - 1 myExcel.Cells(1,j + 1) = datav.Table.Columns(j).ColumnName Next myExcel.Range( A2 ).Resize(rows,cols).Value = DataArray Catch exp As Exception MessageBox.Show( 数据导出失败!请查看是否已经安装了Excel,Me.Text,MessageBoxButtons.OK,MessageBoxIcon.Warning) End Try Else MessageBox.Show( 没有数据!,MessageBoxIcon.Information) End If End Sub 一直提示:数据导出失败!请查看是否已经安装了Excel,哪有错吗???帮忙一下,非常感激..... __________________________________________________________________________ 根据上面的描述,如果LZ确定准了office的话,可能是execl的版本问题; 加外,lz看下有没有引用系统自带的Microsoft excel 11.0 object library组件 __________________________________________________________________________ Debug一下 一直提示:数据导出失败!请查看是否已经安装了Excel 是因为中间段出现错误 程序抛异常处理到 Catch exp As Exception段 检查Try Catch段 __________________________________________________________________________

猜你在找的VB相关文章