Click or drag to resize

EcommerceAfterExportExcel Field

Occurs after export excel provider has formed excel package to be exported.

Namespace:  Dynamicweb.Ecommerce.Notifications
Assembly:  Dynamicweb.Ecommerce (in Dynamicweb.Ecommerce.dll) Version: 1.10.0
Syntax
public const string AfterExportExcel = "DWN_AFTER_EXPORT_EXCEL_BOOK_IS_SAVED"

Field Value

Type: String
Remarks
Examples
C#
using System.IO;
using System.Linq;
using ecomNotifications = Dynamicweb.Ecommerce.Notifications.Ecommerce;
using Dynamicweb.Extensibility.Notifications;
using Dynamicweb.Core;
using OfficeOpenXml;
using static Dynamicweb.Ecommerce.Notifications.Ecommerce;

namespace Dynamicweb.Ecommerce.Examples.Notifications
{
    [Subscribe(ecomNotifications.AfterExportExcel)]
    public class AfterExportExcelSubscriber : NotificationSubscriber
    {
        public override void OnNotify(string notification, NotificationArgs args)
        {
            if (args == null || !(args is ExportExcelCreatedArgs))
                return;

            ExportExcelCreatedArgs afterExcelExportArgs = (ExportExcelCreatedArgs)args;
            //we have custom mappings?
            if (afterExcelExportArgs.ColumnNamesMapping != null && afterExcelExportArgs.ColumnNamesMapping.Any())
            {
                //open excel file
                using (ExcelPackage excelPackage = new ExcelPackage(new FileInfo(SystemInformation.MapPath(afterExcelExportArgs.ExcelFilePath))))
                {
                    //ensure we have some worksheets
                    if (excelPackage.Workbook.Worksheets.Count > 0)
                    {
                        //depending on what kind of export we currently at
                        if (afterExcelExportArgs.Format == ExportExcelCreatedArgs.ExportExcelFormat.UnstructuredExport)
                        {
                            int column = 1;
                            ExcelWorksheet workSheet = excelPackage.Workbook.Worksheets[1];
                            do
                            {                                
                                //getting value of 1 row and any column
                                string row = Converter.ToString(workSheet.GetValue(1, column));
                                string customHeader = string.Empty;
                                if (string.IsNullOrWhiteSpace(row))
                                {
                                    //exit if row value is empty(emd of table)
                                    column = -1;
                                }
                                else
                                {
                                    //update the cell value with custom header from import UI
                                    if (afterExcelExportArgs.ColumnNamesMapping.TryGetValue(row, out customHeader))
                                        workSheet.SetValue(1, column, customHeader);
                                    column += 1;
                                }
                            }
                            while (column != -1);
                        }
                        //save the package
                        excelPackage.Save();
                    }
                }
            }
        }
    }
}
See Also