如何用VB从EXCEL中读取和保存数据。
2个回答
展开全部
Dim i As Integer
Dim j As Integer
Dim ex As Object
Dim exwbook As Object
Dim exsheet As Object
Set ex = CreateObject("Excel.Application")
Set exwbook = Nothing
Set exsheet = Nothing
Set exwbook = ex.Workbooks().Add
Set exsheet = exwbook.Worksheets("sheet1")
'在excel里表格的表头
ex.Range("c3").Value = "序号1"
ex.Range("d3").Value = "时间"
ex.Range("e3").Value = "数据1"
ex.Range("f3").Value = "数据2"
ex.Range("g3").Value = "数据3"
ex.Range("h3").Value = "累计数据"
'i为记录个数使用循环将数据全部添加
For i = 1 To Adodc1.Recordset.RecordCount
j = 3 + i
'k为数据列数
For k = 0 To 5
'通过使用变量k和j变换单元格位置
q = Chr(99 + k) & j
'将datagrid1的数据放到单元格内
ex.Range(q).Value = DataGrid1.Columns(k)
Next k
'指针下移
If Adodc1.Recordset.EOF = False Then
Adodc1.Recordset.MoveNext
End If
Next i
'保存输入到信息查询结果.xls
exwbook.SaveAs "D:\新建文件夹\记录.xls"
'退出excel
ex.Quit
End Sub
Dim j As Integer
Dim ex As Object
Dim exwbook As Object
Dim exsheet As Object
Set ex = CreateObject("Excel.Application")
Set exwbook = Nothing
Set exsheet = Nothing
Set exwbook = ex.Workbooks().Add
Set exsheet = exwbook.Worksheets("sheet1")
'在excel里表格的表头
ex.Range("c3").Value = "序号1"
ex.Range("d3").Value = "时间"
ex.Range("e3").Value = "数据1"
ex.Range("f3").Value = "数据2"
ex.Range("g3").Value = "数据3"
ex.Range("h3").Value = "累计数据"
'i为记录个数使用循环将数据全部添加
For i = 1 To Adodc1.Recordset.RecordCount
j = 3 + i
'k为数据列数
For k = 0 To 5
'通过使用变量k和j变换单元格位置
q = Chr(99 + k) & j
'将datagrid1的数据放到单元格内
ex.Range(q).Value = DataGrid1.Columns(k)
Next k
'指针下移
If Adodc1.Recordset.EOF = False Then
Adodc1.Recordset.MoveNext
End If
Next i
'保存输入到信息查询结果.xls
exwbook.SaveAs "D:\新建文件夹\记录.xls"
'退出excel
ex.Quit
End Sub
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |