您的位置:首页 > 编程语言 > VB

vb.net 操作excel

2020-02-16 09:40 204 查看

Imports Microsoft.Office.Interop.Excel
Public Class Form1
Dim data_arr() As String

Sub excelConnection()
Dim newsht As Worksheet
Dim excelpath As String
Dim shtname As String
Dim wkbook As Workbook
Dim excelapp As Application
Dim teststr As String
Dim totalcells As Integer

excelapp = CreateObject("Excel.Application")

shtname = "testsht.xlsx"
excelpath = "D:\Backup\桌面\intentory_end_of_2016\"

wkbook = excelapp.Workbooks.Open(excelpath & shtname)
excelapp.Visible = False

newsht = wkbook.Sheets(1)
teststr = newsht.Range("A1").Text
totalcells = getlastcell(newsht)

'fill the array
ReDim data_arr(totalcells)

Dim i_this As Integer

For i_this = 1 To UBound(data_arr)
data_arr(i_this - 1) = newsht.Range("A" & i_this + 1).Text
Next

Me.Label1.Text = totalcells
Me.Label2.Text = teststr

'Debug.Print(totalcells)
'MsgBox(teststr)
'MsgBox(newsht.Name)

'lease the control of excel
wkbook.Close()
excelapp = Nothing
wkbook = Nothing
newsht = Nothing

End Sub
'get the max number of one column that is not empty
Public Function getlastcell(sht As Worksheet) As Int16
Dim i As Int16
i = 1

Do While (sht.Range("A" & i).Text <> "")
i = i + 1
Loop
getlastcell = i - 1
End Function

Private Sub FolderBrowserDialog1_HelpRequest(sender As Object, e As EventArgs) Handles FolderBrowserDialog1.HelpRequest

End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Call excelConnection()

End Sub

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged

Dim str As String

Me.ListBox1.Items.Clear()
If UBound(data_arr) > 0 And Me.TextBox1.Text <> "" Then
For Each str In data_arr

If str Like "*" + UCase(Me.TextBox1.Text) + "*" Then
Me.ListBox1.Items.Add(str)
End If
Next
End If
End Sub

Private Sub ListBox1_DoubleClick(sender As Object, e As EventArgs) Handles ListBox1.DoubleClick
Dim pdfpath As String
Dim selectedPartcode As String

pdfpath = "D:\Backup\桌面\intentory_end_of_2016\drawings_pdf\"
selectedPartcode = Me.ListBox1.SelectedItem.ToString
'MsgBox(pdfpath + selectedPartcode)
System.Diagnostics.Process.Start(pdfpath + selectedPartcode)

End Sub

End Class

  • 点赞
  • 收藏
  • 分享
  • 文章举报
Farewell_OldTimes 发布了4 篇原创文章 · 获赞 0 · 访问量 752 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: