vb读取excel内容
一个按钮,点击出现对话框(对话框控件已经有),在硬盘里面查找excel文件(当然,后缀名是xls了),找到目标excel文件后,该excel文件里面是一些数据,点击确定,...
一个按钮,点击出现对话框(对话框控件已经有),在硬盘里面查找excel文件(当然,后缀名是xls了),找到目标excel文件后,该excel文件里面是一些数据,点击确定,就可以把excel里面的内容保存到MSFlexGrid控件里面了
还有一个,按钮,点击后出现对话框,可以保存MSFlexGrid里面的东西到一个excel里面
谢谢大家了 展开
还有一个,按钮,点击后出现对话框,可以保存MSFlexGrid里面的东西到一个excel里面
谢谢大家了 展开
3个回答
展开全部
用CommonDialog可以解决选定打开.xls文件问题 然后就是读取进去 哈哈 保存代码如下
'添加command控件一个 MSFlexGrid控件一个
Private Sub Command1_Click()
On Error Resume Next
Dim fileadd As String
CommonDialog1.ShowOpen
CommonDialog1.Filter = "xls文件(*.xls)|*.xls" '选择你要的文件
fileadd = CommonDialog1.FileName
MSHFlexGrid1.Redraw = False '关闭表格重画,加快运行速度
Set xlApp = CreateObject("Excel.Application") '创建EXCEL对象
Set xlBook = xlApp.Workbooks.Open(fileadd) '打开已经存在的EXCEL工件簿文件
xlApp.Visible = True '设置EXCEL对象可见(或不可见)
Set xlsheet = xlBook.Worksheets("Sheet1") '设置活动工作表
For R = 0 To MSHFlexGrid1.Rows - 1 '行循环
For C = 0 To MSHFlexGrid1.Cols - 1 '列循环
MSHFlexGrid1.Row = R
MSHFlexGrid1.Col = C
xlBook.Worksheets("Sheet1").Cells(R + 1, C + 1) = MSHFlexGrid1.Text '保存到EXCEL
Next C
Next R
MSHFlexGrid1.Redraw = True
xlApp.DisplayAlerts = False '不进行安全提示
'Set xlsheet = Nothing
'Set xlBook = Nothing
'xlApp.Quit
'Set xlApp = Nothing
End Sub
'添加command控件一个 MSFlexGrid控件一个
Private Sub Command1_Click()
On Error Resume Next
Dim fileadd As String
CommonDialog1.ShowOpen
CommonDialog1.Filter = "xls文件(*.xls)|*.xls" '选择你要的文件
fileadd = CommonDialog1.FileName
MSHFlexGrid1.Redraw = False '关闭表格重画,加快运行速度
Set xlApp = CreateObject("Excel.Application") '创建EXCEL对象
Set xlBook = xlApp.Workbooks.Open(fileadd) '打开已经存在的EXCEL工件簿文件
xlApp.Visible = True '设置EXCEL对象可见(或不可见)
Set xlsheet = xlBook.Worksheets("Sheet1") '设置活动工作表
For R = 0 To MSHFlexGrid1.Rows - 1 '行循环
For C = 0 To MSHFlexGrid1.Cols - 1 '列循环
MSHFlexGrid1.Row = R
MSHFlexGrid1.Col = C
xlBook.Worksheets("Sheet1").Cells(R + 1, C + 1) = MSHFlexGrid1.Text '保存到EXCEL
Next C
Next R
MSHFlexGrid1.Redraw = True
xlApp.DisplayAlerts = False '不进行安全提示
'Set xlsheet = Nothing
'Set xlBook = Nothing
'xlApp.Quit
'Set xlApp = Nothing
End Sub
展开全部
Dim n As Long
Set ExcelApp = CreateObject("Excel.Application") '创建EXCEL对象
Set ExcelBook = ExcelApp.Workbooks.open("D:\Documents and Settings\chaoliu\桌面\2月份考勤bbb.xls")
Set ExcelSheet = ExcelBook.Worksheets(1)
n = 1
Do
MsgBox "第" & n & " 条记录,第一列:" & ExcelSheet.Range("A" & n).Value & _
";第二列:" & ExcelSheet.Range("B" & n).Value
n = n + 1
Loop Until ExcelSheet.Range("A" & n).Value = ""
Set ExcelSheet = Nothing
Set ExcelBook = Nothing
Set ExcelApp = Nothing
使用说明:
*****************************************
VB读Excel文件,创建xls表,并写入内容
Set ExcelApp = CreateObject("Excel.Application") '创建EXCEL对象
Set ExcelBook = ExcelApp.Workbooks.Add
Set ExcelSheet = ExcelBook.Worksheets(1) '添加工作页
ExcelSheet.Activate '激活工作页
ExcelApp.DisplayAlerts = False
ExcelSheet.Name="sheet1"
ExcelSheet.Range("A1").Value = 100 '设置A1的值为100
ExcelBook.SaveAs "d:\test.xls" '保存工作表
msgbox "d:\test.xls创建成功!"
ExcelBook.close
set excelApp=nothing
set ExcelBook=nothing
set ExcelSheet=nothing
将以上代码copy到记事本存为"writexls.vbs"文件,可运行测试
读execel文件
Set ExcelApp = CreateObject("Excel.Application") '创建EXCEL对象
Set ExcelBook = ExcelApp.Workbooks.open("d:\test.xls")
Set ExcelSheet = ExcelBook.Worksheets(1)
msgbox ExcelSheet.Range("A1").Value
将以上代码copy到记事本存为"readxls.vbs"文件,可运行测试
以上文章来自于:http://bbs.54master.com/200898,1,1###
以下内容来自于:http://topic.csdn.net/t/20040930/09/3420583.html#
判断Excel文件有多少行和多少列
Dim lCols As Long
Dim lRows As Long
lCols = Sheet1.UsedRange.Cells.Columns.Count
lRows = Sheet1.UsedRange.Cells.Rows.Count
MsgBox "用户区有:" & lCols & "列 有:" & lRows & "行"
Set ExcelApp = CreateObject("Excel.Application") '创建EXCEL对象
Set ExcelBook = ExcelApp.Workbooks.open("D:\Documents and Settings\chaoliu\桌面\2月份考勤bbb.xls")
Set ExcelSheet = ExcelBook.Worksheets(1)
n = 1
Do
MsgBox "第" & n & " 条记录,第一列:" & ExcelSheet.Range("A" & n).Value & _
";第二列:" & ExcelSheet.Range("B" & n).Value
n = n + 1
Loop Until ExcelSheet.Range("A" & n).Value = ""
Set ExcelSheet = Nothing
Set ExcelBook = Nothing
Set ExcelApp = Nothing
使用说明:
*****************************************
VB读Excel文件,创建xls表,并写入内容
Set ExcelApp = CreateObject("Excel.Application") '创建EXCEL对象
Set ExcelBook = ExcelApp.Workbooks.Add
Set ExcelSheet = ExcelBook.Worksheets(1) '添加工作页
ExcelSheet.Activate '激活工作页
ExcelApp.DisplayAlerts = False
ExcelSheet.Name="sheet1"
ExcelSheet.Range("A1").Value = 100 '设置A1的值为100
ExcelBook.SaveAs "d:\test.xls" '保存工作表
msgbox "d:\test.xls创建成功!"
ExcelBook.close
set excelApp=nothing
set ExcelBook=nothing
set ExcelSheet=nothing
将以上代码copy到记事本存为"writexls.vbs"文件,可运行测试
读execel文件
Set ExcelApp = CreateObject("Excel.Application") '创建EXCEL对象
Set ExcelBook = ExcelApp.Workbooks.open("d:\test.xls")
Set ExcelSheet = ExcelBook.Worksheets(1)
msgbox ExcelSheet.Range("A1").Value
将以上代码copy到记事本存为"readxls.vbs"文件,可运行测试
以上文章来自于:http://bbs.54master.com/200898,1,1###
以下内容来自于:http://topic.csdn.net/t/20040930/09/3420583.html#
判断Excel文件有多少行和多少列
Dim lCols As Long
Dim lRows As Long
lCols = Sheet1.UsedRange.Cells.Columns.Count
lRows = Sheet1.UsedRange.Cells.Rows.Count
MsgBox "用户区有:" & lCols & "列 有:" & lRows & "行"
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
查找,麻烦
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询