更新:我刚发现有一个功能更强大的服务器的人正在处理我被分配的任务,所以我没有让这个程序足够快。但是,下面的答案(自动化Excel)有助于使程序快三倍,因此我建议将其推荐给文件较少(但仍然很多)的人。
我试图将许多(超过300,000).txt文件转换为.xls文件。我在这里找到了如何做到这一点:
Batch Convert TXT to XLS Using VBA
但它真的很慢(超过一个小时,它只转换了我们300,000个文件中的200个),即使文件不是很大。
我尝试通过关闭ScreenUpdating来加快速度,但我无法成功关闭ScreenUpdating。有人可以解释在哪里关闭ScreenUpdating,以便我的代码运行得更快?或者,更好的是,任何更有效的计划的想法?
以下是代码:
start="101010"
end="999999"
let str=$start-1
let en=$end+1
echo $str $en
101009 1000000
答案 0 :(得分:5)
有两个选项应该更快。
Powershell
(将以下代码保存在记事本中,如 xx.ps1 ,更新源目录并运行)Powershell的
借鉴https://superuser.com/questions/875831/using-powershell-is-it-possible-to-convert-an-xlsx-file-to-xls和Using Powershell to loop through Excel files and check if Spreadsheet name exists
$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
答案 1 :(得分:0)
要从CSV转换为XLSX,请使用brettdj示例的以下powershell变体:
#Set-ExecutionPolicy Unrestricted
$files = Get-ChildItem C:\test\*.csv
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 + ".xlsx"
$Workbook.SaveAs($NewFilepath,51)
}
$Excel.Quit()
Stop-Process -processname EXCEL