ljtydh
ljtydh

Reputation: 11

How to integrate COM Interop (manipulations of Excel, Ms Word, etc) in a web-API written in C#?

I am trying to integrate COM interop into my web API written in C#, and it seems that .net core of the web API cannot support COM technology.

Here's the error message I got:

PlatformNotSupportedException: COM is not supported
FlicApi.Controllers.ButtonController.Post(string buttonId)
lambda_method(Closure , object , object[] )
Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(object target, object[] parameters)
Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor+SyncObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, object controller, object[] arguments)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync()
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync()
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

Is there any way to bypass this situation that COM is not supported on net-core for web API? Eventually what I want to achieve is call an API and do some manipulations on the Microsoft Excel.

Here's what I tried:

// POST api/button/buttonId
[HttpPost("{buttonId}")]
public ActionResult<string> Post(string buttonId)
{
    //Console.WriteLine(value);

    //start Excel and get Application Object
    oXL = new Excel.Application();
    oXL.Visible = true;
    //Get a new workbook.
    oWB = oXL.Workbooks.Add(Missing.Value);
    oSheet = (Excel._Worksheet)oWB.ActiveSheet;

    //Add table headers going cell by cell.
    oSheet.Cells[1, 1] = "First Name";
    oSheet.Cells[1, 2] = "Last Name";
    oSheet.Cells[1, 3] = "Full Name";
    oSheet.Cells[1, 4] = "Salary";

    oWB.SaveCopyAs(@"/Users/eaxz/Desktop/book1.xlsx");

    return "success";


}

Upvotes: 0

Views: 365

Answers (1)

Alex Yang
Alex Yang

Reputation: 339

COM Interop is not a good option for server side environment. You can try some third party component, for example, GrapeCiy Document API

Upvotes: 0

Related Questions