skip to Main Content

I have an endpoint that should return a value from an excel file.

On localhost this works by enabling UseStaticFiles middleware using this code, and adding excel file into the Resources folder:

app.UseStaticFiles(new StaticFileOptions
{
   FileProvider = new PhysicalFileProvider(
       Path.Combine(env.ContentRootPath, "Resources")),
   RequestPath = "/Resources"
});

However when I publish it in the Azure hosting it shows this error and I cannot acces my application nor this excel file.
enter image description here

After I remove the UseStaticFiles middleware, application works well except for the missing excel file.

So how should I publish and access this file in the Azure environment?

2

Answers


  1. Chosen as BEST ANSWER

    I combined Clean Windows Azure Website and Accessing wwwroot folder to come up with the solution.

    No middleware is required, just by using IWebHostEnvironment service I could access needed excel file, which is now based in the wwwroot/Resources/ directory within my project.

    private readonly IWebHostEnvironment _env;
    
    public ProjectService(IWebHostEnvironment env)
    {
       _env = env;
    }
    
    public double GetExcelValue() 
    {
       string excelFilePath = Path.Combine(_env.WebRootPath, "Resources/file.xlsx");
    
       (...)
    }
    

  2. The UseStaticFiles method only, and I quote, "Enables static file serving". This does not enable you to return a value from a specific cell in an Excel file.

    If you’re looking for serving static files you could for instance have a look at putting the file in Azure Storage and using the desired public access level.

    If you’re looking to read from an Excel file, you might want to have a look at a way to do so that does not require Interop since you will not have Excel available on an App Service.

    On option would be to use the OpenXML XML SDK.

    The SDK is built on the System.IO.Packaging API and provides strongly-typed classes to manipulate documents that adhere to the Office Open XML File Formats specification.

    […]

    The Open XML file formats are useful for developers because they are an open standard and are based on well-known technologies: ZIP and XML.

    Another would be an open source solution like ClosedXML which is a wrapper around OpenXML making working with them easier.

    ClosedXML is a .NET library for reading, manipulating and writing Excel 2007+ (.xlsx, .xlsm) files. It aims to provide an intuitive and user-friendly interface to dealing with the underlying OpenXML API.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search