IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C++Builder Discussion :

composant TExcel dans c++ builder


Sujet :

C++Builder

  1. #1
    Membre averti
    Profil pro
    Inscrit en
    Février 2005
    Messages
    11
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Février 2005
    Messages : 11
    Par défaut composant TExcel dans c++ builder
    bonjour à tous je suis à la recherche de documentation sur les composants Texcel de builder 6 quelqu'un
    pourrait il m'aider à charger un fichier excel avec ce composant ?

  2. #2
    Membre éprouvé
    Avatar de Freeze
    Homme Profil pro
    Inscrit en
    Octobre 2002
    Messages
    131
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Octobre 2002
    Messages : 131
    Par défaut
    tu peux utiliser cette classe :

    le fichier .cpp :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    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
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
     
    //---------------------------------------------------------------------------
    #include "Excel.h"
    //---------------------------------------------------------------------------
    TExcel::TExcel(AnsiString nom)
    {
       FDejaOpen = true;
       FOleExcel.Clear();
       FWorkbook.Clear();
       FWorksheet.Clear();
       Connect();
       OpenBook(nom);
    }
    //---------------------------------------------------------------------------
    TExcel::~TExcel()
    {
       SaveBook();
       CloseBook();
       Deconnect();
    }
    //---------------------------------------------------------------------------
    void TExcel::Connect(void)
    {
       if (FOleExcel.IsEmpty())
       {
          FDejaOpen = true;
          try
          {  FOleExcel = Variant::GetActiveObject("Excel.Application");   }
          catch(...)
          {  FOleExcel = Variant::CreateObject("Excel.Application"); FDejaOpen=false;      }
       }
    }
    //---------------------------------------------------------------------------
    void TExcel::Deconnect(void)
    {
       if (!FOleExcel.IsEmpty())
       {
          CloseBook();
          if (!FDejaOpen)
             FOleExcel.OleFunction("Quit");
          FOleExcel.Clear();
          FWorkbook.Clear();
          FWorksheet.Clear();
       }
    }
    //---------------------------------------------------------------------------
    bool TExcel::OpenBook(AnsiString nom)
    {
       bool ret=false;
       Variant name,workbooks;
     
       name=nom;   name.ChangeType(varOleStr);
       try
       {
          workbooks = FOleExcel.OlePropertyGet("Workbooks");
          FWorkbook = workbooks.OleFunction("Open",name);
       }
       catch(...)
       {  FWorkbook.Clear();   }
       if (!FWorkbook.IsEmpty())      ret=true;
       return(ret);
    }
    //---------------------------------------------------------------------------
    void TExcel::CloseBook(void)
    {
       Variant savechange;
       savechange=false;
       if (!FWorkbook.IsEmpty())
       {
          try   {  FWorkbook.OleFunction("Close",savechange);   }
          catch(...)         {  }
       }
       FWorkbook.Clear();
       FWorksheet.Clear();
    }
    //---------------------------------------------------------------------------
    void TExcel::SaveBook(void)
    {
       if (!FWorkbook.IsEmpty())
       {
          try   {  FWorkbook.OleProcedure("Save");  }
          catch(...)  {  }
       }
    }
    //---------------------------------------------------------------------------
    int TExcel::CountSheet(void)
    {
       int nb=0;
       Variant worksheets;
       if (!FWorkbook.IsEmpty())
       {
          try
          {
             worksheets=FWorkbook.OlePropertyGet("Worksheets");
             nb = worksheets.OlePropertyGet("Count");
          }
          catch(...)  {  }
       }
       return(nb);
    }
    //---------------------------------------------------------------------------
    AnsiString TExcel::SelectSheet(int sheet)
    {
       AnsiString n="";
       if (!FWorkbook.IsEmpty())
       {
          try
          {
             FWorksheet = FWorkbook.OlePropertyGet("Worksheets",sheet);
             FWorksheet.OleProcedure("Activate");
             n = FWorksheet.OlePropertyGet("Name");
          }
          catch(...)  {  }
       }
       return(n);
    }
    //---------------------------------------------------------------------------
    bool TExcel::GetCell(int lig,int col,Variant &value)
    {
       bool ret=false;
       Variant cell;
       if (!FWorksheet.IsEmpty())
       {
          try
          {
             cell=FWorksheet.OlePropertyGet("Cells",lig,col);
             value=cell.OlePropertyGet("Value");
             ret=true;
          }
          catch(...)
          {  value.Clear(); }
       }
       return(ret);
    }
    //---------------------------------------------------------------------------
    void TExcel::SetCell(int lig,int col,Variant value)
    {
       Variant cell;
       if (!FWorksheet.IsEmpty())
       {
          try
          {
             cell = FWorksheet.OlePropertyGet("Cells",lig,col);
             value.ChangeType(varOleStr);
             cell.OlePropertySet("Value",value);
          }
          catch(...)  {  }
       }
    }
    //---------------------------------------------------------------------------
    void TExcel::GetMaxRowCol(int &lig,int &col)
    {
       Variant cell;
       if (!FWorksheet.IsEmpty())
       {
          try
          {
             cell = FWorksheet.OlePropertyGet("Cells").OleFunction("SpecialCells",11);
             lig = cell.OlePropertyGet("Row");
             col = cell.OlePropertyGet("Column");
          }
          catch(...)  {  lig=0;   col=0;   }
       }
    }
    //---------------------------------------------------------------------------
    void TExcel::PrintSheet(void)
    {
       if (!FWorksheet.IsEmpty())
       {
          try
          {  FWorksheet.OleProcedure("PrintOut"); }
          catch(...)  { }
       }
    }
    //---------------------------------------------------------------------------
    void TExcel::SetBorder(int lig,int col,bool visible)
    {
       Variant border,cell;
       if (!FWorksheet.IsEmpty())
       {
          try
          {
             cell=FWorksheet.OlePropertyGet("Cells",lig,col);
             border=cell.OlePropertyGet("Borders");
             if (visible)
                border.OlePropertySet("Weight",-4138);
             else
                border.OlePropertySet("Weight",0);
     
          }
          catch(...)
          {   }
       }
    }
    //---------------------------------------------------------------------------
    le fichier .h

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    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
     
    //---------------------------------------------------------------------------
    #ifndef ExcelH
    #define ExcelH
    //---------------------------------------------------------------------------
    #include <Classes.hpp>
    //---------------------------------------------------------------------------
    class TExcel
    {
       private:
          void Connect(void);
          void Deconnect(void);
          bool OpenBook(AnsiString nom);
          void CloseBook(void);
          void SaveBook(void);
     
          bool FDejaOpen;
          Variant FOleExcel;
          Variant FWorkbook;
          Variant FWorksheet;
       public:
          TExcel(AnsiString nom);
          ~TExcel();
     
          int CountSheet(void);
          void PrintSheet(void);
          void SetBorder(int lig,int col,bool visible);
          AnsiString SelectSheet(int sheet);
          bool GetCell(int lig,int col,Variant &value);
          void SetCell(int lig,int col,Variant value);
          void GetMaxRowCol(int &lig,int &col);
    };
    //---------------------------------------------------------------------------
    #endif

  3. #3
    Membre averti
    Profil pro
    Inscrit en
    Février 2005
    Messages
    11
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Février 2005
    Messages : 11
    Par défaut Texcel
    merci pour l'info mais j'ai déjà essayer avec ce genre de code

    cela fonctionne mais pas pour ce que je veux faire

    j'aimerais ouvrir un fichier excel, le modifier etc... et quand je quitte excel j'aimerais avoir de nouveau la main dans mon programme pour pouvoir sauver le fichier excel avec un code particulier

    je pensais donc utiliser Texcel, Tworksheet TWorkbook qui ont eux des évènement qui se produisent quand on quitte Excel

    mais je n'arrive pas à charger de fichier Excel avec ces composants et je recherche donc des informations à ce sujet car je n'ai pas d'aide d'installer dans c++ builder sur ces composants

    voilà mon problème






    Citation Envoyé par Freeze
    tu peux utiliser cette classe :

    le fichier .cpp :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    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
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
     
    //---------------------------------------------------------------------------
    #include "Excel.h"
    //---------------------------------------------------------------------------
    TExcel::TExcel(AnsiString nom)
    {
       FDejaOpen = true;
       FOleExcel.Clear();
       FWorkbook.Clear();
       FWorksheet.Clear();
       Connect();
       OpenBook(nom);
    }
    //---------------------------------------------------------------------------
    TExcel::~TExcel()
    {
       SaveBook();
       CloseBook();
       Deconnect();
    }
    //---------------------------------------------------------------------------
    void TExcel::Connect(void)
    {
       if (FOleExcel.IsEmpty())
       {
          FDejaOpen = true;
          try
          {  FOleExcel = Variant::GetActiveObject("Excel.Application");   }
          catch(...)
          {  FOleExcel = Variant::CreateObject("Excel.Application"); FDejaOpen=false;      }
       }
    }
    //---------------------------------------------------------------------------
    void TExcel::Deconnect(void)
    {
       if (!FOleExcel.IsEmpty())
       {
          CloseBook();
          if (!FDejaOpen)
             FOleExcel.OleFunction("Quit");
          FOleExcel.Clear();
          FWorkbook.Clear();
          FWorksheet.Clear();
       }
    }
    //---------------------------------------------------------------------------
    bool TExcel::OpenBook(AnsiString nom)
    {
       bool ret=false;
       Variant name,workbooks;
     
       name=nom;   name.ChangeType(varOleStr);
       try
       {
          workbooks = FOleExcel.OlePropertyGet("Workbooks");
          FWorkbook = workbooks.OleFunction("Open",name);
       }
       catch(...)
       {  FWorkbook.Clear();   }
       if (!FWorkbook.IsEmpty())      ret=true;
       return(ret);
    }
    //---------------------------------------------------------------------------
    void TExcel::CloseBook(void)
    {
       Variant savechange;
       savechange=false;
       if (!FWorkbook.IsEmpty())
       {
          try   {  FWorkbook.OleFunction("Close",savechange);   }
          catch(...)         {  }
       }
       FWorkbook.Clear();
       FWorksheet.Clear();
    }
    //---------------------------------------------------------------------------
    void TExcel::SaveBook(void)
    {
       if (!FWorkbook.IsEmpty())
       {
          try   {  FWorkbook.OleProcedure("Save");  }
          catch(...)  {  }
       }
    }
    //---------------------------------------------------------------------------
    int TExcel::CountSheet(void)
    {
       int nb=0;
       Variant worksheets;
       if (!FWorkbook.IsEmpty())
       {
          try
          {
             worksheets=FWorkbook.OlePropertyGet("Worksheets");
             nb = worksheets.OlePropertyGet("Count");
          }
          catch(...)  {  }
       }
       return(nb);
    }
    //---------------------------------------------------------------------------
    AnsiString TExcel::SelectSheet(int sheet)
    {
       AnsiString n="";
       if (!FWorkbook.IsEmpty())
       {
          try
          {
             FWorksheet = FWorkbook.OlePropertyGet("Worksheets",sheet);
             FWorksheet.OleProcedure("Activate");
             n = FWorksheet.OlePropertyGet("Name");
          }
          catch(...)  {  }
       }
       return(n);
    }
    //---------------------------------------------------------------------------
    bool TExcel::GetCell(int lig,int col,Variant &value)
    {
       bool ret=false;
       Variant cell;
       if (!FWorksheet.IsEmpty())
       {
          try
          {
             cell=FWorksheet.OlePropertyGet("Cells",lig,col);
             value=cell.OlePropertyGet("Value");
             ret=true;
          }
          catch(...)
          {  value.Clear(); }
       }
       return(ret);
    }
    //---------------------------------------------------------------------------
    void TExcel::SetCell(int lig,int col,Variant value)
    {
       Variant cell;
       if (!FWorksheet.IsEmpty())
       {
          try
          {
             cell = FWorksheet.OlePropertyGet("Cells",lig,col);
             value.ChangeType(varOleStr);
             cell.OlePropertySet("Value",value);
          }
          catch(...)  {  }
       }
    }
    //---------------------------------------------------------------------------
    void TExcel::GetMaxRowCol(int &lig,int &col)
    {
       Variant cell;
       if (!FWorksheet.IsEmpty())
       {
          try
          {
             cell = FWorksheet.OlePropertyGet("Cells").OleFunction("SpecialCells",11);
             lig = cell.OlePropertyGet("Row");
             col = cell.OlePropertyGet("Column");
          }
          catch(...)  {  lig=0;   col=0;   }
       }
    }
    //---------------------------------------------------------------------------
    void TExcel::PrintSheet(void)
    {
       if (!FWorksheet.IsEmpty())
       {
          try
          {  FWorksheet.OleProcedure("PrintOut"); }
          catch(...)  { }
       }
    }
    //---------------------------------------------------------------------------
    void TExcel::SetBorder(int lig,int col,bool visible)
    {
       Variant border,cell;
       if (!FWorksheet.IsEmpty())
       {
          try
          {
             cell=FWorksheet.OlePropertyGet("Cells",lig,col);
             border=cell.OlePropertyGet("Borders");
             if (visible)
                border.OlePropertySet("Weight",-4138);
             else
                border.OlePropertySet("Weight",0);
     
          }
          catch(...)
          {   }
       }
    }
    //---------------------------------------------------------------------------
    le fichier .h

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    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
     
    //---------------------------------------------------------------------------
    #ifndef ExcelH
    #define ExcelH
    //---------------------------------------------------------------------------
    #include <Classes.hpp>
    //---------------------------------------------------------------------------
    class TExcel
    {
       private:
          void Connect(void);
          void Deconnect(void);
          bool OpenBook(AnsiString nom);
          void CloseBook(void);
          void SaveBook(void);
     
          bool FDejaOpen;
          Variant FOleExcel;
          Variant FWorkbook;
          Variant FWorksheet;
       public:
          TExcel(AnsiString nom);
          ~TExcel();
     
          int CountSheet(void);
          void PrintSheet(void);
          void SetBorder(int lig,int col,bool visible);
          AnsiString SelectSheet(int sheet);
          bool GetCell(int lig,int col,Variant &value);
          void SetCell(int lig,int col,Variant value);
          void GetMaxRowCol(int &lig,int &col);
    };
    //---------------------------------------------------------------------------
    #endif

  4. #4
    Membre éprouvé
    Avatar de Freeze
    Homme Profil pro
    Inscrit en
    Octobre 2002
    Messages
    131
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Octobre 2002
    Messages : 131
    Par défaut
    je vois pas trop ce qui te manque dans ce genre de code ... quant aux composants fournit avec Borland C++ Builder, je te confirme qu'il n'y a pas de documentation, quant à leur fiabilité, j'en mettrai pas ma main à couper ( j'ai moi même pu vérifier le bordel que ca génère avec différentes versions de Office )... je préfère largement le OLE ...

  5. #5
    Membre averti
    Profil pro
    Inscrit en
    Février 2005
    Messages
    11
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Février 2005
    Messages : 11
    Par défaut Texcel
    merci pour l'info

    si on utilise Ole, j'arrive à ouvrir un fichier excel à le visualiser dans Excel

    mais quand je quitte j'aimerai qu excel ne me demande pas de confirmation pour la sauvegarde du fichier que j'ai modifier

    je sauve en fait un fichier au format csv et excel me pose trois ou quatre question pour pouvoir faire sa sauvegarde et c'est très ennnuyeux

    j'aimerais qu'il sauve le fichier sans demander de confirmation

    mais si je mets displayAlerts à false Excel ne sauve plus rien !!!

  6. #6
    Membre éprouvé
    Avatar de Freeze
    Homme Profil pro
    Inscrit en
    Octobre 2002
    Messages
    131
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Octobre 2002
    Messages : 131
    Par défaut
    je crois comprendre ce que tu veux ... je te conseille de te pencher sur les macros en VBA ... c'est la meilleure méthode à mon avis ...

  7. #7
    Membre averti
    Profil pro
    Inscrit en
    Février 2005
    Messages
    11
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Février 2005
    Messages : 11
    Par défaut Texcel
    faire une macro de sauvegarde en vba qui enlèverait displayAllerts quand je quitte Excel c'est faisable mais c'est trop compliqué d' installer la macro de manière automatique sur les postes où se trouve mon programme et puis comment installer une macro de manière automatique ???

  8. #8
    Membre averti
    Profil pro
    Inscrit en
    Février 2005
    Messages
    11
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Février 2005
    Messages : 11
    Par défaut Texcel
    jai résolu le problème en utilisant un olecontainer et en sauvant moi-même le fichier Excel sous le format CSV
    seul petit soucis , c'est un peu plus lent pour l'ouverture et la fermeture du fichier Excel
    Si quelqu'un a une idée pour accélerer cela, elle sera la bienvenue
    merci

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Utilisation de 'vieilles' libraires dans C Builder 6
    Par Gore dans le forum C++Builder
    Réponses: 7
    Dernier message: 14/10/2004, 21h31
  2. Probleme de composant inclus dans un autre.
    Par viro dans le forum C++Builder
    Réponses: 7
    Dernier message: 05/04/2004, 15h44
  3. [RAVE]Composant RTF dans une feuille RAVE ?
    Par hpalpha dans le forum Rave
    Réponses: 3
    Dernier message: 29/03/2004, 19h25
  4. VCL de Crystal Report pour utilisation dans C++Builder
    Par dibak dans le forum C++Builder
    Réponses: 4
    Dernier message: 16/02/2004, 17h04
  5. Documentation DirectX dans C++Builder 3
    Par srvremi dans le forum DirectX
    Réponses: 1
    Dernier message: 26/04/2002, 09h59

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo