如何用VB.NET搜索excel [英] How to search in excel with VB.NET
本文介绍了如何用VB.NET搜索excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Imports Excel = Microsoft.Office.Interop.Excel
公开 类 Form1
私有 Sub Button1_Click(发件人作为 对象,e As EventArgs)句柄 Button1.Click
Dim appXL As Excel.Application
Dim wbXl As Excel.Workbook
Dim shXL As Excel.Worksheet
Dim raXL As Excel.Range
' 启动Excel和获取应用程序对象。
appXL = CreateObject( Excel.Application)
appXL.Visible = True
' 添加新工作簿。
wbXl = appXL.Workbooks.Add
shXL = wbXl.ActiveSheet
' 逐个单元格添加表格标题。
shXL.Cells( 1 , 1 )。值= 名字
shXL.Cells( 1 , 2 )。Value = 姓氏
shXL.Cells( 1 , 3 )。值= 全名
shXL.Cells( 1 , 4 )。值= 专业化
格式A1:D1为粗体,垂直对齐=居中。
使用 shXL.Range( A1, D1)
.Font.Bold = True
.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter
结束 使用
' 创建一个数组,一次设置多个值。
昏暗学生( 5 , 2 )作为 字符串
学生( 0 , 0 )= Zara
学生( 0 , 1 )= 阿里
学生( 1 , 0 )= Nuha
学生( 1 , 1 )= < span class =code-string> Ali
学生( 2 , 0 )= Arilia
学生( 2 , 1 )= RamKumar
学生( 3 , 0 )= Rita
学生( 3 , 1 )= 琼斯
学生( 4 , 0 )= Umme
学生( 4 , 1 )= Ayman
' 使用值数组(名字和姓氏)填充A2:B6。
shXL.Range( A. 2, B6)。值=学生
' 用相对公式填充C2:C6(= A2& & B2)。
raXL = shXL.Range( C2,< span class =code-string> C6)
raXL.Formula = = A2&& B2
' < span class =code-comment>填充D2:D6值。
使用 shXL
.Cells( 2 , 4 )。值= Biology
.Cells( 3 , 4 ).Value = Mathmematics
.Cells( 4 , 4 )。值= 物理学
.Cells( 5 , 4 )。Value = Mathmematics
.Cells( 6 , 4 )。值= 阿拉伯语
结束 使用
' AutoFit列A:D。
raXL = shXL.Range( A1, D1)
raXL.EntireColumn.AutoFit()
' 确保Excel可见并为用户提供控制
Excel的生命周期。
a ppXL.Visible = True
appXL.UserControl = True
发布对象引用。
raXL = 没什么
shXL = 没什么
wbXl = 没什么
appXL.Quit( )
appXL = Nothing
退出 Sub
Err_Handler:
MsgBox(Err.Description,vbCritical, 错误:& Err.Number)
结束 Sub
结束 班级
我是什么尝试过:
如何搜索并获取行号和列号
解决方案
Range.Find方法(Microsoft.Office.Interop.Excel) [ ^ ]。
Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim appXL As Excel.Application
Dim wbXl As Excel.Workbook
Dim shXL As Excel.Worksheet
Dim raXL As Excel.Range
' Start Excel and get Application object.
appXL = CreateObject("Excel.Application")
appXL.Visible = True
' Add a new workbook.
wbXl = appXL.Workbooks.Add
shXL = wbXl.ActiveSheet
' Add table headers going cell by cell.
shXL.Cells(1, 1).Value = "First Name"
shXL.Cells(1, 2).Value = "Last Name"
shXL.Cells(1, 3).Value = "Full Name"
shXL.Cells(1, 4).Value = "Specialization"
' Format A1:D1 as bold, vertical alignment = center.
With shXL.Range("A1", "D1")
.Font.Bold = True
.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter
End With
' Create an array to set multiple values at once.
Dim students(5, 2) As String
students(0, 0) = "Zara"
students(0, 1) = "Ali"
students(1, 0) = "Nuha"
students(1, 1) = "Ali"
students(2, 0) = "Arilia"
students(2, 1) = "RamKumar"
students(3, 0) = "Rita"
students(3, 1) = "Jones"
students(4, 0) = "Umme"
students(4, 1) = "Ayman"
' Fill A2:B6 with an array of values (First and Last Names).
shXL.Range("A2", "B6").Value = students
' Fill C2:C6 with a relative formula (=A2 & " " & B2).
raXL = shXL.Range("C2", "C6")
raXL.Formula = "=A2 & "" "" & B2"
' Fill D2:D6 values.
With shXL
.Cells(2, 4).Value = "Biology"
.Cells(3, 4).Value = "Mathmematics"
.Cells(4, 4).Value = "Physics"
.Cells(5, 4).Value = "Mathmematics"
.Cells(6, 4).Value = "Arabic"
End With
' AutoFit columns A:D.
raXL = shXL.Range("A1", "D1")
raXL.EntireColumn.AutoFit()
' Make sure Excel is visible and give the user control
' of Excel's lifetime.
appXL.Visible = True
appXL.UserControl = True
' Release object references.
raXL = Nothing
shXL = Nothing
wbXl = Nothing
appXL.Quit()
appXL = Nothing
Exit Sub
Err_Handler:
MsgBox(Err.Description, vbCritical, "Error: " & Err.Number)
End Sub
End Class
What I have tried:
how to search and get row number and column number
解决方案
Range.Find method (Microsoft.Office.Interop.Excel)[^].
这篇关于如何用VB.NET搜索excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文