question

Cenk-1028 avatar image
0 Votes"
Cenk-1028 asked AgaveJoe commented

ASP.NET Core Blazor Excel Import

Hello,

I am new to ASP.NET core blazor. I would like to know if there are some tutorials about importing excel data into SQL database. The user selects the excel file from file explorer then uploads it and saved data into the database is displayed on the view.

Thanks in advance.

dotnet-aspnet-core-blazor
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

BrandoZhang-MSFT avatar image
0 Votes"
BrandoZhang-MSFT answered BrandoZhang-MSFT edited

According to your description, I suggest you could use System.Data.OleDb and System.Data.SqlClient package to achieve your requirement inside the blazor server app.

You could firstly save the uploaded excel inside the server and then use olddb to read it and insert it into the sql database.

More details, you could refer to below codes:

Connection string:

   "ConnectionStrings": {
     "constr": "Server=(localdb)\\mssqllocaldb;Database=xxxxxxxxxx;Trusted_Connection=True;MultipleActiveResultSets=true",
     "ExcelConString": "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR=YES'"
   }

Razor codes(Please refer to below extension):

213069-pagecodes.txt

Result:
9qc5d.png



pagecodes.txt (4.2 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Cenk-1028 avatar image
0 Votes"
Cenk-1028 answered AgaveJoe commented

Hi @BrandoZhang-MSFT ,

Thanks for your reply. I wonder after saving to the DB can I navigate to the list page?

Something like this;

 private async Task OnValidSubmit()
     {
         if (this.inventory != null)
         {
             NavigationManager.NavigateTo("/inventories");
         }
                
     }
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Sure you could redirect to it.

0 Votes 0 ·
Cenk-1028 avatar image Cenk-1028 BrandoZhang-MSFT ·

One last thing, can I use EPPlus non-commercial version? Do you know about this?

0 Votes 0 ·

There is nothing stopping you from using EPPlus non-commercial version. I recommend reading the EPPlus documentation and writing sample code to figure out if EPPlus fits your expectations.

0 Votes 0 ·