当前位置:  开发笔记 > 程序员 > 正文

如何*快速*将许多.txt文件转换为.xls文件

如何解决《如何*快速*将许多.txt文件转换为.xls文件》经验,为你挑选了1个好方法。



1> brettdj..:

一些选项应该更快.

    使用Powershell(将以下代码保存在记事本中,如xx.ps1,更新源目录并运行)

    在隐藏的实例中而不是在当前的实例中自动执行Excel.

电源外壳

借鉴https://superuser.com/questions/875831/using-powershell-is-it-possible-to-convert-an-xlsx-file-to-xls并使用Powershell循环浏览Excel文件并检查电子表格名称存在

$files = Get-ChildItem C:\Temp\*.txt
Write "Loading Files..."

$Excel = New-Object -ComObject Excel.Application
$Excel.visible = $false
$Excel.DisplayAlerts = $false

ForEach ($file in $files)
{

     $WorkBook = $Excel.Workbooks.Open($file.Fullname)
     $NewFilepath = $file.Fullname -replace ".{4}$"
     $NewFilepath =  $NewFilepath + ".xls"
     $Workbook.SaveAs($NewFilepath,56)   

}
  Stop-Process -processname EXCEL
  $Excel.Quit()

自动化Excel

Sub TXTconvertXLS2()

Dim objExcel As Excel.Application
Dim wb As Workbook
Dim strFile As String
Dim strDir As String

Set objExcel = New Excel.Application
With objExcel
    .Visible = False
    .DisplayAlerts = False
End With

    'Directories
    strDir = "c:\temp\"
    strFile = Dir(strDir & "*.txt")

    'Loop
     Do While strFile <> ""
        Set wb = objExcel.Workbooks.Open(strDir & strFile)
            With wb
                .SaveAs Replace(wb.FullName, ".txt", ".xls"), 50
                .Close False   '<-already saved in the line directly above
            End With
        Set wb = Nothing
        strFile = Dir   '<- stuffs the next filename into strFile
    Loop

objExcel.DisplayAlerts = False
objExcel.Quit
Set objExel = Nothing

End Sub

推荐阅读
刘美娥94662
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有