简体   繁体   中英

Get cell's data in excel file and save it to text file

I try to take cell's data from excel file and save this data on text file, but after I use this code:

 Imports System.IO 
 Imports Microsoft.Office.Interop 
 Imports Microsoft.Office.Interop.Excel

Public Class Form1
Private myWriter As New StreamWriter("C:\myFile.txt")
Dim appXL As Excel.Application
Dim wbXL As Excel.Workbook
Dim shXL As Excel.Worksheet
Dim XRng As Excel.Range
Dim CellValue As String

Private Sub btnRead_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnRead.Click

    appXL = New Excel.Application
    wbXL = appXL.Workbooks.Open("C:\Worksheet.xlsx")
    appXL.Visible = True
    appXL.Workbooks.Add(Type.Missing)
    shXL = wbXL.Worksheets(1)
    shXL.Visible = True
    XRng = CType(appXL.ActiveSheet, Excel.Worksheet).Range("A1").Value2
    CellValue = XRng.ToString()
    myWriter.WriteLine(CellValue)
    myWriter.Close()
    wbXL.Close()
    appXL.Quit()
End Sub
End Class

The result in text file was System.__ComObject

What should I make correction in the code?

代替使用CellValue = XRng.ToString() ,请尝试使用CellValue = XRng.Value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM