我做了一个excel到出的程序,导出的数据如果没有为空的话可以正常导出.但是如果导出的数据中,只要有一个单元格为空的话,就会出现:时实错误13,类型不匹配
代码如下:
Dim i As Integer
Dim j As Integer
Dim m As Integer
Dim n As Integer
Dim k As Integer
Set xlApp = CreateObject("excel.application")
Dim xlBook As Object
Dim xlSheet As Object
xlApp.Visible = True
'建立excel对象的工作薄对象
Set xlBook = xlApp.Workbooks.Add
'建立excel对象的工作表对象
Set xlSheet = xlBook.Worksheets(1)
j = DataGrid1.Columns.Count
i = 0
For n = 0 To j - 1
'如果DataGrid1表格中第n列可见,则将其该列的列标题输出至excel表
If DataGrid1.Columns(n).Visible = True Then
'在excel表中的第2行,第i列对应的单元格中显示DataGrid1表格中第n列的列标题
xlSheet.Cells(1, i + 1) = DataGrid1.Columns(n).Caption
i = i + 1
End If
Next n
Adodc1.Recordset.MoveFirst
m = 0
Do While Not Adodc1.Recordset.EOF
i = 1
For n = 0 To j - 1
'如果ataGrid1表格中第n列可见,则将其该列的值输出至excel表
If DataGrid1.Columns(n).Visible = True Then
If DataGrid1.Columns(n) = Null Then
xlSheet.Cells(m + 2, i) = ""
Else
'在excel表中的第m+3行,第i列对应的单元格中显示DataGrid1表格中第n列的值
xlSheet.Cells(m + 2, i) = DataGrid1.Columns(n).Value--调试的时候这里错
i = i + 1
End If
End If
Next n
Adodc1.Recordset.MoveNext
m = m + 1
Loop
xlApp.Visible = True '显示Excel
Set xlApp = Nothing '交还控制给Excel
Set xlBook = Nothing
Set xlSheet = Nothing
请大家帮帮我
代码如下:
Dim i As Integer
Dim j As Integer
Dim m As Integer
Dim n As Integer
Dim k As Integer
Set xlApp = CreateObject("excel.application")
Dim xlBook As Object
Dim xlSheet As Object
xlApp.Visible = True
'建立excel对象的工作薄对象
Set xlBook = xlApp.Workbooks.Add
'建立excel对象的工作表对象
Set xlSheet = xlBook.Worksheets(1)
j = DataGrid1.Columns.Count
i = 0
For n = 0 To j - 1
'如果DataGrid1表格中第n列可见,则将其该列的列标题输出至excel表
If DataGrid1.Columns(n).Visible = True Then
'在excel表中的第2行,第i列对应的单元格中显示DataGrid1表格中第n列的列标题
xlSheet.Cells(1, i + 1) = DataGrid1.Columns(n).Caption
i = i + 1
End If
Next n
Adodc1.Recordset.MoveFirst
m = 0
Do While Not Adodc1.Recordset.EOF
i = 1
For n = 0 To j - 1
'如果ataGrid1表格中第n列可见,则将其该列的值输出至excel表
If DataGrid1.Columns(n).Visible = True Then
If DataGrid1.Columns(n) = Null Then
xlSheet.Cells(m + 2, i) = ""
Else
'在excel表中的第m+3行,第i列对应的单元格中显示DataGrid1表格中第n列的值
xlSheet.Cells(m + 2, i) = DataGrid1.Columns(n).Value--调试的时候这里错
i = i + 1
End If
End If
Next n
Adodc1.Recordset.MoveNext
m = m + 1
Loop
xlApp.Visible = True '显示Excel
Set xlApp = Nothing '交还控制给Excel
Set xlBook = Nothing
Set xlSheet = Nothing
请大家帮帮我
解决方案 »
- 各位好,请问如把我输入的值添加到word中?
- 紧急求助:请问在VB中怎样将繁体字(Big5码)和简体字(GB2312码)转化为"UTF-8码"呢?
- VB 中一个简单的关于向数据库中存取图片的问题——————在线等待!!!!(初学者)
- VB中如何实现:程序启动时, 在任务栏右下角添加小图标?谢谢
- vb6打包问题,为什么打完了在2000系统下装不了
- vb可以写控制台应用程序么?
- 请问C++如何向VBS脚本程序传参数
- 诚心求教,如何知道一个DLL动态连接库内的函数名及调用格式
- 请教一个关于VB应用程序的帮助文件问题!(如何在command1-click事件中调用帮助文件)
- 想编一个病人类,因为是初学着却无从下手,请帮助
- 在一个类模块中要调用其他工程中的类模块里面的函数需要怎么调用?
- 如何解决pop-up菜单造成的其他消息停止响应。。
改成:
xlSheet.Cells(m + 2, i) = DataGrid1.Columns(n).Value & ""
就可以了。