Raysefo

Raysefo

  • 1.4k
  • 284
  • 149.8k

EF Core Export to excel

Jul 20 2022 6:03 PM

Hello there,

I am working on an ASP.NET Core Blazor Server app. I have Orders and Order Details data in SQL Server. I would like to export this master-detail relationship to excel. Any ideas on how can I manage this? (By the way, I am using Epplus for uploading excel in my application.)

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; }
         }

Here is how I populate all orders;

public async Task<IEnumerable<Order>> GetAllOrders()
 {
          return await this._db.Orders.Include("OrderDetails").ToListAsync();
 }

 


Answers (2)