Excel保存到TXT文件

时间:2010-08-30 20:18:39

标签: vb.net excel

我有这个脚本,我想另外保存为xls文件,也可以保存为同一目录中的.txt,甚至是另一个目录。我可以在这里获得一些指导吗?

Imports System.IO

Module Module1

 Private Property fs As Object
 Private Property BaseName As Object
 Private Property FullTargetPath As Object

Sub Main()

    Dim targetfolder As String = "C:\TEST"
    Dim sourcefolder As String = "\\10.97.8.16\c$\Checks\XMLFiles"
    Dim Searchpattern As String = String.Format("{0:MM-dd-yyyy}*.xml", Date.Today)
    Dim todaysfiles() As String = Directory.GetFiles(sourcefolder, Searchpattern)
    Dim xlApp, xlWkb
    xlApp = CreateObject("excel.application")
    fs = CreateObject("Scripting.FileSystemObject")
    Const xlnormal = 1

    'Extra Dims


    'Hide Excel
    xlApp.Visible = False

    For Each file As String In todaysfiles
        ' Excel stuff... ' 

        Dim fileName As String = IO.Path.GetFileNameWithoutExtension(file)

        ' Concatenate full path. Extension will be automatically added by Excel. ' 
        Dim fullTargetPath = IO.Path.Combine(targetfolder, fileName)

        'Process each file in SourceFolder
        ' For Each file In fs.GetFolder(SourceFolder).files
        'Open file in SourceFolder
        xlWkb = xlApp.Workbooks.Open(file)
        'Get Filename
        BaseName = fs.getbasename(file)
        'Concatenate full path. Extension will be automatically added by Excel
        fullTargetPath = targetfolder & "\" & BaseName
        'Save as XLS file into TargetFolder
        xlWkb.SaveAs(fullTargetPath, xlnormal)

        'Close the file after its done
        xlWkb.close()
    Next

    xlWkb = Nothing
    xlApp = Nothing
    fs = Nothing

    ' MsgBox("Thank you. Currently the Date is: " & Date.Today & " people like to eat chicken Every " & Date.Today.Ticks & " minutes.")


    'This is for extra code below

End Sub

结束模块

2 个答案:

答案 0 :(得分:0)

与您现在正在做的完全一样,但是将格式从xlnormal更改为xltext。

xlWkb.SaveAs(fullTargetPath, xltext)

在研究这样的事情时,我喜欢在vba编辑器下查看对象浏览器。在这种情况下查找Workbook对象,然后查找SaveAs子以获取定义(参数和类型),然后您可以单击要获取更多信息的参数并从帮助菜单中查找。我说从记忆中我多年没有玩过excel宏。祝你好运!

答案 1 :(得分:0)

为什么你不能打电话

xlWb.SaveAs(fullTargetPath, XlFileFormat.xlTextWindows)

或与任何其他xlFileFormat类型?