Excel开发学习笔记:查找与创建worksheet
2015-12-09 00:05
465 查看
开发环境基于VSTO,具体配置:visual studio 2010,VB .Net,excel 2007,文档级别的定制程序。如题,我在ThisWorkbook.vb中添加了一个public函数来完成查找功能。
入参:待查找的sheet名称
返回:如果存在则返回worksheet对象,如果不存在则返回nothing Public Function WorksheetExist(name As String) As Excel.Worksheet
Try
Dim existSheet As Excel.Worksheet = Globals.ThisWorkbook.Sheets(name)
Return existSheet
Catch ex As Exception
Return Nothing
End Try
End Function
创建worksheet函数,要求在现有sheet的尾部添加新的sheet。 入参:Workbook,新sheet名
返回:成功则返回新的sheet对象,失败返回nothing
Private Function CreatWorksheet(ByRef book As Excel.Workbook, ByRef name As String) As Excel.Worksheet
Dim newSheet As Excel.Worksheet = book.Sheets.Add(After:=book.Worksheets(book.Sheets.Count))
Try
newSheet.Name = name
Return newSheet
Catch ex As Exception
MsgBox("""" + name + """" + "不能被用作excel工作表(sheet)名称")
newSheet.Delete()
Return Nothing
End Try
End Function
查询与创建连起来使用,未查找到则创建的代码片段:
Dim algoSheet As Excel.Worksheet = Globals.ThisWorkbook.WorksheetExist(name)
If algoSheet Is Nothing Then
algoSheet = CreatWorksheet(Globals.ThisWorkbook.Application.Workbooks(Globals.ThisWorkbook.Name), name)
If (algoSheet Is Nothing) Then
Continue For
End If
End If
入参:待查找的sheet名称
返回:如果存在则返回worksheet对象,如果不存在则返回nothing Public Function WorksheetExist(name As String) As Excel.Worksheet
Try
Dim existSheet As Excel.Worksheet = Globals.ThisWorkbook.Sheets(name)
Return existSheet
Catch ex As Exception
Return Nothing
End Try
End Function
创建worksheet函数,要求在现有sheet的尾部添加新的sheet。 入参:Workbook,新sheet名
返回:成功则返回新的sheet对象,失败返回nothing
Private Function CreatWorksheet(ByRef book As Excel.Workbook, ByRef name As String) As Excel.Worksheet
Dim newSheet As Excel.Worksheet = book.Sheets.Add(After:=book.Worksheets(book.Sheets.Count))
Try
newSheet.Name = name
Return newSheet
Catch ex As Exception
MsgBox("""" + name + """" + "不能被用作excel工作表(sheet)名称")
newSheet.Delete()
Return Nothing
End Try
End Function
查询与创建连起来使用,未查找到则创建的代码片段:
Dim algoSheet As Excel.Worksheet = Globals.ThisWorkbook.WorksheetExist(name)
If algoSheet Is Nothing Then
algoSheet = CreatWorksheet(Globals.ThisWorkbook.Application.Workbooks(Globals.ThisWorkbook.Name), name)
If (algoSheet Is Nothing) Then
Continue For
End If
End If
相关文章推荐
- 快速排序--过程详解
- 求Sn=a+aa+aaa+aaaa+aaaaa的前5项之和,其中a是一个数字,例如:2+22+222+2222+22222
- Android Studio 出现非法字符 'ufeff'错误
- Python异常
- windows下安装python包管理器pip
- Linux磁盘系统 - 管理磁盘的命令
- 仅1个例子轻松学习正则表达式
- PHP判断手机是IOS还是Android
- 学习php设计模式 php实现观察者模式(Observer)
- 学习php设计模式 php实现备忘录模式(Memento)
- 作为程序员必知的16个最佳PHP库
- 微信支付PHP SDK之微信公众号支付代码详解
- 分享php代码将360浏览器导出的favdb的sqlite数据库文件转换为html
- thinkPHP使用post方式查询时分页失效的解决方法
- linux grep命令技巧
- js获取鼠标位置实例详解
- 深入探讨前端框架react
- 详解js跨域原理以及2种解决方案
- js实现跨域访问的三种方法
- JavaScript知识点整理