我是靠谱客的博主 现代小土豆,最近开发中收集的这篇文章主要介绍方法range作用于对象worksheet时失败_VB.NET Excel操作类(获取工作簿列表和工作表列表及工作表对象)...,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

效果展示

12d04844635dea94fb72b13ddb4842e6.gif

引用excel类
Imports Microsoft.Office.Interop

Excel类代码开始
Public Class Cls_excel

返回Excel对象

'''
''' 返回Excel对象
'''
'''
Private Shared Function Xlapp() As Excel.Application
Try
Return GetObject(, "Excel.Application")
Catch ex As Exception
MsgBox("连接Excel失败!", MsgBoxStyle.Critical, "警告")
Return Nothing
End Try
End Function
返回工作簿列表
    ''' 
''' 返回工作簿列表
'''
'''
Public Shared Function Get_worklist() As ArrayList
Try
Dim arr As New ArrayList
For Each XL In Xlapp.Workbooks
arr.Add(XL.name)
Next
Return arr
Catch ex As Exception
Return Nothing
End Try
End Function

返回工作表列表
    ''' 
''' 返回工作表列表
'''
'''
Public Shared Function Get_sheetlist(ByVal book As String) As ArrayList
Try
Dim arr As New ArrayList
Dim xll As Excel.Workbook = Xlapp.Workbooks(book)
For Each XL In xll.Worksheets
arr.Add(XL.name)
Next
Return arr
Catch ex As Exception
Return Nothing
End Try
End Function
返回指定工作表对象
    ''' 
''' 返回选中的工作表
'''
'''
Public Shared Function XlSheet(ByVal boname As String, ByVal shname As String) As Excel.Worksheet
Try
Dim xlll As Excel.Workbook = Xlapp.Workbooks(boname)
xlll.Activate()
Dim xlsh As Excel.Worksheet = xlll.Worksheets(shname)
xlsh.Activate()
Return xlsh
Catch ex As Exception
Return Nothing
End Try
End Function
End Class

使用方法:

Public Class Form1    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click        ListBox1.DataSource = Cls_excel.Get_worklist    End Sub    Private Sub ListBox1_Click(sender As Object, e As EventArgs) Handles ListBox1.Click        ListBox2.DataSource = Cls_excel.Get_sheetlist(ListBox1.SelectedItem)    End Sub    Private Sub ListBox2_Click(sender As Object, e As EventArgs) Handles ListBox2.Click        Dim XL As Object = Cls_excel.XlSheet(ListBox1.SelectedItem, ListBox2.SelectedItem)        Label2.Text = XL.Name & vbCrLf & "A1单元格的值:" & XL.RANGE("a1").value    End SubEnd Class

今天教程到此结束啦!

9ba0a69b577861f703e4a43f4205ad0e.png

最后

以上就是现代小土豆为你收集整理的方法range作用于对象worksheet时失败_VB.NET Excel操作类(获取工作簿列表和工作表列表及工作表对象)...的全部内容,希望文章能够帮你解决方法range作用于对象worksheet时失败_VB.NET Excel操作类(获取工作簿列表和工作表列表及工作表对象)...所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(47)

评论列表共有 0 条评论

立即
投稿
返回
顶部