Click here to Skip to main content
15,040,743 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello.
I'm trying to read an excel file in blazor. It goes well as long as I have direct path to the file "FilePath", which can be seen in the code below. But I need to drop it and open the file directly from the InputFile button so I can open any excel file. Is it possible? Can anyone help?


What I have tried:

public List<Order> ReadExcel()
        {
            List<Order> orders = new List<Order>();
            string FilePath = "C:/Users/Medarbejder 2/Desktop/Ny mappe/Benny/Posteringer2.xlsx";
            FileInfo existingFile = new FileInfo(FilePath);
            ExcelPackage.LicenseContext = LicenseContext.NonCommercial;

            using (ExcelPackage package = new ExcelPackage(existingFile))
            {
                ExcelWorksheet worksheet = package.Workbook.Worksheets.FirstOrDefault();
                int colCount = worksheet.Dimension.End.Column;
                int rowCount = worksheet.Dimension.End.Row;

                for (int row = 1; row <= rowCount; row++)
                {
                    Order order = new Order();
                    for (int col = 1; col <= colCount; col++)
                    {
                        if (col == 1) order.Kontonummer = Convert.ToInt32(worksheet.Cells[row, col].Value.ToString());
                        else if (col == 2) order.Navn = worksheet.Cells[row, col].Value.ToString();
                        else if (col == 3) order.Type = worksheet.Cells[row, col].Value.ToString();
                        else if (col == 4) order.Moms = worksheet.Cells[row, col].Value.ToString();
                    }
                    orders.Add(order);
                }
            }
            return orders;
        }


protected void ReadExcel()
    {
        orders = data.ReadExcel();
    }
Posted
Updated 1-Aug-21 7:47am
v2

1 solution

Have you tried this Microsoft tutorial (ASP.NET Core Blazor file uploads | Microsoft Docs[^])? If you still can't make it work, please let me know.

Blazor is a new Microsoft solution to replace the old Web Forms & Knockout, both officially retired. Knockout did not live long, about 2-3 years only, dying in its infancy. I don't know if Blazor will follow Knockout due to its poor & non-mainstream design. If you have a choice, making a tiny framework on top of ASP.Net is a better way to go, well, assuming you'll have to maintain the code or manage the project for the next 10 years.

Another popular way is to use Angular/AngularJS, React or Vue as the frontend & raw ASP.Net web services as its backend. Even Microsoft web service frameworks retire too fast.
   
Comments
Member 14085040 1-Aug-21 14:54pm
   
@Code Fan. Thanks for your reply. I have tried Microsoft tutorial but it does not work!
Code Fan 1-Aug-21 15:55pm
   
May you update your code?
Member 14085040 1-Aug-21 16:43pm
   
Yes I did.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900