Getting text from an Excel file in ASP.NET Without using OleDb

Viewed 7

This question was asked for c#. I am trying to do this with ASP.NET and can't use OleDb for my application.

I'm trying to read contents from an Excel file but i can't seem to figure it out. I use IFormFile to receive a file. Then i use OpenReadStream and StreamReader to read the file. However the output of each row will give a string like this H?A49vFt??DPb~?*♥8!∟??Y4'♦?3?BV???)?]?@Q?P??<B instead of the real text from the Excel. Does someone know how i can get the text of this string? I used ASP.NET 6 Web Api. My code is:

        [HttpPost]
        public async Task<IActionResult> Post(IFormFile file)
        {
            try
            {
                using (var reader = new StreamReader(file.OpenReadStream()))
                {
                    string row;
                    while ((row = reader.ReadLine()) != null)
                    {
                        Console.WriteLine("row: " + row);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }
            return Ok();
        }
0 Answers
Related