Search This Blog

Wednesday 25 June 2014

Export to Excel in Microsoft Dynamics AX 2012

Below is the sample code snippet to export the data from Microsoft Dynamics AX to Excel.

Example no 1:

            SysExcelWorkbooks workbooks;
            SysExcelWorkbook workbook;
            SysExcelWorksheets worksheets;
            SysExcelWorksheet worksheet;
            SysExcelCells cells;
            SysExcelCell cell;
   
            application = SysExcelApplication::construct();
            workbooks = application.workbooks();
            workbook = workbooks.add();
            worksheets = workbook.worksheets();
            worksheet = worksheets.itemFromNum(1);
            cells = worksheet.cells();
           
            cell = cells.item(row, col);
       
            cell.value("sample Code");
           
            worksheet.columns().autoFit(); //use to autofit the columns of the excel sheet
       
            application.visible(true);

IF you want to print some Headings in Bold format and in more than one cell:

Example No 2:

             fontSizeRange   = #B+int2str(2)+","+#C+int2str(2);
             mergeCellsRange = #B+int2str(2)+":"+#C+int2str(2);
       
             this.insertValue(row,col,"Project Comparison Budget VS               Actual",true,fontSizeRange,13,true,mergeCellsRange,false,0);
           
             public void insertValue(int         _row,
                                int         _col,
                                anytype     _value,
                               boolean     _isFontBold = false,
                                str         _fontSizeRange = "",
                                int         _fontSizeWeight = 11,
                                boolean     _isMergeCell = false,
                                str         _mergeRange = "",
                                boolean     _allignment  = false,
                                int         _allignmentType = #xlLeft)
            {
       
                cell = this.parmCells().item(_row, _col);
                cell.value(_value);
       
                if (_isFontBold)
                {
                    font = cell.font();
                    font.bold(_isFontBold);
                }
                if (_fontSizeRange != "")
                {
                    worksheetCOM    = this.parmWorksheet().comObject();
                    rangeCOM        = worksheetCOM.Range(_fontSizeRange);
                    fontCOM         = rangeCOM.Font();
                    fontCOM.Size(_fontSizeWeight);
                }
                if (_isMergeCell)
                {
                    range = this.parmCells().range(_mergeRange);
                    range.comObject().MergeCells(1);
                }
                if (_allignment)
                {
                    if (_isMergeCell)
                    {
                        range.horizontalAlignment(_allignmentType);
                    }
                    else
                    {
                        cellCOM =  cell.comObject();
                        range = this.parmCells().range(cellCOM.address()+":"+cellCOM.address());
                        range.horizontalAlignment(_allignmentType);
                    }
                }
            }

This was all related to the export to excel.

If you have any query related to export to excel than feel free to comment.


Thanks

Muhammad Zahid



2 comments: