Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to read xls file in c#?

Net core API. I am trying to read xls file using ExcelDataReader. In ExcelDataReader I do not want to pass file path but I want to pass stream. I tried below

 [HttpPost("import")]
 public async Task<IActionResult> Import(IFormFile formFile)
 {
   excelReader.IExcelDataReader reader;

            if (formFile == null || formFile.Length <= 0)
            {
                return  Ok("formfile is empty");
            }
            using (var stream = new MemoryStream())
            {
                await formFile.CopyToAsync(stream);
                reader = ExcelReaderFactory.CreateReader(stream);
            }
 }

When I execute I get exception

No data is available for encoding 1252. For information on defining a
custom encoding, see the documentation for the
Encoding.RegisterProvider method.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I am not sure what would be the root cause of the issue? Can someone help me here to fix the issue? Any help would be appreciated. Thank you

>Solution :

You need to add a dependency to the package System.Text.Encoding.CodePages and register the encoding by adding this line to your code:

System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);

See this note on github

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading