Apr 20, 2022

Sample excel export job (X++)

 static void CustomerDetailsExport(Args _args)

{

CustTable CustTable;

SysExcelApplication application;

SysExcelWorkbooks workbooks;

SysExcelWorkbook workbook;

SysExcelWorksheets worksheets;

SysExcelWorksheet worksheet;

SysExcelCells cells;

SysExcelCell         cell;

int                          row;

DimensionAttributeValueSetStorage  dimstorage;


int         i = 0;

str         fileName    = "C:\\Users\\XXX\\Desktop\\CustDetailsWithFinDim.xlsx";

;


application = SysExcelApplication::construct();

workbooks = application.workbooks();

workbook = workbooks.add();

worksheets = workbook.worksheets();

worksheet = worksheets.itemFromNum(1);

cells = worksheet.cells();

cells.range('A:A').numberFormat('@');


cell = cells.item(1,1);

cell.value("Customer");

cell = cells.item(1,2);

cell.value("Name");

cell = cells.item(1,3);

cell.value("Warehouse");

cell = cells.item(1,4);

cell.value("CostCenter");

cell = cells.item(1,5);

cell.value("Department");

cell = cells.item(1,6);

cell.value("BusinessUnit");

cell = cells.item(1,7);

cell.value("SalesType");


row = 1;

while select CustTable

    where CustTable.AccountNum 

{

    row++;

    cell = cells.item(row, 1);

    cell.value(CustTable.AccountNum);

    cell = cells.item(row, 2);

    cell.value(CustTable.name());


    dimstorage = DimensionAttributeValueSetStorage::find(CustTable.DefaultDimension);

    for(i=1;i <= dimstorage.elements();i++)

    {

       if(DimensionAttribute::find(dimstorage.getAttributeByIndex(i)).Name == 'Warehouse')

       {

            cell = cells.item(row, 3);

            cell.value(dimstorage.getDisplayValueByIndex(i));

       }

       if(DimensionAttribute::find(dimstorage.getAttributeByIndex(i)).Name == 'CostCenter')

       {

            cell = cells.item(row, 4);

            cell.value(dimstorage.getDisplayValueByIndex(i));

       }

       if(DimensionAttribute::find(dimstorage.getAttributeByIndex(i)).Name == 'Department')

       {

            cell = cells.item(row, 5);

            cell.value(dimstorage.getDisplayValueByIndex(i));

       }

        if(DimensionAttribute::find(dimstorage.getAttributeByIndex(i)).Name == 'BusinessUnit')

       {

            cell = cells.item(row, 6);

            cell.value(dimstorage.getDisplayValueByIndex(i));

       }

        if(DimensionAttribute::find(dimstorage.getAttributeByIndex(i)).Name == 'SalesType')

       {

            cell = cells.item(row, 7);

            cell.value(dimstorage.getDisplayValueByIndex(i));

       }

    }

}

    workbook.saveAs(fileName);

    application.visible(true);

    info(strFmt('Exported %1 records',row-1));

}

No comments: