PHP shell_exec System.OutOfMemoryException

PHP shell_exec System.OutOfMemoryException

我使用 PowerShell 模块 ImportExcel 将 Excel 文件转换为 csv 文件:

exportCSV.ps1:

Param(
    [string]$cInput,
    [string]$cOutput,
    [string]$cSheet
)

if (!$cInput -Or !$cOutput -Or !$cSheet) { Write-Host "failed" }
else {
    try {
        Import-Excel -WorksheetName $cSheet -Path $cInput -NoHeader | Export-Csv $cOutput -NoTypeInformation -encoding utf8
        Write-Host "done"
    } catch {
        Write-Host $error
    }
    
}

用于 运行 脚本的命令:

powershell -File "D:\test\exportCSV.ps1" -cInput "D:\test\testFile.xlsx" -cOutput "D:\test\testFile.csv" -cSheet "Sheet 1"

当我直接在 cmd 中 运行 这个脚本时,它运行没有任何问题。但是当我 运行 在 php 和 shell_exec() 中时,我得到以下错误:

Exception calling "Load" with "1" argument(s): "Exception of type 'System.OutOfMemoryException' was thrown." 

testFile.xlsx 有 91,094 KB。当我使用一个只有 27,307 KB 的文件时,它也适用于 PHP 的 shell_exec()。我已经检查了 php.ini (256M) 中的 memory_limit,所以这应该不是问题。

文件在磁盘上占用的内存量不一定与导入后数据对象的大小相匹配。特别是如果 xslx 文件被压缩(不知道它们是否被压缩)

我建议暂时将 memory_limit 提高到 1024M,然后使用 testFile.xlsx 再次尝试以确保不需要更多 space 来将导入的数据存储在内存中.

如果这不起作用,则可能是导入程序中的错误正在浪费内存。