使用VB2012读取Excel文件需要先添加引用:
解决方案右键-添加引用-COM-类型库,中勾选Microsoft Excel引用项,如图:
这是Excel2010的引用项,不同的office版本可能会有差别,注意找一下。
在代码中添加引用,注意一定是在代码开始位置,(Public Class之前)
Imports MicrosoftOfficeInterop打开、写入、读取、关闭Excel文档的代码如下:
Public Class frmExcelOperateDim xapp As ExcelApplication '定义Excel进程
Dim strFilePath As String '定义Excel文档位置
'打开Excel文档
Private Sub btnOpen_Click(sender As Object, e As EventArgs) Handles btnOpenClick
xapp = New ExcelApplication() '新建Excel进程
strFilePath = "E:\ExcelOperatexlsx" '指定Excel文件的位置
xappVisible = False '设置Excel进程不可见
xappDisplayAlerts = False '屏蔽Excel的提示,使用程序接管
xappWorkbooksOpen(strFilePath) '打开指定的Excel文件
End Sub
'添加数据
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAddClick
'xappWorkbooksAdd() '添加工作簿,默认使用第一个工作表
xappCells(Val(txtAddRowText), Val(txtAddColText)) = txtAddDataText '将数据写入指定单元格
End Sub
'读取数据
Private Sub btnRead_Click(sender As Object, e As EventArgs) Handles btnReadClick
txtReadDataText = xappCells(Val(txtReadRowText), Val(txtReadColText))valueToString '从指定单元格读取数据
End Sub
'关闭Excel文档
Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnCloseClick
xappWorkbooksClose() '关闭工作簿
xappQuit() '退出Excel进程
'遍历系统中名为excel的进程,如果是本程序创建的进程,其MainWindowTitle属性应为空值,清理所有符合条件的进程
'用户自己打开的Excel进程不受影响
For Each p As Process In ProcessGetProcessesByName("excel")
If StringIsNullOrEmpty(pMainWindowTitle) Then
pKill()
End If
Next
End Sub
End Class
将生成的exe放入文件夹
private
txtname()
as
string
'定义一个数组装入txt名称
dim
spath
as
string
sub
gettxtname(byval
spath
as
string,
byval
filter
as
string)
'这是获取指定文件夹下指定后缀名的文件名称的过程,装入数组txtname()中,spath就是这里写放txt的文件夹路径
dim
sdir
as
string
dim
sfilter()
as
string
dim
lngfilterindex
as
long
dim
lngindex
as
long
sfilter
=
split(filter,
",")
if
right(spath,
1)
<>
"\"
then
spath
=
spath
&
"\"
for
lngfilterindex
=
lbound(sfilter)
to
ubound(sfilter)
sdir
=
dir(spath
&
sfilter(lngfilterindex))
do
while
len(sdir)
>
0
lngfiles
=
lngfiles
+
1
redim
preserve
txtname(1
to
lngfiles)
txtname(lngfiles)
=
sdir
sdir
=
dir
loop
next
end
sub
private
sub
command1_click()
dim
i%
spath
=
apppath
&
"\"
call
gettxtname(spath,
"html")
for
i
=
1
to
ubound(txtname)
name
spath
&
txtname(i)
as
spath
&
left(txtname(i),
instr(txtname(i),
"")
-
1)
&
"pdf"
next
msgbox
"done!"
end
sub
Sub xxx() WorkbooksOpen ApplicationGetOpenFilename("Excel文件(xlsx),xlsx") ActiveWorkbookClose 1 End Sub 部分自己增加代码。
麻烦采纳,谢谢!
思路:
1取得excel对象,然后控制该对象,读取该对象的所有工作表,并写入listbox,
2当在用户listbox选取某一工作表时,用ADODC控件连接excel,再把DataGrid绑定ADODC控件,通过DataGrid显示表内的数据
详细方法如下
建一个窗体:
增加listbox,dataGrid,adodc
控件各1个
窗体内代码如下
Option
Explicit
Public
path
As
String
'excel文件路径
Private
Sub
Form_Load()
Dim
myexcel
As
New
ExcelApplication
Dim
mybook
As
Workbook
Dim
mysheet
As
Worksheet
Dim
su,
j
As
Long
'excel表路径
path
=
"D:\我的文档\3000条新书无资料11_3xls"
Set
mybook
=
myexcelWorkbooksOpen(path)
'遍历每个工作表,取出所有表名
Dim
a
As
Worksheet
For
Each
a
In
mybookSheets
'在此循环里,你可以添加到listbox控件
List1AddItem
aName
'
MsgBox
aName
'显示工作表的名称
'
MsgBox
aRange("a1")
'显示A1单元格
Next
mybookClose
Set
mysheet
=
Nothing
Set
mybook
=
Nothing
Set
myexcel
=
Nothing
End
Sub
Sub
List1_Click()
'单击listbox的表名时
Dim
Conn
As
New
ADODBConnection
Dim
rs
As
New
ADODBRecordset
Dim
Sql
As
String,
strSheetName
As
String
strSheetName
=
List1Text
Sql
=
"select
from
["
&
strSheetName
&
"$]"
'指定EXCEL表
Adodc1ConnectionString
=
"Provider=MicrosoftJetOLEDB40;Persist
Security
Info=False;Data
Source="
&
path
&
";Extended
Properties='Excel
80;HDR=Yes'"
'连接EXCEL文件
Adodc1RecordSource
=
Sql
Set
DataGrid1DataSource
=
Adodc1
Adodc1Refresh
End
Sub
'引用:
'添加对该对象的引用 工程菜单-->引用,找到 Microsoft Scripting Runtime
'引用microsoft Excel 140 object library
'
Dim elApp As ExcelApplication
Dim elBooks As ExcelWorkbook
Dim ekSheet As ExcelWorksheet
Dim TblMap_Card '创建一个变量
Private Sub Command1_Click()
Dim i As Integer
openEl
Set dic = CreateObject("ScriptingDictionary")
'MsgBox ekSheetCells(RowsCount, 1)End(3)Row
For i = 2 To ekSheetCells(RowsCount, 1)End(3)Row
If dicExists(ekSheetCells(i, 2)Value) Then
dic(ekSheetCells(i, 1)Value) = dic(ekSheetCells(i, 1)Value) + ekSheetCells(i, 2)Value
Else
dic(ekSheetCells(i, 1)Value) = ekSheetCells(i, 2)Value
End If
Next i
ekSheetRange("H:J")Clear
'ekSheetCells(1, 9)Resize(1, 2) = Array("商品", "售量")
ekSheetCells(2, 9)Resize(dicCount, 1) = ApplicationTranspose(dicKeys)
ekSheetCells(2, 10)Resize(dicCount, 1) = ApplicationTranspose(dicItems)
End Sub
Private Sub openEl()
Dim myPath As String
myPath = "\weekxlsx"
Set elApp = CreateObject("ExcelApplication")
Set elBooks = elAppWorkbooksOpen(AppPath & myPath)
Set ekSheet = elBooksWorksheets("Sheet1")
'Set ekSheet = elBooksWorksheets(1)
elAppVisible = True
End Sub
VB6的,测试通过。界面上只有一个按钮。
以上就是关于怎么在vb2012中调用excel文件全部的内容,包括:怎么在vb2012中调用excel文件、VB如何提取文件后缀、VB读取2007Excel的源码等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)