Skip to main content

Create Execl Report using dialog in AX2009

First create the Excel template with all the fields one by one and place it into Specific location .//

In Ax 2009, We have the option to create EXCEL Report. Using class we have to create the excel report.  creating excel report is very simple. 

Using dialog we get the input and based on the input we have to generate the Report. 
Requirement is based on the customer Po and Item Id, I want to generate the Report.


I am getting input form the dialog.



//Declare all the variable in the class declaration.
class InventoryStockSheet
{
    //Define the column
    #Define.A(1)
    #Define.B(2)
    #Define.C(3)
    #Define.D(4)
    #Define.E(5)
    #Define.F(6)
    #Define.G(7)
    #Define.H(8)
    #Define.I(9)
    #Define.J(10)
    #Define.K(11)
    #Define.L(12)
    #Define.M(13)
    #Define.N(14)
    #Define.O(15)
    #Define.P(16)
    #Define.Q(17)
    #Define.R(18)
    #Define.S(19)
    #Define.T(20)
    #Define.U(21)

//Excel Application initialization varibles
//Start
    SysExcelApplication         excelApplication;
    SysExcelWorksheet           excelWorksheet;
    SysExcelWorksheets          excelWorksheets;
    SysExcelWorkbooks           excelWorkbooks;
    SysExcelWorkbook            excelWorkbook;
    SysExcelRange               excelRange;
    SysExcelCell                excelCell;
    SysExcelCells               excelCells;
    COMVariant                  COMVariant1;
    COM                         Borders;
    COM                         Border;
    SysExcelStyles              styles;
    SysExcelStyle               style;
//End
// Variable Declaration Start
    Counter                     row,slNo;

    DialogField                 dialogCustPO;
    DialogField                 dialogItemId;

    ItemId                      itemId;
    SalesIdBase                 salesId;
    SalesLine                   salesLine;
    BOM                         bom;
    SalesTable                  salesTable;
    InventTable                 inventTable;
    BOMVersion                  bomVersion;

    ItemControl                 itemControl;
    CustPurchaseOrder           custPo;
    ObjectRun                   objRun;
//End
}

//This method is initialize the Excel Sheet and find the Path.
void initilizeExcel()
{

    excelApplication    = SysExcelApplication::construct();
    excelWorkbooks      = excelApplication.workbooks();
    cOMVariant1         = new COMVariant();
    cOMVariant1.bStr('C:\\Users\\ADeveloper2\\Desktop\\StockRep1');
    excelWorkbook       = excelWorkbooks.add(COMVariant1);
    excelWorksheets     = excelWorkbook.worksheets();
    excelWorksheet      = excelWorksheets.itemFromNum(1);
    excelWorkSheet.name("@SYS16399");

}

//This method is for Dialog BoX
boolean  promptPeriod(Caption _caption)
{

    Dialog          dialog  = new Dialog(_caption);
    DialogGroup     mainGroup1;
    str             tmp;            
    ;
    tmp = objRun.args().parm();

    mainGroup1   = dialog.addGroup("Stock Sheet");
    mainGroup1.frameType(FormFrameType::Edged3D);
    mainGroup1.widthMode(1);

   // dialogCustPO    =   dialog.addFieldValue(TypeId(CustPurchaseOrder),custPo,"Cust PO");
    dialogCustPo    = dialog.addField(str2int(tmp));
    dialogItemId    =   dialog.addFieldValue(TypeId(ItemId),itemId,"Item Id");
    if (! dialog.run())
        return false;

    return true;
}

//This method is getting value form dialog Box.
void getFromDialog()
{
    ;

    custPo  =   dialogCustPO.value();
    itemId  =   dialogItemId.value();

}

//This method is printing the value in Excel Sheet.
//All the logic is written in this method. In normal report we have to write the code in fetch method. Similarly we have to write the logic here.
void getdata()
{
     int         Val;
    boolean  check,increment;
    Qty      totQty;
    int      zeroVal;
    InventBatchId   batch;
    ;
    zeroVal = 0;
 // in dialog we pass the customer PO and Item It means. This if loop will be executed.
    if(custPo && itemId)
    {
       Val =   5;
        check = true;
// second row and B cell display the customer Po
        row = 2;
        excelCell = excelWorksheet.cells().item(row,#b);
        excelCell.value(custPo);

        while select salesTable
            where salesTable.PurchOrderFormNum  == custPo
        {
            while select salesLine
                index hint SalesLineIdx
                where salesLine.SalesId == salesTable.SalesId
                && salesLine.ItemId == itemId
                && salesLine.ItemId != "100.000-RM"
                && salesLine.ItemId != "100.000"
            {
                row = Val;
                if(check == true)
                {
                    excelCell = excelWorksheet.cells().item(row,#a);
                    excelCell.value(salesTable.PurchorderFormNum);
                    excelApplication.visible(true);

                    excelCell = excelWorksheet.cells().item(row,#b);
                    excelCell.value(salesTable.SearchName);
                    excelApplication.visible(true);
                    check = false;
                }
                excelCell = excelWorksheet.cells().item(row,#c);
                excelCell.value(salesLine.ItemId);
                excelApplication.visible(true);

                excelCell = excelWorksheet.cells().item(row,#d);
                excelCell.value(salesLine.SalesQty);
                excelApplication.visible(true);

               

                Val++;
            }
        }
    }

    else if(custPO)
    {
        Val =   5;
        check = true;
        row = 2;
        excelCell = excelWorksheet.cells().item(row,#b);
        excelCell.value(custPo);

        while select salesTable
            where salesTable.PurchOrderFormNum  == custPo
        {
            while select salesLine
                index hint SalesLineIdx
                where salesLine.SalesId == salesTable.SalesId
                && salesLine.ItemId != "100.000-RM"
                && salesLine.ItemId != "100.000"
            {
                row = Val;
                if(check == true)
                {
                    excelCell = excelWorksheet.cells().item(row,#a);
                    excelCell.value(salesTable.PurchorderFormNum);
                    excelApplication.visible(true);

                    excelCell = excelWorksheet.cells().item(row,#b);
                    excelCell.value(salesTable.SearchName);
                    excelApplication.visible(true);
                    check = false;
                }
                excelCell = excelWorksheet.cells().item(row,#c);
                excelCell.value(salesLine.ItemId);
                excelApplication.visible(true);

                excelCell = excelWorksheet.cells().item(row,#d);
                excelCell.value(salesLine.SalesQty);
                excelApplication.visible(true);

               
                Val++;
            }
        }

    }

}

//we call the dialog, Initialize excel and getdata method here. And we will give the run method into the main method.
void run()
{
    ;
//    this.getFromDialog();
    this.initilizeExcel();
    this.getdata();

}

//In this method we fix the font size and color.
void setFontHeader(SysExcelCell _sysExelCell)
{
    COM comg,comfont;
    ;
    comg    = _sysExelCell.comObject();
    comfont = comg.font();
    comfont.bold(3);
    comfont.size(16);
    comfont.color(WINAPI::RGB2int(0,0,255));

}
//Create main method and create instance here.
public static void main(Args args)
{
    InventoryStockSheet  stockSheet;
    ;
    stockSheet = new InventoryStockSheet();
    if(stockSheet.promptPeriod(""))
    {
           stockSheet.run();
    }


}

Thanks
DAX

Comments

Popular posts from this blog

Using File path on a form getting Error in Axapta

Hi Folks, After a long time am coming  back to posting few new things here for AX Developer... When we want to select the file path in form level. Normally what we will do create one EDT and extends with filepath (EDT), but that time when you are trying to select the path we will get error like stack-trace/Error message .Don’t worry there is no problem with your ax application. Simple we need to provide the method to the form like filepathLookUpTitle (). Below method we need to add it into form level methods, i.e. str filePathLookupTitle() {     return "Select document folder"; } Thanks Happy Daxing....

How to add Filter functionality to Display method in dynamics AXAPTA

Hi Friends, Normally filters will work only in table fields but we can't do filters to display method. This below code will work for filters to display method also. Step 1: Go to the form design right click on particular control properties Auto Declaration No to Yes. Step 2: Override the context() method on the display method  . public void context() {     int             selectedMenu;     formrun         fr;     Args            ag;     Name            strtext;     querybuilddataSource qb1;     queryrun    qr;     query       q;     PopupMenu menu = new PopupMenu(element.hWnd());     int a = menu.insertItem('Filter By Field');     int b = menu.insertItem('Filter By Selection');     i...

Split string useing List in AX2009

static void Job277(Args _args) {     List _list = new List(Types::String);     Container packedList;     ListIterator iterator;     str cade = "Jhon*,smt*,and*,caro*";     ;     _list = Global::strSplit(cade,",");     iterator = new ListIterator(_list);     while(iterator.more())     {         packedList += iterator.value();         iterator.next();     }     info(conpeek(packedList,2));     info(strfmt("%1",conlen(packedList)));   }