Excel Export Zellen Formatieren

10. Mai 2010 14:26

Guten Tag,

habe noch ein wenig Schwierigkeiten mit einem excel export, kann man beispielsweise beim Export sagen das Beispielsweise die Ueberschriften bold sind oder vielleicht auch einzelnen Zellen ein Hintergrund bild hinzufuegen???


Code:
 
 SysExcelApplication      xlsApplication;
   SysExcelWorkBooks        xlsWorkBookCollection;
   SysExcelWorkBook         xlsWorkBook;
   SysExcelWorkSheets       xlsWorkSheetCollection;
   SysExcelWorkSheet        xlsWorkSheet;
   SysExcelRange            xlsRange;
   SysExcelStyles           xlsStyles;
   SysExcelStyle            xlsStyle;
   SysExcelInterior         xlsInterior;
   SysExcelFont             xlsFont;

// Excel initalisieren und öffnen.
        xlsApplication = SysExcelApplication::construct();
        xlsApplication.visible(true);

        // Neues Excel Worksheet erzeugen.
        xlsWorkBookCollection    = xlsApplication.workbooks();
        xlsWorkBook              = xlsWorkBookCollection.add();
        xlsWorkSheetCollection   = xlsWorkBook.worksheets();
        xlsWorkSheet             = xlsWorkSheetCollection.itemFromNum(1);
       
       


        // Zellenüberschriften in das Worksheet schreiben.
        xlsWorkSheet.cells().item(row,1).value('Article Nr.');
        xlsWorkSheet.cells().item(row,2).value('Article Name');
        xlsWorkSheet.cells().item(row,3).value('Group Id');
        xlsWorkSheet.cells().item(row,4).value('Prod. Group');
        xlsWorkSheet.cells().item(row,5).value('Gross Price');
       
        row++;
       


Muss doch mit xlsStyle.font().bold(true) irgendwie moeglichs ein oder sehe ich das Falsch?

Re: Excel Export Zellen Formatieren

10. Mai 2010 15:42

Hi,

um z.B. Überschriften als "bold" zu Formatieren kann man ungefähr so vorgehen:
Code:
COM    excelRange, font;
;
excelRange = xlsWorkSheet.cells().item(row,5).comObject();
font = excelRange.Font();
font.Bold(true);


Es kann sein, dass noch einige "Zwischenvariablen" verwendet werden müssen.
Es gab mal ein paar Probleme wenn man im Code mehr als zwei Methoden-Ebenen "übersprungen" hat.
Bsp.:
Code:
excelRange = xlsWorkSheet.cells().item(row,5).comObject();


Sollte dies so nicht funktionieren bitte so umschreiben, dass immer nur eine Methoden-Ebene verwendet wird.
Bsp.:
Code:
cells = xlsWorkSheet.cells();
item = cells.item(row, 5);
excelRange = item.comObject();


Ein Beispiel für das Einfügen eines Hintergrundbildes habe ich derzeit leider nicht zur Hand, sollte aber auch möglich sein.

Re: Excel Export Zellen Formatieren

11. Mai 2010 07:31

ahhh das ja suuuper vielen Dank.

Ich werde mal versuchen ein Hintergrundbild einzustellen und falls es klappt berichten.
Wie koennte es denn mit Rahmenlinien aussehen?


Ich hoffe nicht das ich das Thema allzusehr aufbausche aber ich habe eine augenscheinlich klasse Klasse ;-) gefunden die einen großen Umfang an Excel Formatierungen beinhaltet.
Kann man als xpo speichern und importieren...

Code:
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 BGOExcelReporter
#{
# 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


Waere vielleicht interessant wenn man dazu ein Beispiel schreiben koennte, ich habe es bisher noch nicht funktionstuechtig hinbekommen :-?