用vb读取Excel中某个单元格的数据

把c盘根目录中的a.xls中sheet1中b2单元格中的数字显示在text1中
2024-12-03 00:42:02
推荐回答(3个)
回答1:

单纯的说怎么读取很简单,假设你的控件名称为A,则  A.text = “sheet1.cells(2,2).value”

下面给出VB.NET 的完整代码:

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim DataSPath As String
        DataSPath = "C:\a.xls"
        Dim ExlApp = CreateObject("Excel.Application")
        Dim xlbook = ExlApp.Workbooks
        Dim xlbk = ExlApp.workbooks.Open(DataSPath)
        Dim St1 = xlbk.worksheets(1)
        TextBox1.Text = DataSPath & " 表1中 " & St1.cells(2, 2).value
        St1 = Nothing
        xlbook = Nothing
        ExlApp = Nothing
        MsgBox("OK", MsgBoxStyle.Exclamation, "Tudy")
    End Sub
End Class

回答2:

Private Sub Command1_Click()
Dim A As Integer
Dim scxls As New excel.Application
Dim scbook As excel.Workbook
Dim scsheet As excel.Worksheet
Set scxls = CreateObject("excel.application") '创建EXCEL对象
scxls.DisplayAlerts = False '不显示对话框,如果D:\2\1\3.xls文件已存在,让它不出现这个提示,就需加这段代码。
Set scbook = scxls.Workbooks.Open("D:\2\4.xls")
'Set scbook = scxls.Workbooks.Add '新建EXCEL工件簿文件
Set scsheet = scbook.Worksheets(1) '设置活动工作表
scxls.Visible = True '显示Excle

Text1.Text = scsheet.Cells(2, 2) '读取数据 并在text1里显示
Text2.Text = scsheet.Cells(2, 6) '读取数据 并在text1里显示
scsheet.SaveAs ("D:\2\1\3.xls") 'excel 存档 语法:表达式.SaveAs(FileName, FileFormat, Password, WriteResPassword, ReadOnlyRecommended, CreateBackup, AccessMode, ConflictResolution, AddToMru, TextCodepage, TextVisualLayout, Local)
scxls.Quit 'excel 关闭

End Sub

回答3:

range("xx").value或者 cells(x,x).value