Search This Blog

Wednesday 25 June 2014

Export to Excel change cell format to percentage format in Microsoft Dynamics AX 2012

Below is the code snippet to change the cell format to percentage format.

cells.range("D21:D30").numberFormat("0.00%");

Below is the link for more info on excel cells format.

http://www.teachexcel.com/free-excel-macros/m-72,Format-Cells-as-a-Percentage-in-Excel-Number-Formatting.html


Thanks

Muhammad Zahid.

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



SSRS Reports Range Bar Chart Show Start and End Date Only

Below are the steps to show the start and end date only in a range bar chart in SSRS reports.

First of all just select the Horizontal Axis of the chart as you can see this in the below image.










Now right click this horizontal axis and than select axis properties.

A new window will open select the first tab i.e Axis Options.As you can see this window in the below image.


As you can see in the above image in the minimum text box select the startdate and in the maximum text box select the end date and in the interval write the expression i.e

=datediff("d", Fields!PSASchedStart.Value, Fields!PSASchedEnd.Value)

after that click ok and than save the report.

After running the report you will see that you can only see the start and end dates in the range bar chart as you can see in the below image.









This was all related to the range bar chart showing the start and end date only.

Below is the link for more information

http://stackoverflow.com/questions/18004792/ssrs-chart-x-axis-show-start-and-end-date-only

If you have any issues feel free to comment on this.

Thanks

Muhammad Zahid.

SSRS Reports Hide Duplicate Records and White Spaces

Some times while working on SSRS reports there are some duplicate records issue occurs.

Like you can see this in the below image.











Now to remove the duplicate records just right click the table and than select properties and than set the hidden property to:

=Fields!ActivityNumber.Value = Previous(Fields!ActivityNumber.value)

AS you can see the property in the below image:








Now after setting this property the data on the report looks like this:








In the above image the duplicate  record is hidden but the issue is that there are some white spaces left between the rows.

Two resolve this issue we have to remove the expression from the hidden property of the table.
And than  select the row and than right click select row group and than select the group properties as you can see this in the below image














After selecting the group properties now select  select visibility and than add the expression that we have added above in the hidden property i.e

=Fields!ActivityNumber.Value = Previous(Fields!ActivityNumber.value)

As you can see this in the below image.



  



Now after doing that the duplicate records will be remove and also the white spaces between the rows will be remove as you can see that in the below image.







The above example that I have given is related to hiding row based on single column.If you want to hide the records based on two or more columns that you can use the below expression.

=(Fields!Voucher.Value = Previous(Fields!Voucher.Value)) And (Fields!AccountNum.Value = Previous(Fields!AccountNum.Value))

The above expression means that if the voucher and the AccountNum both are repeating than report short hide the row.

This was all related to the duplicate records and white spaces.
Below are some more links related to this.

http://glutenfreesql.wordpress.com/2012/10/02/ssrs-hide-duplicates/

http://stackoverflow.com/questions/11562043/trying-to-get-rid-of-white-space-in-ssrs-report

http://dba.stackexchange.com/questions/53727/how-to-hide-rows-in-ssrs-report


If you have any questions related to that than feel free to comment on this.


Thanks

Muhammad Zahid.

Tuesday 3 June 2014

Microsoft Dynamics AX 2012: Uploading data from Excel into our Dynamics Ax table.



In that post I will show you example on how can we import data from excel and than map it on your AX table.

I have created an excel sheet in which I have added some data as you can see this in the image below.

After that I will import that the data into my AX table.






The staging table that I have created on which I will map the Excel data can be seen in the below image.


















Now I will show you the process of Importing this Data into our AX tables.

I have just created a class.In the main method of that class I am calling another method and passing path of my excel sheet as a parameter.

You can see that method in the below image.






Now inside the method ImportData() there is a process written which gets the data from excel cells and than map them into the Ax tables.

You can see the code snippet for that method below.

public void ImportData
(Filename _fileName)
{
    SysExcelApplication     application;
    SysExcelWorkbooks       workbooks;
    SysExcelWorkbook        workbook;
    SysExcelWorksheets      worksheets;
    SysExcelWorksheet       worksheet;
    SysExcelCells           cells;
    COMVariantType          type;
    int                     row ;
    Zahidtable lineImport;
    str offsetType;
    str account;
    str offsetAccount;
    str transactionType;

    application = SysExcelApplication::construct();
    workbooks = application.workbooks();



    try
    {
        workbooks.open(_fileName,0,true);
    }
    catch (Exception::Error)
    {
        throw error('@SYS19358');
    }

    workbook = workbooks.item(1);
    worksheets = workbook.worksheets();
    worksheet = worksheets.itemFromNum(1);
    cells = worksheet.cells();

    row = 2;

    lineImport.clear();

    startLengthyOperation();
    do
    {
        row++;

        try
        {
            lineImport.clear();

            transactionType = this.VarientConversionMethod(cells.item(row, 8).value());

            //For acquisition
            if(transactionType == "Acquisition")
            {
                lineImport.TransType = AssetTransTypeJournal::Acquisition;
                account = this.VarientConversionMethod(cells.item(row, 1).value());
                lineImport.Account = account;
                lineImport.LedgerDimension =       AxdDimensionUtil::getMultiTypeDefaultAccountId(129,                LedgerJournalACType::FixedAssets, [account]);
                lineImport.txt = this.VarientConversionMethod(cells.item(row ,2).value());
                lineImport.AmountCurDebit = this.VarientConversionMethod(cells.item(row, 3).value());
                offsetType = this.VarientConversionMethod(cells.item(row, 4).value());
                offsetAccount = this.VarientConversionMethod(cells.item(row, 5).value());
                lineImport.BookId = this.VarientConversionMethod(cells.item(row, 6).value());
                lineImport.AssetBookId = this.VarientConversionMethod(cells.item(row, 7).value());
            }

            if (offsetType == "ledger")
            {
                lineImport.OffsetAccountType = LedgerJournalACType::Ledger;
                lineImport.OffsetLedgerDimension = AxdDimensionUtil::getLedgerAccountId( [offsetAccount, offsetAccount, 0]);
            }

            ttsBegin;
            lineImport.insert();
            ttsCommit;

        }
        catch(Exception::Error)
        {
            info("Error found while importing data from excel");
        }

        type = cells.item(row+1, 1).value().variantType();
    }
    while (type != COMVariantType::VT_EMPTY);

    endLengthyOperation();

}


In the above code. I am basically calling another method i.e VarientConversionMethod(COMVariant _varientValue).

In that method  I am going to type cast the values that I have fetched from the excel based on the fields on the table.

You can see the code of  that method below.

private anytype VarientConversionMethod(COMVariant _varientValue)
{
    str value;
    real value2;
    switch(_varientValue.variantType())
    {
        case COMVariantType::VT_BSTR:
            value = strFmt("%1", _varientValue.bStr());
            break;
        case COMVariantType::VT_DECIMAL, COMVariantType::VT_R4, COMVariantType::VT_R8:
            value = strFmt("%1",_varientValue.double());
            value2 = System.Convert::ToDecimal(value);
            value = System.Convert::ToString(value2);
            if (Global::numOfDec(_varientValue.double()) == 0)
            {
                value = strFmt("%1",real2int(_varientValue.double()));
            }
            break;
        case COMVariantType::VT_I1, COMVariantType::VT_I2, COMVariantType::VT_I4:
            value = strFmt("%1",_varientValue.int());
            break;
        case COMVariantType::VT_UI1, COMVariantType::VT_UI2, COMVariantType::VT_UI4:
            value = strFmt("%1",_varientValue.uLong());
            break;

        case COMVariantType::VT_DATE:
            value = strFmt("%1",_varientValue.date());
            break;
         case COMVariantType::VT_EMPTY:
            value = '';
            break;
        default:
            throw error(strfmt('Unhandled variant type (%1).', _varientValue.variantType()));
    }

    return value;
}

Now I have run that class and I am successfully able to the record inside my table.

You can see the record inside my staging table in the below image.





This was all related to uploading the Data from Excel into our Microsoft Dynamics AX table.

I hope you like that post.

Thanks

Muhammad Zahid.