Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :

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

Vishals786 Profile Picture Vishals786


 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 : - 


        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);

        }


                

                        ****Please mention your queries if any in the comments area****





This was originally posted here.

Comments

*This post is locked for comments