Tuesday, February 23, 2010

Create formated Excel reports from Ax 2009

Extracting Excel reports from Dynamics AX 2009 is easier task but client ask for formatted reports developers face a problem in generating a report directly in excel as per client requirement as:
1. Borders
2. interiors
3. Bold/Italicheader
4. image in excel
5. Fonts
6. Underline










Below class can be used to generate the reports copy the code in text file and save in xpo import the file in Dynamcis AX 2009

Exportfile for AOT version 1.0 or later
Formatversion: 1

***Element: CLS

; Microsoft Dynamics AX Class: ExcelReporter unloaded
; --------------------------------------------------------------------------------
CLSVERSION 1

CLASS #ExcelReporter
Id 30095
PROPERTIES
Name #ExcelReporter
Extends #
RunOn #Called from
ENDPROPERTIES

METHODS
Version: 3
SOURCE #classDeclaration
#class ExcelReporter
#{
# COM excelApplication;
# COM excelWorkBooks;
# COM excelWorkBook;
# COM excelWorkSheets;
# COM excelWorkSheet;
# COM excelCell;
# COM Module;
# COM range;
# COM Borders;
# COM Border;
# COM font;
# COM styles;
# COM style;
# COM interior;
# COM entireColumn;
# COM formula;
#
# COM AutoFilter;
# COM EnableAutoFilter ;
#
# COM excelCharts;
# COM excelChart;
#
# COM ActiveChart;
# COM ChartObjects;
# COM WrapText;
# COM ColoumnWidth;
#
#
# int lineNum;
#
# Array arr;
#
# str sCode;
# int ColNameLen;
#
#
#}
ENDSOURCE
SOURCE #ColoumnWidth
#void ColoumnWidth(COM _range,int Value)
#{
# ColoumnWidth = _range.ColoumnWidth(value);
#}
ENDSOURCE
SOURCE #companyLogo
#display Bitmap companyLogo()
#{
# CompanyInfo companyInfo = CompanyInfo::find();
# ;
#
# return CompanyImage::find(companyInfo.DataAreaId, companyInfo.TableId, companyInfo.RecId).Image;
#}
ENDSOURCE
SOURCE #getColChar
#str getColChar(int _num)
#{
# int basePosition = 0;
# int Counts;
# str char;
# int expo;
# boolean flagfound;
#
#
# if(_num > 16384)
# throw error('Not Supported by the system');
#
# ColNameLen = 1;
# while(_num > power(26,ColNameLen))
# {
# ColNameLen = ColNameLen + 1;
# }
#
#
# while(!flagfound)
# {
# switch(_num)
# {
# case 1+basePosition :
# char = 'A';
# break;
#
# case 2+basePosition :
# char = 'B';
# break;
#
# case 3+basePosition :
# char = 'C';
# break;
#
# case 4+basePosition :
# char = 'D';
# break;
#
# case 5+basePosition :
# char = 'E';
# break;
#
# case 6+basePosition :
# char = 'F';
# break;
#
# case 7+basePosition :
# char = 'G';
# break;
#
# case 8+basePosition :
# char = 'H';
# break;
#
# case 9+basePosition :
# char = 'I';
# break;
#
# case 10+basePosition :
# char = 'J';
# break;
#
# case 11+basePosition :
# char = 'K';
# break;
#
# case 12+basePosition :
# char = 'L';
# break;
#
# case 13+basePosition :
# char = 'M';
# break;
#
# case 14+basePosition :
# char = 'N';
# break;
#
# case 15+basePosition :
# char = 'O';
# break;
#
# case 16+basePosition :
# char = 'P';
# break;
#
# case 17+basePosition :
# char = 'Q';
# break;
#
# case 18+basePosition :
# char = 'R';
# break;
#
# case 19+basePosition :
# char = 'S';
# break;
#
# case 20+basePosition :
# char = 'T';
# break;
#
# case 21+basePosition :
# char = 'U';
# break;
#
# case 22+basePosition :
# char = 'V';
# break;
#
# case 23+basePosition :
# char = 'W';
# break;
#
# case 24+basePosition :
# char = 'X';
# break;
#
# case 25+basePosition :
# char = 'Y';
# break;
#
# case 26+basePosition :
# char = 'Z';
# break;
# }
# if(char)
# flagfound = true;
#
# if(basePosition)
# counts = counts + 1;
#
# basePosition = basePosition + 26;
# }
#
#
# if(strlen(char) < ColNameLen)
# char = this.getColChar(counts) + char;
#
# return char;
#
#}
#
#
#
#
#
#
#
#
#
#
#/* switch(_num)
# {
# case 1 :
# return 'A';
# break;
#
# case 2 :
# return 'B';
# break;
#
# case 3 :
# return 'C';
# break;
#
# case 4 :
# return 'D';
# break;
#
# case 5 :
# return 'E';
# break;
#
# case 6 :
# return 'F';
# break;
#
# case 7 :
# return 'G';
# break;
#
# case 8 :
# return 'H';
# break;
#
# case 9 :
# return 'I';
# break;
#
# case 10 :
# return 'J';
# break;
#
# case 11 :
# return 'K';
# break;
#
# case 12 :
# return 'L';
# break;
#
# case 13 :
# return 'M';
# break;
#
# case 14 :
# return 'N';
# break;
#
# case 15 :
# return 'O';
# break;
#
# case 16 :
# return 'P';
# break;
#
# case 17 :
# return 'Q';
# break;
#
# case 18 :
# return 'R';
# break;
#
# case 19 :
# return 'S';
# break;
#
# case 20 :
# return 'T';
# break;
#
# case 21 :
# return 'U';
# break;
#
# case 22 :
# return 'V';
# break;
#
# case 23 :
# return 'W';
# break;
#
# case 24 :
# return 'X';
# break;
#
# case 25 :
# return 'Y';
# break;
#
# case 26 :
# return 'Z';
# break;
#
#
#
#
#
# }
#
#}
#*/
#
ENDSOURCE
SOURCE #insertHeader
#void insertHeader(Types _types,
# int _idx,
# str _xlRowCol,
# str _value)
#{
# arr = new Array(_types);
# arr.value(_idx,_value);
# range = excelWorkSheet.Range(_xlRowCol);
# range.value2(COMVariant::createFromArray(arr));
#}
#
#
#
#
ENDSOURCE
SOURCE #insertImage
#void insertImage(int _idx,
# str _xlRowCol,
# Bitmap _value)
#{
# ;
# range = excelWorkSheet.Range(_xlRowCol);
# range.value(_value);
#}
#
#
#
#
ENDSOURCE
SOURCE #new
#void new()
#{
# ;
# excelApplication = new COM("excel.application");
# excelWorkBooks = excelApplication.workBooks();
# excelWorkBook = excelWorkBooks.add();
# excelWorkSheets = excelWorkBook.worksheets();
# excelWorkSheet = excelWorkSheets.add();
#
#}
ENDSOURCE
SOURCE #variant2COM
#void variant2COM(COM _COM, COMVariant _variant)
#
# {
#
# _COM.attach(_variant.iDispatch());
#
# }
#
ENDSOURCE
SOURCE #xlAllBorder
#void xlAllBorder(COM _range,int _weight)
#{
# Borders = _range.Borders();
# Border = Borders.Item(2);
# Border.lineStyle(7);
# Border.Weight(_weight);
#
# Borders = null;
# border = null;
#
# Borders = _range.Borders();
# Border = Borders.Item(1);
# Border.lineStyle(7);
# Border.Weight(_weight);
#
# Borders = null;
# border = null;
#
# Borders = _range.Borders();
# Border = Borders.Item(8);
# Border.lineStyle(7);
# Border.Weight(_weight);
#
# Borders = null;
# border = null;
#
# Borders = _range.Borders();
# Border = Borders.Item(9);
# Border.lineStyle(7);
# Border.Weight(_weight);
#
#}
ENDSOURCE
SOURCE #xlAutoFilter
#void xlAutoFilter(str _xlRowCol,
# boolean _Filter)
#
#
#
#{
# ;
# range = excelWorkSheet.Range(_xlRowCol);
#
#}
ENDSOURCE
SOURCE #xlAutoFit
#void xlAutoFit(COM _range)
#{
# entireColumn = _range.entireColumn();
# entirecolumn.autofit();
#}
ENDSOURCE
SOURCE #xlBotBorder
#void xlBotBorder(COM _range)
#{
# Borders = _range.Borders();
# Border = Borders.Item(9);
# Border.lineStyle(7);
# Border.Weight(3);
#}
ENDSOURCE
SOURCE #xlBoxBorder
#void xlBoxBorder(str _row,str _coll,int _weight)
#{
# int rowNum = any2int(strdel(_row,1,1));
# int collNum = any2int(strdel(_coll,1,1));
# str row = strdel(_row,2,strlen(_row));
# str coll = strdel(_coll,2,strlen(_coll));
#
# Com _range;
# // top
# _range = excelWorkSheet.Range(strfmt("%1%2",row,rownum),strfmt("%1%2",coll,rownum));
#
# Borders = _range.Borders();
# Border = Borders.Item(8);
# Border.lineStyle(7);
# Border.Weight(_weight);
#
# Borders = null;
# border = null;
# _range = null;
#
# // left
# _range = excelWorkSheet.Range(strfmt("%1%2",row,rownum),strfmt("%1%2",row,collnum));
#
# Borders = _range.Borders();
# Border = Borders.Item(1);
# Border.lineStyle(7);
# Border.Weight(_weight);
#
# Borders = null;
# border = null;
# _range = null;
#
# // right
# _range = excelWorkSheet.Range(strfmt("%1%2",num2char(char2num(coll,1)+1),rownum),strfmt("%1%2",num2char(char2num(coll,1)+1),collnum));
#
# Borders = _range.Borders();
# Border = Borders.Item(1);
# Border.lineStyle(7);
# Border.Weight(_weight);
#
# Borders = null;
# border = null;
# _range = null;
#
# // bottom
# _range = excelWorkSheet.Range(strfmt("%1%2",row,collnum+1),strfmt("%1%2",coll,collnum+1));
#
# Borders = _range.Borders();
# Border = Borders.Item(8);
# Border.lineStyle(7);
# Border.Weight(_weight);
#
# Borders = null;
# border = null;
# _range = null;
#}
#
ENDSOURCE
SOURCE #xlChart
#void xlChart(str _xlRowCol,
# str _Text)
#
#
#{
#
#}
ENDSOURCE
SOURCE #xlClose
#void xlClose()
#{
# excelApplication.quit();
#}
ENDSOURCE
SOURCE #xlCreateHeader
#Array xlCreateHeader(Array _arr,
# int _idx,
# str _value)
#{
# _arr.value(_idx,_value);
# return _arr;
#}
#
#
#
#
ENDSOURCE
SOURCE #xlFormatCell
#void xlFormatCell(str _xlRowCol,
# int _fontSize,
# boolean _bold,
# boolean _italic,
# boolean _strikethrough,
# int _underline,
# boolean _Subscript,
# boolean _Superscript,
# str _fontname)
#
#
#
#
#{
# ;
# range = excelWorkSheet.Range(_xlRowCol);
# font = range.Font();
# font.Size(_fontSize);
# font.bold(_bold);
# font.italic(_italic);
# font.underline(_underline);
# font.strikethrough(_strikethrough);
# font.Subscript(_Subscript);
# font.Superscript(_Superscript);
# font.name(_fontname);
#
#}
#
ENDSOURCE
SOURCE #xlFormula
#void xlFormula(str _xlRowCol)
#{
# ;
# range = excelWorkSheet.Range(_xlRowCol);
#
# formula = range.formula();
#}
#
ENDSOURCE
SOURCE #xlFormulan
#COM xlFormulan()
#{
# ;
# formula = range.formula();
# return formula;
#}
#
ENDSOURCE
SOURCE #xlGridBorder
#void xlGridBorder(COM _range,int _weight)
#{
# Borders = _range.Borders();
# Border = Borders.Item(2);
# Border.lineStyle(7);
# Border.Weight(_weight);
#
# Borders = null;
# border = null;
#
# Borders = _range.Borders();
# Border = Borders.Item(1);
# Border.lineStyle(7);
# Border.Weight(_weight);
#
# Borders = null;
# border = null;
#
# Borders = _range.Borders();
# Border = Borders.Item(8);
# Border.lineStyle(7);
# Border.Weight(_weight);
#
# Borders = null;
# border = null;
#
# Borders = _range.Borders();
# Border = Borders.Item(9);
# Border.lineStyle(7);
# Border.Weight(_weight);
#
# Borders = null;
# border = null;
#
# Borders = _range.Borders();
# Border = Borders.Item(3);
# Border.lineStyle(7);
# Border.Weight(_weight);
#
#}
ENDSOURCE
SOURCE #xlHorizontaltBorder
#void xlHorizontaltBorder(str _row,str _coll,int _weight)
#{
# int rowNum = any2int(strdel(_row,1,1));
# int collNum = any2int(strdel(_coll,1,1));
# str row = strdel(_row,2,strlen(_row));
# str coll = strdel(_coll,2,strlen(_coll));
#
# Com _range;
# // top
# _range = excelWorkSheet.Range(strfmt("%1%2",row,rownum),strfmt("%1%2",coll,rownum));
#
# Borders = _range.Borders();
# Border = Borders.Item(8);
# Border.lineStyle(7);
# Border.Weight(_weight);
#
# Borders = null;
# border = null;
# _range = null;
#
# // left
# _range = excelWorkSheet.Range(strfmt("%1%2",row,rownum),strfmt("%1%2",row,collnum));
#
# Borders = _range.Borders();
# Border = Borders.Item(1);
# Border.lineStyle(7);
# Border.Weight(_weight);
#
# Borders = null;
# border = null;
# _range = null;
#
# // right
# _range = excelWorkSheet.Range(strfmt("%1%2",num2char(char2num(coll,1)+1),rownum),strfmt("%1%2",num2char(char2num(coll,1)+1),collnum));
#
# Borders = _range.Borders();
# Border = Borders.Item(1);
# Border.lineStyle(7);
# Border.Weight(_weight);
#
# Borders = null;
# border = null;
# _range = null;
#
# // bottom
# _range = excelWorkSheet.Range(strfmt("%1%2",row,collnum+1),strfmt("%1%2",coll,collnum+1));
#
# Borders = _range.Borders();
# Border = Borders.Item(8);
# Border.lineStyle(7);
# Border.Weight(_weight);
#
# Borders = null;
# border = null;
# _range = null;
#
# _range = excelWorkSheet.Range(strfmt("%1%2",row,rownum),strfmt("%1%2",coll,collnum));
# Borders = _range.Borders();
# Border = Borders.Item(8);
# Border.lineStyle(7);
# Border.Weight(_weight);
#
# Borders = null;
# border = null;
#
# _range = excelWorkSheet.Range(strfmt("%1%2",row,rownum),strfmt("%1%2",coll,collnum));
# Borders = _range.Borders();
# Border = Borders.Item(9);
# Border.lineStyle(7);
# Border.Weight(_weight);
#
# Borders = null;
# border = null;
#
# _range = excelWorkSheet.Range(strfmt("%1%2",row,rownum),strfmt("%1%2",coll,collnum));
# Borders = _range.Borders();
# Border = Borders.Item(3);
# Border.lineStyle(7);
# Border.Weight(_weight);
#
#}
ENDSOURCE
SOURCE #xlInsert
#void xlInsert(Types _types,
# int _row,
# int _col,
# str _value)
#{
# excelcell = excelworksheet.cells();
# this.variant2COM(excelcell, excelcell.item(_row,_col));
# excelcell.value2(_value);
#}
#
#
#
#
ENDSOURCE
SOURCE #xlInsertArr
#void xlInsertArr(Array _arr,
# str _xlRowCol)
#{
# ;
# range = excelWorkSheet.Range(_xlRowCol);
# range.value2(COMVariant::createFromArray(_arr));
#}
ENDSOURCE
SOURCE #xlInsertss
#void xlInsertss(Types _types,
# int _row,
# int _col,
# str _value)
#{
# str xlcol = this.getColChar(_col);;
# arr = new Array(_types);
# arr.value(1,_value);
# range = excelWorkSheet.Range(strfmt("%1%2",xlcol,_row));
# range.value2(COMVariant::createFromArray(arr));
#}
#
#
#
#
ENDSOURCE
SOURCE #xlInsertTblArr
#Array xlInsertTblArr(Common _common,
# int _refFieldId,
# Array _arr,
# int _idx)
#{
# dictIndex dictIndex ;
# ;
#
# dictIndex = new DictIndex(_common.tableId,_refFieldId);
# _arr.value(_idx,_common.(dictIndex.field(_refFieldId)));
#
# return _arr;
#}
ENDSOURCE
SOURCE #xlInterior
#void xlInterior(str _xlRowCol,
# int _Index)
#{
#
# ;
# range = excelWorkSheet.Range(_xlRowCol);
# interior = range.interior();
#
# interior.colorIndex(_Index);
#
#}
ENDSOURCE
SOURCE #xlLeftBorder
#void xlLeftBorder(COM _range)
#{
# Borders = _range.Borders();
# Border = Borders.Item(2);
# Border.lineStyle(7);
# Border.Weight(3);
#}
ENDSOURCE
SOURCE #xlRange
#com xlRange(str _row, str _col)
#{
# ;
# range = excelWorkSheet.Range(_row,_col);
# return range;
#}
ENDSOURCE
SOURCE #xlrightBorder
#void xlrightBorder(COM _range)
#{
# Borders = _range.Borders();
# Border = Borders.Item(1);
# Border.lineStyle(7);
# Border.Weight(3);
#}
ENDSOURCE
SOURCE #xlShow
#void xlShow()
#{
# excelApplication.visible(true);
#}
ENDSOURCE
SOURCE #xlStyle
#void xlStyle(Com _range,Int _color,str _styleName)
#{
# styles = null;
# style = null;
# interior = null;
# styles = excelWorkBook.styles();
# style = styles.add(_styleName);
# interior = style.interior();
# interior.color(_color);//WinApi::RGB2int(246, 233, 206));
# _range.style(_styleName);
#}
ENDSOURCE
SOURCE #xlTopBorder
#void xlTopBorder(COM _range)
#{
# Borders = _range.Borders();
# Border = Borders.Item(8);
# Border.lineStyle(7);
# Border.Weight(3);
#}
ENDSOURCE
SOURCE #xlWorkSheetName
#void xlWorkSheetName(str _name)
#{
# excelWorkSheet.name(_name);
#}
ENDSOURCE
SOURCE #xlWrap
#void xlWrap(COM _range)
#{
# wrapText = _range.wraptext(true);
#}
ENDSOURCE
ENDMETHODS
ENDCLASS

***Element: END

X++ Code to restrict user login multiple times in ax2009

Copy Paste the Following Code in startupPost method of info class in AOT

void startupPost()
{
// To restrict user login form second login
xSession session;
SysClientSessions SysClientSessions;
UserId currentUserId;
int counter;

;

currentUserId = curUserId();

if(currentUserId!="Admin")// Allow Admin User to login multiple time
{
while select SysClientSessions
where SysClientSessions.userId == currentUserId &&
SysClientSessions.Status == 1 // 1 : Login 0 : Logout
{
session = new xSession(SysClientSessions.SessionId, true);
if (session && session.userId())
{
counter++;
}
}

if(counter>=2)
{
Box::stop("Already Logged-in : The same user id can't log in twice.");
infolog.shutDown(true);
}
}
}


Please take backup of your application before copying code