请问何如使用VB将一数据文本文件导入 EXCEL ?
abc.TXT
Tom    1000    3.15
Jone   300     4.00
Mike   415     0.89

解决方案 »

  1.   

    '先引用Microsoft Excel Object LibrayPrivate Sub Form_Load()
    Dim i As Integer
    Dim intRow As Integer
    Dim intColumn As Integer
    Dim intfnum As Integer
    Dim strLine As String
    Dim varLine As VariantDim xlApp As New Excel.Application
    Dim xlWorkbook As Excel.Workbook    intfnum = FreeFile() '打开文本文件
        Open "c:\abc.txt" For Input As #intfnum    Set xlWorkbook = xlApp.Workbooks.Add    intRow = 0
        Do While Not EOF(intfnum)
            intRow = intRow + 1
            Line Input #intfnum, strLine '读文本文件一行
            varLine = Split(strLine, " ") '以空格为分隔符
            intColumn = 0
            For i = 0 To UBound(varLine)
                If varLine(i) <> vbNullString Then
                    intColumn = intColumn + 1
                    xlWorkbook.Worksheets(1).Cells(intRow, intColumn) = varLine(i)
                End If
            Next
        Loop    xlWorkbook.SaveAs "c:\abc.xls" '保存Excel文件后退出
        xlApp.Quit
        Set xlApp = Nothing
        Set xlWorkbook = Nothing    Close '关闭文本文件    Unload Me
        
    End Sub
      

  2.   

    Thank you.
    Wait me trying...
      

  3.   

    '先引用Microsoft Excel Object Libray
    没有找到  Microsoft Excel Object Libray
    我用的是 OfficeXP
      

  4.   

    引用的操作步骤:
    选择VB菜单 工程(Project)—— >引用(Reference)—— >Microsoft Excel 10.0 Object Libray,按确定(OK)。
      

  5.   

    怎么没有Microsoft Excel 10.0 Object Libray呀?
      

  6.   

    Private Sub Form_Load()
    Dim i As Integer
    Dim intRow As Integer
    Dim intColumn As Integer
    Dim intfnum As Integer
    Dim strLine As String
    Dim varLine As VariantDim xlApp As Object
    Dim xlWorkbook As Object    intfnum = FreeFile() '打开文本文件
        Open "c:\abc.txt" For Input As #intfnum    set xlApp=CreateObject ("Excel.Application")
        set xlWorkbook = xlApp.Workbooks.Add    intRow = 0
        Do While Not EOF(intfnum)
            intRow = intRow + 1
            Line Input #intfnum, strLine '读文本文件一行
            varLine = Split(strLine, " ") '以空格为分隔符
            intColumn = 0
            For i = 0 To UBound(varLine)
                If varLine(i) <> vbNullString Then
                    intColumn = intColumn + 1
                    xlWorkbook.Worksheets(1).Cells(intRow, intColumn) = varLine(i)
                End If
            Next
        Loop    xlWorkbook.SaveAs "c:\abc.xls" '保存Excel文件后退出
        xlApp.Quit
        Set xlApp = Nothing
        Set xlWorkbook = Nothing    Close '关闭文本文件    Unload Me
        
    End Sub
      

  7.   

    Ctrl+T 后无法看到 Microsoft Excel 10.0 Object Libray
      

  8.   

    Ctrl+T出来的是部件,里面当然没有Libray
    你是按照我2003-5-16 19:29:52 介绍的步骤做的吗?
      

  9.   

    引用的操作步骤:
    选择VB菜单 工程(Project)—— >引用(Reference)—— >Microsoft Excel 10.0 Object Libray,按确定(OK)。没有找到。
      

  10.   

    你按照 2003-5-19 7:24:56 的方法不需要引用,除非你Office装的有问题。
    到此为止。