Table of Contents

Class Ecommerce.ExportExcel

Namespace
Dynamicweb.Ecommerce.Notifications
Assembly
Dynamicweb.Ecommerce.dll
Class ExportExcel. This class cannot be inherited.
public sealed class Ecommerce.ExportExcel
Inheritance
Ecommerce.ExportExcel
Inherited Members

Fields

AfterExportExcel

Occurs after export excel provider has formed excel package to be exported.
public const string AfterExportExcel = "DWN_AFTER_EXPORT_EXCEL_BOOK_IS_SAVED"

Field Value

string

Examples

using System.IO;
using System.Linq;
using Dynamicweb.Extensibility.Notifications;
using Dynamicweb.Core;
using OfficeOpenXml;
using static Dynamicweb.Ecommerce.Notifications.Ecommerce.ExportExcel;

namespace Dynamicweb.Ecommerce.Examples.Notifications
{
    [Subscribe(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();
                    }
                }
            }
        }
    }
}

Remarks

The passed NotificationArgs Is Ecommerce.ExportExcel.ExportExcelCreatedArgs
To top