ASP.NET Core Blazor Excel Import

Cenk 956 Reputation points
2022-06-20T18:44:38.38+00:00

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.

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,402 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Brando Zhang-MSFT 2,961 Reputation points Microsoft Vendor
    2022-06-21T02:36:50.153+00:00

    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

    0 comments No comments

  2. Cenk 956 Reputation points
    2022-06-21T05:30:46.44+00:00

    Hi @Brando Zhang-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. Cenk 956 Reputation points
    2022-07-14T11:20:34.69+00:00

    Hi,

    I want to insert one to many related data; orders and order details. How can I achieve this?

    Here is a sample excel
    220730-sample-excel.png

    Here are the entities:

    public class Order  
        {  
            public int Id { get; set; }  
              
            [Required]  
            public DateTime OrderDateTime { get; set; }  
            [Required]  
            [MaxLength(250)]  
            public string CustomerName { get; set; }  
            [Required]  
            [MaxLength(250)]  
            public string VendorName { get; set; }  
            public string Status { get; set; }  
            [MaxLength(50)]  
            public string DoneBy { get; set; }  
            public List<OrderDetail> OrderDetails { get; set; }  
      
      
        }  
      
    public class OrderDetail  
        {  
            public int Id { get; set; }  
              
            [Required]  
            [MaxLength(100)]  
            public string ProductCode { get; set; }  
            [Required]  
            [MaxLength(250)]  
            public string ProductName { get; set; }  
            [Required]  
            public int BuyQuantity { get; set; }  
            [Required]  
            public int SellQuantity { get; set; }  
            public double CostRatio { get; set; }  
            public double UnitCost { get; set; }  
            public double TotalBuyPrice { get; set; }  
            public double TotalSellPrice { get; set; }  
            [MaxLength(150)]  
            public string ShippingNumber { get; set; }  
            public string Status { get; set; }  
            [MaxLength(150)]  
            public string TrackingNumber { get; set; }  
            [MaxLength(400)]  
            public string Description { get; set; }  
            public int OrderId { get; set; }  
            public virtual Order Order { get; set; }  
        }  
    
    0 comments No comments