获取excel文件中的单元格数据并将其保存到文本文件中

时间:2017-08-26 15:51:50

标签: excel vb.net text

我尝试从excel文件中获取单元格数据并将此数据保存在文本文件中,但在使用此代码后:

 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

文本文件中的结果为System.__ComObject

我应该在代码中进行哪些更正?

1 个答案:

答案 0 :(得分:0)

请尝试使用CellValue = XRng.ToString()

,而不是使用CellValue = XRng.Value