我参照别人将DataGrid的数据导出到EXCEL里面进行打印的例子,代码如下:
Private Sub Command1_Click()
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Set xlApp = CreateObject("Excel.Application")
'xlApp.Visible = True
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets(1)
xlSheet.Columns.AutoFit
Me.MousePointer = 11
For k = 0 To DataGrid1.Columns.Count - 1 'DataGrid所有的列数
xlSheet.Cells(1, k + 1) = DataGrid1.Columns(k).Caption '第一行为DataGrid的列标题
Next
DataGrid1.Scroll 0, -DataGrid1.FirstRow'导出前拉动过垂直滚动条
DataGrid1.Row = 0
For i = 0 To DataGrid1.ApproxCount - 1 'DataGrid的所有行数
For j = 0 To DataGrid1.Columns.Count - 1 'DataGrid所有的列数,若将此数改小到不拉DataGrid的垂直滚动条的时候能看见的行数的时候正常
DataGrid1.Col = j
(1) xlSheet.Cells(i + 2, j + 1) = DataGrid1.Text '从第二行显示'DataGrid的内容
Next
If i < DataGrid1.ApproxCount - 1 Then
DataGrid1.Row = DataGrid1.Row + 1
End If
Next
Me.MousePointer = 0
MsgBox "ok"
xlApp.Visible = True
Set xlApp = Nothing '交还控制给Excel
Set xlBook = Nothing
Set xlSheet = Nothing
End Sub 在我自己datagrid列中有一栏是身份证号码,18位的,比方说是452020202020202020,导出到Excel里却成了4.52E+17,VB把它当Double处理了,请问我该怎么改?应该是在(1)的位置改吧?
Private Sub Command1_Click()
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Set xlApp = CreateObject("Excel.Application")
'xlApp.Visible = True
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets(1)
xlSheet.Columns.AutoFit
Me.MousePointer = 11
For k = 0 To DataGrid1.Columns.Count - 1 'DataGrid所有的列数
xlSheet.Cells(1, k + 1) = DataGrid1.Columns(k).Caption '第一行为DataGrid的列标题
Next
DataGrid1.Scroll 0, -DataGrid1.FirstRow'导出前拉动过垂直滚动条
DataGrid1.Row = 0
For i = 0 To DataGrid1.ApproxCount - 1 'DataGrid的所有行数
For j = 0 To DataGrid1.Columns.Count - 1 'DataGrid所有的列数,若将此数改小到不拉DataGrid的垂直滚动条的时候能看见的行数的时候正常
DataGrid1.Col = j
(1) xlSheet.Cells(i + 2, j + 1) = DataGrid1.Text '从第二行显示'DataGrid的内容
Next
If i < DataGrid1.ApproxCount - 1 Then
DataGrid1.Row = DataGrid1.Row + 1
End If
Next
Me.MousePointer = 0
MsgBox "ok"
xlApp.Visible = True
Set xlApp = Nothing '交还控制给Excel
Set xlBook = Nothing
Set xlSheet = Nothing
End Sub 在我自己datagrid列中有一栏是身份证号码,18位的,比方说是452020202020202020,导出到Excel里却成了4.52E+17,VB把它当Double处理了,请问我该怎么改?应该是在(1)的位置改吧?
解决方案 »
- StrtoBase64的问题
- 如何将字符串型"2008-10-02 0000"(即2008年10月2日00:00分)改成日期型
- 问个棘手的问题
- 用VB编写的EXE窗体程序能不能和嵌在网页中的ocx进行通信,怎么样通信?ocx也是VB编写的。
- 我开发的XLibrary资料收集管理程序出炉。请大家近来瞅瞅!欢迎排砖!
- 此种WORD保护文档如何制作?
- mciSendString("save mySound c:\test.wav")出错!错误代码MCIERR_FILE_NOT_SAVED
- 怎么把byte数组转换成Unicode格式的字符串,vb的StrConv好像有问题
- MSFlexGrid 问题
- 这个问题我问了好几次了,还没有解决!!
- 一个SQL问题
- 关于界面显示的问题
难道我要设一个变量作为计数器,等到变量到了身份证那列再进行转换?太麻烦了吧
看一下了!
xlSheet.Cells(H + 1, L).NumberFormatLocal = "@"
For j = 0 To DataGrid1.Columns.Count - 1
DataGrid1.Col = j
If j = 4 Then
xlSheet.Cells(i + 2, j + 1) = CStr(DataGrid1.Text) '转换
Else
xlSheet.Cells(i + 2, j + 1) = DataGrid1.Text
End If
Next
可仍然不行,请问什么地方出问题了
除非该单元格已经手工设置为文本格式。
所以,如 QQRN 所说,你的代码中必须要有设置excel单元格属性的代码,或者输出时多加个非数字字符:英文状态下的单引号。
至于如何设置excel表格相关属性,你可以打开excel,然后启动记录宏,再开始设置,然后停止宏,再去看excel中VBA自动生成的代码,基本上,简单复制到VB中就可以了。
以你的代码为例,在输出身份证字串的语句前,再加上一句:
For j = 0 To DataGrid1.Columns.Count - 1
DataGrid1.Col = j
If j = 4 Then
xlSheet.Cells(i + 2, j + 1).NumberFormatLocal = "@" '加这句看看
xlSheet.Cells(i + 2, j + 1) = CStr(DataGrid1.Text) '转换
Else
xlSheet.Cells(i + 2, j + 1) = DataGrid1.Text
End If
Next