cetee
級(jí)別: 略有小成
精華主題: 0
發(fā)帖數(shù)量: 73 個(gè)
工控威望: 218 點(diǎn)
下載積分: 10544 分
在線時(shí)間: 580(小時(shí))
注冊(cè)時(shí)間: 2010-08-05
最后登錄: 2024-10-30
查看cetee的 主題 / 回貼
樓主  發(fā)表于: 2018-09-08 17:55
vb.net 求助,請(qǐng)大家?guī)兔。謝謝

Imports excel = Microsoft.Office.Interop.Excel

Imports System.IO
Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs)

    End Sub
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub
    Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
        Dim fileTest As String = "c:\Temp\ExcelTest\test.xlsx"
        If File.Exists(fileTest) Then
            File.Delete(fileTest)
        End If

        Dim oexcel As Object
        oexcel = CreateObject("excel.Application")
        Dim obook As excel.Workbook
        Dim osheet As excel.Worksheet

        obook = oexcel.Workbooks.Add
        osheet = oexcel.Worksheets(1)

        osheet.Name = "test name"
        osheet.Range("A").Value = "button 1"
        obook.SaveAs(fileTest)
        obook.Close()
        obook = Nothing
        oexcel.Quit()
        oexcel = Nothing
    End Sub
End Class


附件: 報(bào)錯(cuò)資料.pdf (141 K) 下載次數(shù):16
網(wǎng)站提示: 請(qǐng)不要用迅雷下載附件,容易出錯(cuò)
不小心遇見(jiàn)你
自動(dòng)項(xiàng)目、PLC、視覺(jué)、通訊等工控軟件開(kāi)發(fā)QQ3515716
級(jí)別: 工控俠客

精華主題: 3 篇
發(fā)帖數(shù)量: 2036 個(gè)
工控威望: 2953 點(diǎn)
下載積分: 25161 分
在線時(shí)間: 1427(小時(shí))
注冊(cè)時(shí)間: 2014-01-05
最后登錄: 2024-11-06
查看不小心遇見(jiàn)你的 主題 / 回貼
1樓  發(fā)表于: 2018-09-08 19:42
你是對(duì)某一表格讀寫(xiě)吧?
osheet.Range("A").Value是讀取  
再說(shuō)你這個(gè)也沒(méi)指定那個(gè)表格 默認(rèn)讀取sheet1
再說(shuō)也應(yīng)該是
dim str1 as string
str1=osheet.Range("A1").Value 'A是列號(hào) 1是行號(hào)
正確的寫(xiě)法
dim str1 as string
AppXls.Workbooks(1).Sheets("Sheet1").Cells(1, 1).Value = "這是寫(xiě)入的"    '這是寫(xiě)入  用Cells(A, 1)也可
str1 = AppXls.Workbooks(1).Sheets("Sheet1").Cells(1, 1).Value      '這是讀取  用Cells(A, 1)也可
MsgBox(str1 )
雖然沒(méi)用過(guò)VB net 應(yīng)該和vb語(yǔ)法很接近 C#也差不多是這樣
項(xiàng)目、視覺(jué)、通訊QQ3515716