导出 Excel 无法在 Azure Functions 中工作

问题描述 投票:0回答:1

我正在 Azure Functions (Powershell) 中导入 csv 文件,并且想将其导出为 excel 文件。我决定使用 Export-Excel 模块。不幸的是,当我将其添加到我的要求中时:

@{
    Az = '2.*'
    SqlServer = '21.1.18147'
    ImportExcel = '7.8.6'
}

我收到以下错误:

Result: Failure Exception: Failed to install function app dependencies. Error: 'The script file 'requirements.psd1' has parsing errors: Missing '=' operator after key in hash literal.

然后我下载了整个模块并将其上传到我的“Modules”文件夹(在 KUDU 上)。当我尝试导入模块时,我再次收到错误:

ERROR: The specified module 'Export-Excel' was not loaded because no valid module file was found in any module directory.

最后,我只是简单地获取了整个函数的代码(在这里找到:https://www.powershellgallery.com/packages/ImportExcel/1.90/Content/Export-Excel.ps1)并将其直接包含到我的代码中 - 然后当我尝试调用它时,出现以下错误:

ERROR: Unable to find type [OfficeOpenXml.Style.ExcelFillStyle].

有人曾设法在 Azure Functions 上使用此模块吗?还可能吗?

azure-functions azure-powershell
1个回答
0
投票

我已经在我的环境中重现了,以下是我的预期结果:

requirements.psd1:

@{
    'Az' = '11.*'
    'ImportExcel' = '7.8.4'
}

run.ps1:

using namespace System.Net

param($Request, $TriggerMetadata)
Install-Module -Name ImportExcel -Force
Import-Module -Name ImportExcel -Force

$csvData = @"
Name, Age, City
Rithwik, 23, Hyd
Bojja, 24, Sec
Chotu, 24, Island
"@

$testobj = ConvertFrom-Csv -InputObject $csvData
$tempExcelFile = New-TemporaryFile
$testobj | Export-Excel -Path $tempExcelFile.FullName -AutoSize -NoHeader -WorksheetName "Data"
$path=$tempExcelFile.FullName
Write-Output "Temporary file created at: $path"

Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
    StatusCode = [HttpStatusCode]::OK
    Body = $testobj
})

host.json:

{
  "version": "2.0",
  "managedDependency": {
    "Enabled": true
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[4.*, 5.0.0)"
  },
      "functionTimeout": "00:10:00"
}

Output:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.