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 :

Ouvrir un fichier excel existant avec borland C++Builder


Sujet :

C++Builder

  1. #1
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Juillet 2011
    Messages
    2
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Cameroun

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Santé

    Informations forums :
    Inscription : Juillet 2011
    Messages : 2
    Points : 3
    Points
    3
    Par défaut Ouvrir un fichier excel existant avec borland C++Builder
    bsr a tous je commence a manipuler l'OLE Excel avec Borland C++BUILDER 6 et je n'arrive pas a ouvrir un fichier existant
    j'ai une exception qui est générée. voici mon code:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    Variant vMSExcel;
    Variant vFileName,vXLWorkbooks,vXLWorkbook;
    vMSExcel = Variant::CreateObjet("Excel.Application");
    vFileName = "c:\\test.xls";
    vXLWorkbooks = vMSExcel.OleFunction("Workbooks");
    vXLWorkbook = vXLWorkbooks.OleFunction("Open",vFileName ) ;

  2. #2
    Membre habitué
    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
    Points : 162
    Points
    162
    Par défaut
    je pense qu'il faut que tu gères le CreateObject avec un GetActiveObject pour pas avoir de souci... après faut voir du côté du OleFunction à remplacer par un OlePropertyGet, je te file une petite classe que j'ai de côté pour ce genre de cas, c'est la TExcel, y a sûrement des petites choses à changer, mais ça doit être exploitable

    pour l'utiliser, ça doit donner un truc dans le genre :

    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
    #include <Excel.h>
    ...
    TExcel *e=new TExcel();
    e->Connect();
    e->Password=password;
    if (e->OpenBook(filename))
    {
       e->SelectSheet(nusheet);
       e->GetCell(ligne1,colonne1,&ret);   
       e->SetCell(ligne2,colonne2,valeur);
       e->SaveBook();
       e->CloseBook();
    }
    e->Deconnect();
    delete e;
    ...
    le .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
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    //---------------------------------------------------------------------------
    #include "Excel.h"
    //---------------------------------------------------------------------------
    TExcel::TExcel(void)
    {
       FDejaOpen = true;
       FOleExcel.Clear();
       FWorkbook.Clear();
       FWorksheet.Clear();
    }
    //---------------------------------------------------------------------------
    TExcel::~TExcel(void)
    {
    }
    //---------------------------------------------------------------------------
    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();
       }
    }
    //---------------------------------------------------------------------------
    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");
             Deprotege(FPassword);
          }
          catch(...)
          {
          }
       }
       return(n);
    }
    //---------------------------------------------------------------------------
    bool TExcel::Deprotege(AnsiString motpasse)
    {
       bool ret=false;
       Variant cell;
       if (!FWorksheet.IsEmpty())
       {
          try
          {
             FWorksheet.OleProcedure("Unprotect",motpasse);
             ret=true;
          }
          catch(...)
          {
          }
       }
       return(ret);
    }
    //---------------------------------------------------------------------------
    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;
          }
       }
    }
    //---------------------------------------------------------------------------
    et le .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 <vcl.h>
    //---------------------------------------------------------------------------
    class TExcel
    {
    	private:	// Déclarations utilisateur
    		bool FDejaOpen;
    		Variant FOleExcel;
    		Variant FWorkbook;
    		Variant FWorksheet;
          AnsiString FPassword;
    	public:		// Déclarations utilisateur
     
    		void Connect(void);
    		void Deconnect(void);
    		bool OpenBook(AnsiString nom);
    		void CloseBook(void);
    		void SaveBook(void);
    		int CountSheet(void);
    		AnsiString SelectSheet(int sheet);
    		bool Deprotege(AnsiString motpasse);
    		bool GetCell(int lig,int col,Variant &value);
    		void SetCell(int lig,int col,Variant value);
    		void GetMaxRowCol(int &lig,int &col);
                    __property AnsiString Password = { read = FPassword , write = FPassword };
     
    		TExcel(void);
    		~TExcel(void);
    };
    //---------------------------------------------------------------------------
    #endif

  3. #3
    Rédacteur
    Avatar de blondelle
    Homme Profil pro
    Inscrit en
    Mars 2006
    Messages
    2 738
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 2 738
    Points : 3 766
    Points
    3 766
    Par défaut
    Salut dkjauspin
    Peut etre comme ceci
    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
     
    //---------------------------------------------------------------------------
    #include <vcl.h>
    #pragma hdrstop
    #include <utilcls.h> // <--- ne pas oublier d'inclure
    #include "Unit1.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm1 *Form1;
    Variant vCell, vMSExcel, vXLWorkbooks, vXLWorkbook, vWorksheet;
    Variant vFileName;
    //---------------------------------------------------------------------------
    __fastcall TForm1::TForm1(TComponent* Owner)
            : TForm(Owner)
    {
    }
    //---------------------------------------------------------------------------
    // ne pas oublier d'inclure " #include <utilcls.h> "
    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
    try
     {
        vMSExcel = Variant::GetActiveObject("Excel.Application");
    }
     catch(...)
     {
        vMSExcel = Variant::CreateObject("Excel.Application");
    }
    vMSExcel.OlePropertySet("Visible", true);
    vFileName = "C:\\Users\\home\\Documents\\Pacitel\\Numero_Pacitel.txt";
    vXLWorkbooks = vMSExcel.OlePropertyGet("Workbooks");
    vXLWorkbook = vXLWorkbooks.OleFunction("Open", WideString(vFileName));
    // ici le fichier est charge
    vWorksheet = vXLWorkbook.OlePropertyGet("Worksheets", 1);
    --
    Plutot que d'essayer de réinventer la roue, apprenons à nous en servir

Discussions similaires

  1. [LV 8.6.1] ouvrir un fichier excel existant
    Par Quent' dans le forum LabVIEW
    Réponses: 3
    Dernier message: 23/02/2010, 14h48
  2. Ouvrir un fichier excel existant
    Par Renardo dans le forum Access
    Réponses: 1
    Dernier message: 09/02/2007, 20h07
  3. Ouvrir un fichier excel déja existant
    Par lynal dans le forum Langage
    Réponses: 9
    Dernier message: 24/06/2006, 00h03
  4. Ouvrir un fichier Excel existant
    Par Yanmeunier dans le forum Macros et VBA Excel
    Réponses: 8
    Dernier message: 24/11/2005, 11h17
  5. ouvrir un fichier Excel avec une requete perso
    Par legillou dans le forum Access
    Réponses: 9
    Dernier message: 21/06/2005, 15h14

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