public async Task <IActionResult> Export() { var taxRates = await _taxRateRepository .Query() .Select(x => new TaxRateImport { TaxClassId = x.TaxClassId, CountryId = x.CountryId, StateOrProvinceName = x.StateOrProvince.Name, ZipCode = x.ZipCode, Rate = x.Rate }) .ToListAsync(); var csvString = CsvConverter.ExportCsv(taxRates); var csvBytes = Encoding.UTF8.GetBytes(csvString); // MS Excel need the BOM to display UTF8 Correctly var csvBytesWithUTF8BOM = Encoding.UTF8.GetPreamble().Concat(csvBytes).ToArray(); return(File(csvBytesWithUTF8BOM, "text/csv", "tax-rates.csv")); }
public async Task <IActionResult> OrderLinesExport([FromBody] SmartTableParam param, [FromServices] IRepository <OrderItem> orderItemRepository) { var query = orderItemRepository.Query(); var currentUser = await _workContext.GetCurrentUser(); if (!User.IsInRole("admin")) { query = query.Where(x => x.Order.VendorId == currentUser.VendorId); } if (param.Search.PredicateObject != null) { dynamic search = param.Search.PredicateObject; if (search.Id != null) { long id = search.Id; query = query.Where(x => x.Id == id); } if (search.Status != null) { var status = (OrderStatus)search.Status; query = query.Where(x => x.Order.OrderStatus == status); } if (search.CustomerName != null) { string customerName = search.CustomerName; query = query.Where(x => x.Order.Customer.FullName.Contains(customerName)); } if (search.CreatedOn != null) { if (search.CreatedOn.before != null) { DateTimeOffset before = search.CreatedOn.before; query = query.Where(x => x.Order.CreatedOn <= before); } if (search.CreatedOn.after != null) { DateTimeOffset after = search.CreatedOn.after; query = query.Where(x => x.Order.CreatedOn >= after); } } } var orderItems = await query .Select(x => new OrderLineExportVm { Id = x.Id, OrderStatus = (int)x.Order.OrderStatus, IsMasterOrder = x.Order.IsMasterOrder, DiscountAmount = x.Order.DiscountAmount, CreatedOn = x.Order.CreatedOn, OrderStatusString = x.Order.OrderStatus.ToString(), PaymentFeeAmount = x.Order.PaymentFeeAmount, OrderTotal = x.Order.OrderTotal, Subtotal = x.Order.SubTotal, SubTotalWithDiscount = x.Order.SubTotalWithDiscount, PaymentMethod = x.Order.PaymentMethod, ShippingAmount = x.Order.ShippingFeeAmount, ShippingMethod = x.Order.ShippingMethod, TaxAmount = x.Order.TaxAmount, CustomerId = x.Order.CustomerId, CustomerName = x.Order.Customer.FullName, CustomerEmail = x.Order.Customer.Email, LatestUpdatedOn = x.Order.LatestUpdatedOn, Coupon = x.Order.CouponCode, Items = x.Order.OrderItems.Count(), BillingAddressId = x.Order.BillingAddressId, BillingAddressAddressLine1 = x.Order.BillingAddress.AddressLine1, BillingAddressAddressLine2 = x.Order.BillingAddress.AddressLine2, BillingAddressContactName = x.Order.BillingAddress.ContactName, BillingAddressCountryName = x.Order.BillingAddress.Country.Name, BillingAddressDistrictName = x.Order.BillingAddress.District.Name, BillingAddressZipCode = x.Order.BillingAddress.ZipCode, BillingAddressPhone = x.Order.BillingAddress.Phone, BillingAddressStateOrProvinceName = x.Order.BillingAddress.StateOrProvince.Name, ShippingAddressAddressLine1 = x.Order.ShippingAddress.AddressLine1, ShippingAddressAddressLine2 = x.Order.ShippingAddress.AddressLine2, ShippingAddressId = x.Order.ShippingAddressId, ShippingAddressContactName = x.Order.ShippingAddress.ContactName, ShippingAddressCountryName = x.Order.ShippingAddress.Country.Name, ShippingAddressDistrictName = x.Order.ShippingAddress.District.Name, ShippingAddressPhone = x.Order.ShippingAddress.Phone, ShippingAddressStateOrProvinceName = x.Order.ShippingAddress.StateOrProvince.Name, ShippingAddressZipCode = x.Order.ShippingAddress.ZipCode, OrderLineDiscountAmount = x.DiscountAmount, OrderLineQuantity = x.Quantity, OrderLineTaxAmount = x.TaxAmount, OrderLineTaxPercent = x.TaxPercent, OrderLineId = x.Id, ProductId = x.ProductId, ProductName = x.Product.Name, ProductPrice = x.ProductPrice }) .ToListAsync(); var csvString = CsvConverter.ExportCsv(orderItems); var csvBytes = Encoding.UTF8.GetBytes(csvString); // MS Excel need the BOM to display UTF8 Correctly var csvBytesWithUTF8BOM = Encoding.UTF8.GetPreamble().Concat(csvBytes).ToArray(); return(File(csvBytesWithUTF8BOM, "text/csv", "order-lines-export.csv")); }