Thursday, September 2, 2021

How to export data from table to excel through x++ in D365 FO


 Exporting data to excel might be required in most of the scenarios where Data Integration is involved.

Excel can be proved as one of the most significant applications for Data Transformation and manipulations.

Let us see how can achieve the data export using x++ coding standards :-
u



DocuFileSaveResult           saveResult;

Table1                       table1details , table1datasource , table1update;

PurchAgreementHeader         purchAgreementHeader;

TransDate                    delDate;

FormDataSource               fdsTable1det = sender.formRun().dataSource('Table1');


table1datasource = fdsTable1det.cursor();

select firstonly purchAgreementHeader where purchAgreementHeader.RecId ==                                                                                                            table1datasource.AgreementHeaderRefRecId;

while select forupdate table1update where                                                                                                                         table1update.AgreementHeaderRefRecId ==                                                                                                                table1datasource.AgreementHeaderRefRecId
{
     if(table1update)
     {
           ttsbegin;

           if(InventItemPurchSetup::findDefault(table1update.ItemId).LeadTime != 0)
           {
                delDate =  DateTimeUtil::getToday(DateTimeUtil::getUserPreferredTimeZone()) +     InventItemPurchSetup::findDefault(table1update.ItemId).LeadTime;
                
                table1update.RequestedDeliveryDate = delDate;
           }

          table1update.PurchQty = 0;

          table1update.RemarksComments = "Enter your comments here";

          table1update.update();

          ttscommit;

     }

}

saveResult = DocuFileSave::promptForSaveLocation("Table1", "xlsx", null, "Table1 Details");

if (saveResult && saveResult.parmAction() != DocuFileSaveAction::Cancel)
{
    saveResult.parmOpenParameters('web=1');

    saveResult.parmOpenInNewWindow(false);

    System.IO.Stream workbookStream = new System.IO.MemoryStream();

    System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();

    using (var package = new ExcelPackage(memoryStream))
    {
        var currentRow = 1;
        
        var worksheets = package.get_Workbook().get_Worksheets();

        var Table1Worksheet = worksheets.Add("Export");

        var cells = Table1Worksheet.get_Cells();

        OfficeOpenXml.ExcelRange cell = cells.get_Item(currentRow, 1);

        System.String value = "Item Number";

        cell.set_Value(value);

        cell = null;

        value = "Unit of Measure";

        cell = cells.get_Item(currentRow, 2);

        cell.set_Value(value);

        cell = null;

        value = "Requested Delivery Date";

        cell = cells.get_Item(currentRow, 3);

        cell.set_Value(value);

        cell = null;

        value = "Remarks and Comments";

        cell = cells.get_Item(currentRow, 4);

        cell.set_Value(value);

        cell = null;

        value = "Purchase Quantity";

        cell = cells.get_Item(currentRow, 5);

        cell.set_Value(value);

        cell = null;

        value = "Agreement Quantity";

        cell = cells.get_Item(currentRow, 6);

        cell.set_Value(value);

        cell = null;

        value = "Remaining Quantity";

        cell = cells.get_Item(currentRow, 7);

        cell.set_Value(value);

        cell = null;

        value = "Released Quantity";

        cell = cells.get_Item(currentRow, 8);

        cell.set_Value(value);

        cell = null;

        value = "Received Quantity";

        cell = cells.get_Item(currentRow, 9);

        cell.set_Value(value);

        cell = null;

        value = "Invoiced Quantity";

        cell = cells.get_Item(currentRow, 10);

        cell.set_Value(value);

        cell = null;            

        value = "Agreement Line Record Id";

        cell = cells.get_Item(currentRow, 11);

        cell.set_Value(value);

        cell = null;

        value = "Agreement Header Record Id";

        cell = cells.get_Item(currentRow, 12);

        cell.set_Value(value);



        while select table1details where table1details.AgreementHeaderRefRecId ==                                                                       purchAgreementHeader.RecId
        {

              currentRow ++;

              cell = null;

              cell = cells.get_Item(currentRow, 1);

              cell.set_Value(table1.ItemId);

              
              cell = null;

              cell = cells.get_Item(currentRow, 2);

              cell.set_Value(table1.ProductUnitOfMeasure);

              
              cell = null;
              
              cell = cells.get_Item(currentRow, 3);

              cell.set_Value(any2Str(table1.RequestedDeliveryDate));


              cell = null;

              cell = cells.get_Item(currentRow, 4);

              cell.set_Value(any2Str(table1.RemarksComments));

              cell = null;


               cell = cells.get_Item(currentRow, 5);

               cell.set_Value(any2Str(table1.PurchQty));


               cell = null;

               cell = cells.get_Item(currentRow, 6);

               cell.set_Value(any2Str(table1.AgreementQty));

               
               cell = null;

               cell = cells.get_Item(currentRow, 7);

               cell.set_Value(any2Str(table1.RemainingQty));

           
               cell = null;

               cell = cells.get_Item(currentRow, 8);

               cell.set_Value(any2Str(table1.ReleasedQty));

               
               cell = null;

               cell = cells.get_Item(currentRow, 9);

               cell.set_Value(any2Str(table1.ReceivedQty));

               
               cell = null;

               cell = cells.get_Item(currentRow, 10);

               cell.set_Value(any2Str(table1.InvoicedQty));


               cell = null;          

               cell = cells.get_Item(currentRow, 11);

               cell.set_Value(any2Str(table1.AgreementLineRefRecId));

               
               cell = null;

               cell = cells.get_Item(currentRow, 12);

               cell.set_Value(any2Str(table1.AgreementHeaderRefRecId));
               
       }
       
       package.Save();

  }
       memoryStream.Seek(0, System.IO.SeekOrigin::Begin);

       DocuFileSave::processSaveResult(memoryStream, saveResult);

}


That's all for now. Please let us know your questions or feedback in comments section !!!!

No comments:

Post a Comment

How to reverse Free Text Invoice Voucher entries without Dialog

 Hey Folks ,  This blog post is in continuation of the previous post for Reversing Free Text Invoice Voucher entries with Dialog. Only diffe...