如何使用 .NET Core 中的 Syncfusion Xlsio 从 url 将图像添加到 XLSX 文件

How to add an image to an XLSX file from a url using Syncfusion Xlsio in .NET Core

我有一个 .NET Core 2.2 API,我需要用数据和图像填充 XLSX 文件。我正在使用 Syncfusion Xlsio 17.4.0.5 来执行此操作。除图像外,该文件填充得很好。该图像位于 url,因此我需要从 url 下载它并将其插入到文件中。这是我目前的尝试:

HttpClient client = new HttpClient();
using (Stream stream = await client.GetStreamAsync(imageUrl))
{
    worksheet.Pictures.AddPicture(1, 1, stream);
}

当我 运行 这样做时,我在 AddPicture 行上收到以下错误:

{System.ArgumentException: Stream
   at Syncfusion.Drawing.Image..ctor(Stream stream)
   at Syncfusion.XlsIO.Implementation.Collections.PicturesCollection.AddPicture(Int32 topRow, Int32 leftColumn, Stream stream, ExcelImageFormat imageFormat)
   at MyProject.App.Services.LadingService.PopulateForm(IWorksheet worksheet, Lading lading) in C:\Users\jlewi\Source\GIT\my-project-backend\src\MyProject.App\Services\LadingService.cs:line 94
   at MyProject.App.Services.LadingService.GetLadingExport(String ladingId) in C:\Users\jlewi\Source\GIT\my-project-backend\src\MyProject.App\Services\LadingService.cs:line 58
   at MyProject.Api.Controllers.LadingController.ExportLading(String id) in C:\Users\jlewi\Source\GIT\my-project-backend\src\MyProject.Api\Controllers\LadingController.cs:line 147
   at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync()
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync()
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext)
   at MyProject.Api.Middleware.UserBlockedMiddleware.InvokeAsync(HttpContext context, IUserRepository userRepository) in C:\Users\jlewi\Source\GIT\my-project-backend\src\MyProject.Api\Middleware\UserBlockedMiddleware.cs:line 72
   at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
   at AspNetCoreRateLimit.RateLimitMiddleware`1.Invoke(HttpContext context)
   at AspNetCoreRateLimit.RateLimitMiddleware`1.Invoke(HttpContext context)
   at IdentityServer4.Hosting.IdentityServerMiddleware.Invoke(HttpContext context, IEndpointRouter router, IUserSession session, IEventService events) in C:\local\identity\server4\IdentityServer4\src\IdentityServer4\src\Hosting\IdentityServerMiddleware.cs:line 72
   at IdentityServer4.Hosting.MutualTlsTokenEndpointMiddleware.Invoke(HttpContext context, IAuthenticationSchemeProvider schemes) in C:\local\identity\server4\IdentityServer4\src\IdentityServer4\src\Hosting\MtlsTokenEndpointMiddleware.cs:line 60
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at IdentityServer4.Hosting.BaseUrlMiddleware.Invoke(HttpContext context) in C:\local\identity\server4\IdentityServer4\src\IdentityServer4\src\Hosting\BaseUrlMiddleware.cs:line 36
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
   at VOGBackend.Core.Middleware.ApiExceptionHandlerMiddleware.InvokeAsync(HttpContext context, ILogger`1 logger, IHostingEnvironment hostingEnvironment) in C:\Users\jlewi\Source\GIT\sharedlibrary.core\src\Middleware\ApiExceptionHandlerMiddleware.cs:line 36}

我已经在其他图像 url 和 none 上尝试过。有什么想法吗?

更新 好的,下面的工作但感觉有点老套。也许 AddPicture 不喜欢使用纯 Stream 对象?

HttpClient client = new HttpClient();
using (Stream stream = await client.GetStreamAsync(url))
{
    using (MemoryStream memStream = new MemoryStream())
    {
        stream.CopyTo(memStream);
        var shape = worksheet.Pictures.AddPicture(1, 1, memStream);
    }
}

来自 Syncfusion 的问候。

Syncfusion XlsIO 不支持在 ASP.NET 核心平台的 AddPicture 方法中读取 HttpConnection.ContentLengthReadStream

因此,我们要求您将其复制到您提到的 MemoryStream 中,以克服异常。

注意:我为 Syncfusion 工作。

此致,
莫涵.