| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 
 | #pragma hdrstop
 
#include "CExcel.h"
#include <utilcls.h>
#include <stdio.h>
 
/*======================================================================
**  Nom         : Constructeur()
**  Description : rien à faire
**====================================================================*/
CExcel::CExcel()
{
  nomFichier[0] = 0;
  existe = false;
}
 
/*======================================================================
**  Nom         : ouvrirExcel()
**  Description : lancement d'Excel en arrière plan
**====================================================================*/
void CExcel::ouvrirExcel()
{
  if (  !existe )
  {
    CoInitialize(NULL);
    vMSExcel = Variant::CreateObject("Excel.Application");
    existe = true;
  }
  else
    vMSExcel = Variant::GetActiveObject("Excel.Application");
 
  vMSExcel.OlePropertySet("Visible", true);
}
 
/*======================================================================
**  Nom         : ouvrirExcel()
**  Description : lancement d'Excel et ouverture du fichier
** ---------------------------------------------------------------------
**  Parametres  : 2[E]
**     AnsiString nomFicExcel : nom du fichier à ouvrir
**====================================================================*/
void CExcel::ouvrirClasseur(AnsiString nomFicExcel)
{
  this->nomFicExcel = nomFicExcel;
  sprintf(nomFichier,"%s",nomFicExcel.c_str());
  vFileName = nomFichier; 
  vXLWorkbooks = vMSExcel.OlePropertyGet("Workbooks");
  vXLWorkbook = vXLWorkbooks.OleFunction("Open",vFileName);
  vWorksheet = vXLWorkbook.OlePropertyGet("Worksheets",1);
}
 
/*======================================================================
**  Nom         : ouvrirExcel()
**  Description : lancement d'Excel et ouverture du fichier
** ---------------------------------------------------------------------
**  Parametres  : 2[E]
**     AnsiString nomFicExcel : nom du fichier à ouvrir
**====================================================================*/
void CExcel::ouvrirClasseur(char *nomFicExcel)
{
  this->nomFicExcel = nomFicExcel;
  vFileName = nomFicExcel;
  vXLWorkbooks = vMSExcel.OlePropertyGet("Workbooks");
  vXLWorkbook = vXLWorkbooks.OleFunction("Open",vFileName);
  vWorksheet = vXLWorkbook.OlePropertyGet("Worksheets",1);
}
 
/*======================================================================
**  Nom         : creerClasseur()
**  Description : création d'un nouveau classeur
**====================================================================*/
void CExcel::creerClasseur()
{
  vXLWorkbooks = vMSExcel.OlePropertyGet("Workbooks");
  vXLWorkbook = vXLWorkbooks.OleFunction("Add");
  vWorksheet = vXLWorkbook.OlePropertyGet("Worksheets",1);
}
 
/*======================================================================
**  Nom         : sauvegarder()
**  Description : sauvegarde le classeur
**====================================================================*/
void CExcel::sauvegarder()
{
  if (nomFichier[0] != 0)
  {
    vXLWorkbook.OleProcedure("Save");
  }
  else
    Application->MessageBoxA("Le fichier n'a pas de nom","Erreur nom",MB_OK);
}
 
/*======================================================================
**  Nom         : sauvegarder()
**  Description : sauvegarde le classeur sous le nom passé en paramètre
** ---------------------------------------------------------------------
**  Parametres  : [E]
**     AnsiString nomFicExcel : nom du fichier sauvegardé
**====================================================================*/
void CExcel::sauvegarderSous(AnsiString nomFicExcel)
{
  char nom[40];
 
  strcpy(nomFichier,nomFicExcel.c_str());
  sprintf(nom,"%s",nomFicExcel.c_str());
  vFileName = nom;
  vXLWorkbook.OleProcedure("Saveas", vFileName);
}
 
/*======================================================================
**  Nom         : sauvegarder()
**  Description : sauvegarde le classeur sous le nom passé en paramètre
** ---------------------------------------------------------------------
**  Parametres  : [E]
**     AnsiString nomFicExcel : nom du fichier sauvegardé
**====================================================================*/
void CExcel::sauvegarderSous(char * nomFicExcel)
{
  char nom[40];
 
  strcpy(nomFichier,nomFicExcel);
  vFileName = nomFicExcel;
  vXLWorkbook.OleProcedure("Saveas", vFileName);
}
 
/*======================================================================
**  Nom         : fermerExcel()
**  Description : on quitte Excel
**====================================================================*/
void CExcel::fermerExcel()
{
  vMSExcel.OleFunction("Quit");   //MON PROBLEME EST LA!!!!!!
  vMSExcel = Unassigned;
}
 
/*======================================================================
**  Nom         : modifierCel()
**  Description : modification d'une cellule
** ---------------------------------------------------------------------
**  Parametres  : 2[E]
**     AnsiString nomCel : nom de la cellule
**     AnsiString val    : contenu de la cellule
**====================================================================*/
void CExcel::modifierCel(AnsiString nomCel, AnsiString val)
{
  char s[80];
 
  sprintf(s,"%s",nomCel.c_str());
  vRange = s;
  vWorksheet = vXLWorkbook.OlePropertyGet("Worksheets",1);
  vCell = vWorksheet.OlePropertyGet("Range", vRange);
  sprintf(s,"%s",val.c_str());
  vValue = s;
  vCell.OlePropertySet("value", vValue);
}
 
/*======================================================================
**  Nom         : valCel()
**  Description : contenu d'une cellule
** ---------------------------------------------------------------------
**  Parametres  : [E]
**     AnsiString cel : nom de la cellule
**  Retour : contenu de la cellule
**====================================================================*/
AnsiString CExcel::valCel(AnsiString cel)
{
  AnsiString val;
  Variant vValue, vRange, vCell;
  char s[10];
 
  vWorksheet = vXLWorkbook.OlePropertyGet("Worksheets",1);
  sprintf(s,"%s", cel.c_str() );
  vRange = s;
  vCell = vWorksheet.OlePropertyGet("Range", vRange);
  val = vCell.OlePropertyGet("Value");
  return val;
}
 
//---------------------------------------------------------------------------
 
#pragma package(smart_init) | 
Partager