Developer forum

Forum » Ecommerce - Standard features » Excel structure for default Export order from Customer center

Excel structure for default Export order from Customer center

Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi guys,

There is a default functionality for exporting orders from Customer center.

It points to /Admin/Public/CustomerCenter/ExcelExport.aspx.

One of our customers using this functionality has asked us to adjust the structure of the export(Adding and removing some columns).

Is this possible without completely recreating the functionality? Is there an Excel template or definition that can be adjusted?

Thank you,
Adrian


Replies

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Hi Adrian

That is not possible - but the feature is pretty simple - you can use the code and make your own custom version simple.

Below the code - it uses CustomerOrderCollection and you probably want to use the orderservice instead.

public static OutputResult RenderExcel(string shopId, string orderContextId)
{
    var response = Context.Current.Response;
    int uid = UserContext.Current.UserId;
    bool isQuote = Converter.ToString(Context.Current.Request["OrderType"]) == "quotes";
    bool isLedgerEntry = string.Equals(Context.Current.Request["OrderType"], "LedgerEntries", StringComparison.CurrentCultureIgnoreCase);
    if (uid == 0)
        return ContentOutputResult.Empty;

    var orderType = OrderType.Order;
    if (isQuote)
    {
        orderType = OrderType.Quote;
    }
    else if (isLedgerEntry)
    {
        orderType = OrderType.LedgerEntry;
    }

    var customerOrders = new CustomerOrderCollection();
    customerOrders.Load(uid, shopId, orderType, 0, false, orderContextId);

    var orders = customerOrders.OrderBy(o => o.Id);

    var sb = new System.Text.StringBuilder();

    // // Write headers
    sb.Append("Date" + @"\t");
    sb.Append("Order ID" + @"\t");
    sb.Append("Product ID" + @"\t");
    sb.Append("Product Number" + @"\t");
    sb.Append("Variant ID" + @"\t");
    sb.Append("Variant Text" + @"\t");
    sb.Append("Product Name" + @"\t");
    sb.Append("Quantity" + @"\t");
    sb.Append("Unit Price" + @"\t");
    sb.Append("Currency" + @"\t");
    sb.Append("Currency Rate" + @"\t");
    sb.Append(System.Environment.NewLine);
    foreach (Order enumOrder in orders)
    {
        foreach (OrderLine orderLine in enumOrder.OrderLines)
        {
            sb.Append(enumOrder.Date.ToShortDateString() + @"\t");
            sb.Append(enumOrder.Id + @"\t");
            sb.Append(orderLine.ProductId + @"\t");
            sb.Append(orderLine.ProductNumber + @"\t");
            sb.Append(orderLine.ProductVariantId + @"\t");
            sb.Append(orderLine.ProductVariantText + @"\t");
            sb.Append(orderLine.ProductName + @"\t");
            sb.Append(orderLine.Quantity.ToString() + @"\t");
            sb.Append(orderLine.Price.Price.ToString() + @"\t");
            sb.Append(enumOrder.CurrencyCode + @"\t");
            sb.Append(enumOrder.CurrencyRate.ToString() + @"\t");
            sb.Append(System.Environment.NewLine);
        }
    }

    var preamble = System.Text.Encoding.UTF32.GetPreamble();
    var fileBytes = System.Text.Encoding.UTF32.GetBytes(sb.ToString());
    var output = new byte[preamble.Length + fileBytes.Length];
    Buffer.BlockCopy(preamble, 0, output, 0, preamble.Length);
    Buffer.BlockCopy(fileBytes, 0, output, preamble.Length, fileBytes.Length);

    return new StreamOutputResult
    {
        ContentType = "application/vnd.ms-excel; charset=" + System.Text.Encoding.UTF32.WebName,
        Filename = "MyOrders.xls",
        ContentStream = new MemoryStream(output)
    };
}
 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

And the DW9 version:

        public static void RenderExcel(string shopId, string orderContextId)
        {
            var response = Context.Current.Response;
            int uid = (Security.UserManagement.User.GetCurrentExtranetUser()?.ID).GetValueOrDefault();
            bool isQuote = Converter.ToString(Context.Current.Request["OrderType"]) == "quotes";
            bool isLedgerEntry = string.Equals(Context.Current.Request["OrderType"], "LedgerEntries", StringComparison.CurrentCultureIgnoreCase);
            if (uid == 0)
                response.End();
            response.Clear();
            response.ContentType = "application/vnd.ms-excel; charset=" + System.Text.Encoding.UTF32.WebName;
            response.AddHeader("content-disposition", "attachment;filename=MyOrders.xls");
            var orderType = OrderType.Order;
            if (isQuote)
            {
                orderType = OrderType.Quote;
            }
            else if (isLedgerEntry)
            {
                orderType = OrderType.LedgerEntry;
            }

            var customerOrders = new CustomerOrderCollection();
            customerOrders.Load(uid, shopId, orderType, 0, false, orderContextId);
#if !DW10
            customerOrders.Sort("OrderID", SortOrderType.Asc);
            var orders = customerOrders;
#endif
#if DW10
            var orders = customerOrders.OrderBy(o => o.Id);
#endif
            var sb = new System.Text.StringBuilder();

            // // Write headers
            sb.Append("Date" + @"\t");
            sb.Append("Order ID" + @"\t");
            sb.Append("Product ID" + @"\t");
            sb.Append("Product Number" + @"\t");
            sb.Append("Variant ID" + @"\t");
            sb.Append("Variant Text" + @"\t");
            sb.Append("Product Name" + @"\t");
            sb.Append("Quantity" + @"\t");
            sb.Append("Unit Price" + @"\t");
            sb.Append("Currency" + @"\t");
            sb.Append("Currency Rate" + @"\t");
            sb.Append(System.Environment.NewLine);
            foreach (Order enumOrder in orders)
            {
                foreach (OrderLine orderLine in enumOrder.OrderLines)
                {
                    sb.Append(enumOrder.Date.ToShortDateString() + @"\t");
                    sb.Append(enumOrder.Id + @"\t");
                    sb.Append(orderLine.ProductId + @"\t");
                    sb.Append(orderLine.ProductNumber + @"\t");
                    sb.Append(orderLine.ProductVariantId + @"\t");
                    sb.Append(orderLine.ProductVariantText + @"\t");
                    sb.Append(orderLine.ProductName + @"\t");
                    sb.Append(orderLine.Quantity.ToString() + @"\t");
                    sb.Append(orderLine.Price.Price.ToString() + @"\t");
                    sb.Append(enumOrder.CurrencyCode + @"\t");
                    sb.Append(enumOrder.CurrencyRate.ToString() + @"\t");
                    sb.Append(System.Environment.NewLine);
                }
            }

            response.BinaryWrite(System.Text.Encoding.UTF32.GetPreamble());
            response.BinaryWrite(System.Text.Encoding.UTF32.GetBytes(sb.ToString()));
            response.Flush();
            response.End();
        }
 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Thank you,

I just wanted to be sure I won't recreate an existing functionality.

Thank you,
Adrian

 

You must be logged in to post in the forum