Tableau croisé dynamique

Résolu
cs_couf Messages postés 213 Date d'inscription dimanche 5 janvier 2003 Statut Membre Dernière intervention 6 janvier 2011 - 8 juil. 2009 à 18:22
cs_couf Messages postés 213 Date d'inscription dimanche 5 janvier 2003 Statut Membre Dernière intervention 6 janvier 2011 - 15 juil. 2009 à 17:21
Bonjour à Tous 

Je viens encore solliciter votre aide, et remercie quiconque voudra bien se pencher sur mon code
 Je tiens à dire que la recherche francophone et anglophone sur le sujet en delphi est très rare.

Voilà   j'ai le code suivant (c'est une application console car dois se lancer via scheduller)
program TCD;

{$APPTYPE CONSOLE}

uses
  Windows,
  Messages,
  SysUtils,
  Variants,
  Classes,
  Graphics,
  Controls,
  Forms,
  Dialogs,
  comobj,
  ExtDlgs,
  StdCtrls,
  strutils,
  shellapi,
  inifiles,
  comini;

var
chrep:string;
  vlig,vcellimage,vsupCol,vCell,vcellfusion,vcellfusion1,vWorksheet,vXLWorkbook, vMSExcel,vXLWorkbooks,vMS1Excel,xlapp:variant;
   fileini:Tinifile;
   aSheetName ,vFileName : AnsiString;
   xls, pivot, fld: OLEVariant;
   const
   xlDatabase = $00000001;
   xlRowField = $00000001;
 //  xlDatabase = $00000001;

  xlColumnField = $00000002;
  xlDataField = $00000004;
  xlPageField = $00000003;
 // xlRowField = $00000001;

  xlAverage = $FFFFEFF6;
  xlCount = $FFFFEFF0;
  xlCountNums = $FFFFEFEF;
  xlMax = $FFFFEFD8;
  xlMin = $FFFFEFD5;
  xlProduct = $FFFFEFCB;
  xlStDev = $FFFFEFC5;
  xlStDevP = $FFFFEFC4;
  xlSum = $FFFFEFC3;

begin
  { TODO -oUser -cConsole Main : Insert code here }

  try
 fileIni:= Tinifile.Create('C:\center\Xcel.ini');
 chrep:=fileini.ReadString('petits_pois','reporting','')  ;
// chimg:=fileini.ReadString('chemin','image','');
 finally

end;

  vMSExcel := CreateOleObject('Excel.Application');
    vMSExcel.Visible := true;
    vFileName := chrep;
    vXLWorkbooks := vMSExcel.Workbooks;
    vXLWorkbook := vXLWorkbooks.Open(vFileName);
    aSheetName := 'conso petits_pois';
    vWorksheet := vXLWorkbook.WorkSheets[aSheetName];

    vcell:=vworksheet.range['A3'];
    vcell.select;
    vcell.currentregion.select;

    vxlworkbook.pivotcaches.add(xlDatabase,quotedstr('conso petits_pois')+ 'R3C1:R251C21').createpivottable('','TCD1');
     {xls.Active***.PivotTableWizard(TableDestination :=
xls.Active***.Cells[3, 1]); }
    vcell.pivottablewizard(vxlworkbook.cells[3,1]);

    vcell.activesheet.pivottables('TCD1').pivotfields('Ville').orientation:=xlrowfield;
    vcell.activesheet.pivottables('TCD1').pivotfields('Ville').caption:='ville';

    vcell.activesheet.pivottables('TCD1').pivotfields('Ville').position:=1;
    vcell.activesheet.pivottables('TCD1').pivotfields('Ville').function:=1;
    vcell.activesheet.pivottables('TCD1').format(4);

Ma feuille excel s'ouvre bien (je l'ai fait pour controle) toute ma partie donnée s'ouvre
mais je n'ai aucun début de TCD .

je pense que le problème vien de ces deux lignes:

vxlworkbook.pivotcaches.add(xlDatabase,quotedstr('conso petits_pois')+ 'R3C1:R251C21').createpivottable('','TCD1');
    
    vcell.pivottablewizard(vxlworkbook.cells[3,1]);

mais je ne comprends pas ou j'ai fais l'erreur

saus si la ligne du dessus" vcell.currentregion.select;" est mal employé mais je ne vois pas comment faire.

si un de vous veux bien me faire tourner  mon code je l'en remercie par avance

end.

couf

3 réponses

cs_couf Messages postés 213 Date d'inscription dimanche 5 janvier 2003 Statut Membre Dernière intervention 6 janvier 2011 1
15 juil. 2009 à 17:21
Bonjour

Bon je vais me répondre à moi même ;-)
ce site m'a souvent aidé et sa philosophie est bonne
je décide de mettre ce petit bout de code ici car question TCD Delphi
on ne trouve rien sur aucun site Francophone ni anglophone du reste.
Si ce bout de code vous a été utile faites le moi savoir

Je vais donc mettre ici VOLONTAIREMENT sans fonction en mode console
le code:
Pour le réaliser je me suis appuyer sur les sites:
-http://trac.std.tsogu.ru/browser/zacommon/components/OfficeConst/ExcelConst.pas?rev= 1598 (pour les constantes tres bon site on peut se faire une unité)
-http://silkyroad.developpez.com/excel/tcd/ (exélent site si vous voulez apprendre vite très vite à faire un TCD)
-http://www.developpez.net/forums/d768495/environnements-developpement/delphi/api-com-sdks/delphi-ole-excel-tableau-croise-dynamique/ lien sur ce forum mais avec quelques erreurs


Ce TCD s'appuie sur un fichier excel de 24 colonnes et de 1722 lignes.

J'ai choisi de montrer comment créer un TCD sur une nouvelle feuille

puis 2 TCD sur une PAGE


**************************************************************************************************************************************************************************************
**************************************************************************************************************************************************************************************
program TCD;

{$APPTYPE CONSOLE}

uses
Windows,
Messages,
SysUtils,
Variants,
Classes,
Graphics,
Controls,
Forms,
Dialogs,
comobj,
ExtDlgs,
StdCtrls,
strutils,
shellapi,
inifiles,
comini; [b]// c'est une unité que j'ai créé car demandé par le compilateur
/b
var
chrep,jourq:string;
Jour1:integer;
vCell,vWorksheet, vMSExcel:olevariant;
fileini:Tinifile;
FichiersXLS, aSheetName ,vFileName : AnsiString;


const [b]// les constantes qui peuvent être utiles
/b
xlDatabase = $00000001;
xlRowField = $00000001;
xlColumnField = $00000002;
xlDataField = $00000004;
xlPageField = $00000003;
xlAverage = $FFFFEFF6;
xlCount = $FFFFEFF0;
xlCountNums = $FFFFEFEF;
xlMax = $FFFFEFD8;
xlMin = $FFFFEFD5;
xlProduct = $FFFFEFCB;
xlStDev = $FFFFEFC5;
xlStDevP = $FFFFEFC4;
xlSum = $FFFFEFC3;
xlR1C1 = -4150 ;



begin
{ TODO -oUser -cConsole Main : Insert code here }




[b]//On ouvre une nouvelle instance
/b
vMSExcel := CreateOleObject('Excel.Application');
vMSExcel.Visible := true;
vFileName := 'c:\TCD\conso.xls';
aSheetName := 'conserve';

//astuce dans mon fichier qui va me permettre de récupérer la valeur d'une cellule
vmsexcel.workbooks.open(vfilename);
vcell: = vmsexcel.activeworkbook.activesheet.range['E4'];
jour1:= vcell.value;
jourq :=inttostr(jour1) ;
delete(jourq, 7, 2);

// ici il faut sélectionner tous les champs qui vont être concerné par notre Tableau croisé dynamique
vmsexcel.activeworkbook.activesheet.range['A3'];
vmsexcel.selection.currentregion.select;

// création d'un tableau croisé dynamique
vmsexcel.activeworkbook.PivotCaches.add(xlDatabase,quotedstr
('conso MDM')+ '!R3C1:R251C21').CreatePivotTable('', 'TCD1','XlPivotTableversion10');;


[b]//champs pages "xlpagefield" n° compte ,produit
/b
vmsexcel.activeworkbook.activesheet.PivotTables('TCD1').pivotfields('n° compte').orientation: =xlpagefield;
vmsexcel.activeworkbook.activesheet.pivottables('TCD1').pivotfields('n° compte').caption:= 'n° compte';
vmsexcel.activeworkbook.activesheet.pivottables('TCD1').pivotfields('n° compte').position:=1;

vmsexcel.activeworkbook.activesheet.PivotTables('TCD1').pivotfields('produit').orientation:=xlpagefield;
vmsexcel.activeworkbook.activesheet.pivottables('TCD1').pivotfields('produit').caption:='produit';
vmsexcel.activeworkbook.activesheet.pivottables('TCD1').pivotfields('produit').position:=2;


[b] //Champs lignes "xlrowfield" villes produits
/bvmsexcel.activeworkbook.activesheet.PivotTables('TCD1').pivotfields('Ville').orientation:=xlrowfield;
vmsexcel.activeworkbook.activesheet.pivottables('TCD1').pivotfields('Ville').caption:='ville';
vmsexcel.activeworkbook.activesheet.pivottables('TCD1').pivotfields('Ville').position:=1;

vmsexcel.activeworkbook.activesheet.PivotTables('TCD1').pivotfields('Prestations').orientation:=xlrowfield;
vmsexcel.activeworkbook.activesheet.pivottables('TCD1').pivotfields('Prestations').caption:='Prestations';
vmsexcel.activeworkbook.activesheet.pivottables('TCD1').pivotfields('Prestations').position:=2;


// Champs données "xldatafield" Nombre de n° récep Nombre de nb pal Poids montant HT C°


vmsexcel.activeworkbook.activesheet.PivotTables('TCD1').pivotfields('n° récep').orientation: =xldatafield ;

vmsexcel.activeworkbook.activesheet.PivotTables('TCD1').pivotfields('action').orientation:= xldatafield ;

vmsexcel.activeworkbook.activesheet.PivotTables('TCD1').pivotfields('nb pal').orientation:=xldatafield;
vmsexcel.activeworkbook.activesheet.pivottables('TCD1').pivotfields('nb pal').caption:='nb pal';


vmsexcel.activeworkbook.activesheet.PivotTables('TCD1').pivotfields('Poids').orientation:=xldatafield;
vmsexcel.activeworkbook.activesheet.pivottables('TCD1').pivotfields('Poids').caption:='poids';


vmsexcel.activeworkbook.activesheet.PivotTables('TCD1').pivotfields('montant HT C°').orientation:=xldatafield;
vmsexcel.activeworkbook.activesheet.pivottables('TCD1').pivotfields('montant HT C°').caption:='montant HT';



Ici on choisit le format du TCD (voir le 3ème lien de la ligographie)

vmsexcel.activeworkbook.activesheet.pivottables('TCD1').format(4);

[b]//ATTENTION ici vous pourriez vous arracher les cheveux la propriété function de la classe pivotfields
par défaut quand vous faites votre TCD et que votre programme rencontre des valeurs numériques par défaut il en fait une somme.
si vous voulez compter une valeur numérique vous devez utiliser les 2 fonctions xlCount (case non vide de valeur numérique ou non)
ou xlcountnums (case non vide valeur numérique uniquement) pivotfields('somme de n° récep') parceque le champs change de
nom et s'appelle désormais 'somme de n° récep'/b

vmsexcel.activeworkbook.activesheet.pivottables('TCD1').pivotfields('somme de n° récep').function:=-4113;

vmsexcel.activeworkbook.activesheet.name:='récap_' + jourq ;
on renomme la feuille de calcul de son TCD

vMSExcel.activeworkbook.worksheets['conso MDM'].activate ;
vmsexcel.activeworkbook.activesheet.range['A3'];
vmsexcel.selection.currentregion.select;

{********************************************************************************************************************//
* 3 ème et 4 ème TCD sur la même feuille
*
********************************************************************************************************************}

// pour TCD3 même calcul que pour TCD1

vMSExcel.activeworkbook.worksheets['conso MDM'].activate ;
vmsexcel.activeworkbook.activesheet.range['A3'];
vmsexcel.selection.currentregion.select;


vmsexcel.activeworkbook.PivotCaches.add(xlDatabase,quotedstr
('conso MDM')+ '!R3C1:R251C21').CreatePivotTable('', 'TCD3','XlPivotTableversion10');


//champs pages

vmsexcel.activeworkbook.activesheet.PivotTables('TCD3').pivotfields('n° compte').orientation: =xlpagefield;
vmsexcel.activeworkbook.activesheet.pivottables('TCD3').pivotfields('n° compte').caption:= 'n° compte';
vmsexcel.activeworkbook.activesheet.pivottables('TCD3').pivotfields('n° compte').position:=1;

vmsexcel.activeworkbook.activesheet.PivotTables('TCD3').pivotfields('produit').orientation:=xlpagefield;
vmsexcel.activeworkbook.activesheet.pivottables('TCD3').pivotfields('produit').caption:='produit';
vmsexcel.activeworkbook.activesheet.pivottables('TCD3').pivotfields('produit').position:=2;


// champs lignes

vmsexcel.activeworkbook.activesheet.PivotTables('TCD3').pivotfields('dpt').orientation: =xlrowfield;
vmsexcel.activeworkbook.activesheet.pivottables('TCD3').pivotfields('dpt').caption:= 'dpt';
vmsexcel.activeworkbook.activesheet.pivottables('TCD3').pivotfields('dpt').position:=1;

//champs données

vmsexcel.activeworkbook.activesheet.PivotTables('TCD3').pivotfields('n° récep').orientation:=xldatafield ;
vmsexcel.activeworkbook.activesheet.PivotTables('TCD3').pivotfields('nb pal').orientation:=xldatafield;
vmsexcel.activeworkbook.activesheet.pivottables('TCD3').pivotfields('nb pal').caption:='nb pal';
vmsexcel.activeworkbook.activesheet.PivotTables('TCD3').pivotfields('Poids').orientation:=xldatafield;
vmsexcel.activeworkbook.activesheet.pivottables('TCD3').pivotfields('Poids').caption:='poids';
vmsexcel.activeworkbook.activesheet.PivotTables('TCD3').pivotfields('montant HT ').orientation:=xldatafield;
vmsexcel.activeworkbook.activesheet.pivottables('TCD3').pivotfields('montant HT ').caption:='montant HT';

vmsexcel.activeworkbook.activesheet.pivottables('TCD3').format(4);

vmsexcel.activeworkbook.activesheet.pivottables('TCD3').pivotfields('somme de n° récep').function:=-4113;

vmsexcel.activeworkbook.activesheet.name:='recap_dpt' ;




//4 ème Tableau on va le mettre dans la même feuille que TCD3

vworksheet : = vmsexcel.worksheets['recap_dpt'];
vcell:= vworksheet.range['I3']; // ici il nous faut un variant sur la cellule I3


vMSExcel.activeworkbook.worksheets['conso MDM'] ;
vmsexcel.activeworkbook.activesheet.range['A3'];
vmsexcel.selection.currentregion.select;

vmsexcel.activeworkbook.PivotCaches.add(xlDatabase,quotedstr
('conso MDM')+ '!R3C1:R251C21').CreatePivotTable(vcell, 'TCD4','XlPivotTableversion10');

[b] //champs pages ne pas oublier avant d'activer la feuille ou l'on veut mettre les données
/b
vcell.activate;
vmsexcel.activeworkbook.activesheet.PivotTables('TCD4').pivotfields('n° compte').orientation: =xlpagefield;
vmsexcel.activeworkbook.activesheet.pivottables('TCD4').pivotfields('n° compte').caption:= 'n° compte';
vmsexcel.activeworkbook.activesheet.pivottables('TCD4').pivotfields('n° compte').position:=1;

vmsexcel.activeworkbook.activesheet.PivotTables('TCD4').pivotfields('produit').orientation:=xlpagefield;
vmsexcel.activeworkbook.activesheet.pivottables('TCD4').pivotfields('produit').caption:='produit';
vmsexcel.activeworkbook.activesheet.pivottables('TCD4').pivotfields('produit').position:=2;


//champs lignes

vmsexcel.activeworkbook.activesheet.PivotTables('TCD4').pivotfields('PF').orientation: =xlrowfield;
vmsexcel.activeworkbook.activesheet.pivottables('TCD4').pivotfields('PF').caption:= 'PF';
vmsexcel.activeworkbook.activesheet.pivottables('TCD4').pivotfields('PF').position:=1;




//champs données


vmsexcel.activeworkbook.activesheet.PivotTables('TCD4').pivotfields('n° récep').orientation: =xldatafield ;
vmsexcel.activeworkbook.activesheet.PivotTables('TCD4').pivotfields('nb pal').orientation:=xldatafield;
vmsexcel.activeworkbook.activesheet.pivottables('TCD4').pivotfields('nb pal').caption:='nb pal';
vmsexcel.activeworkbook.activesheet.PivotTables('TCD4').pivotfields('Poids').orientation:=xldatafield;
vmsexcel.activeworkbook.activesheet.pivottables('TCD4').pivotfields('Poids').caption:='poids';
vmsexcel.activeworkbook.activesheet.PivotTables('TCD4').pivotfields('montant HT C°').orientation:=xldatafield;
vmsexcel.activeworkbook.activesheet.pivottables('TCD4').pivotfields('montant HT C°').caption:='montant HT';

vmsexcel.activeworkbook.activesheet.pivottables('TCD4').format(4);

vmsexcel.activeworkbook.activesheet.pivottables('TCD4').pivotfields('somme de n° récep').function:=-4113;

// vmsexcel.activeworkbook.activesheet.name:='recap_PF' ;


vmsexcel.activeworkbook.SaveAs(FichiersXLS,-4143); // pas obligatoire ici mon fichier original est en excel V2.0 eeeeeeeeeh oui
ne pas oublier de libérer le processus
vmsexcel.quit;
vmsexcel := unassigned;


end.







couf
3
cs_couf Messages postés 213 Date d'inscription dimanche 5 janvier 2003 Statut Membre Dernière intervention 6 janvier 2011 1
9 juil. 2009 à 12:12
Bonjour

Toujours moi,

  En fait après avoir toiletté mon code, je me suis rendu compte
que cela venait de ces lignes

 vmsexcel.selection.currentregion.select;

    vmsexcel.activeworkbook.PivotCaches.add(xlDatabase,quotedstr
    ('conso MDM')+ 'R3C1:R251C21').CreatePivotTable('', 'TCD1','XlPivotTableversion10');;
    
    vmsexcel.workbooks.activesheet.pivottablewizard(vmsexcel.activeworkbook.activesheet.cells[3,1]);

  El là je bloque dur
Si quelqu'un pouvait m'aiguiller SVP

couf
0
cs_couf Messages postés 213 Date d'inscription dimanche 5 janvier 2003 Statut Membre Dernière intervention 6 janvier 2011 1
9 juil. 2009 à 17:29
Re,

  J'avance à petit pas , je vais mettre tout mon codecorrigé icic ar je pense qu'il va aider un jour quelqu'un
bref

si quelqu'un nous suis je cherche juste un coup de main SVP pour ceci

vmsexcel.activeworkbook.activesheet.PivotTables('TCD1').pivotfields('nb pal').orientation:=xldatafield;
    vmsexcel.activeworkbook.activesheet.pivottables('TCD1').pivotfields('nb pal').caption:='nbre pal';
    vmsexcel.activeworkbook.activesheet.pivottables('TCD1').pivotfields('nbre pal').function:=-4113;

je veux juste compter le champ nbre pal
j'ai essayé avec  xlCountNums = $FFFFEFEF;
                                                0
                                                1
mais j'obtiens toujurs     code erreurs

"impossible de définir la propriété function de la classe pivotfields"
a tout hasard.......

couf
0
Rejoignez-nous