Excel et SaveDialog

Résolu
revsys Messages postés 15 Date d'inscription lundi 13 mars 2006 Statut Membre Dernière intervention 7 juin 2007 - 26 avril 2007 à 11:12
moldov Messages postés 165 Date d'inscription mercredi 8 octobre 2003 Statut Membre Dernière intervention 10 novembre 2008 - 10 nov. 2008 à 15:58
Bonjour,


Je cherche à utiliser un SaveDialog pour permettre à l'utilisatuer de choisir un nom de fichier Excel puis de l'enregistrer où il le souhaite.
Je pensais tout d'abord utiliser la technique OLE qui peut créer un fichier Excel par défaut dans un endroit donné du disque, mais cela ne permet pas de demander le nom du fichier sous lequel l'utilisatuer souhaite l'enregistrer.


Voici le code por créer le fichier Excel avec OLE :


unit UExcel;


interface


uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, OleServer, Excel97, ComObj, StdCtrls;


type
  Texcel = class
 
  procedure Creer(nom : string);
 
  private
    { Déclarations privées }
  public
    { Déclarations publiques }
  end;
 
var
  Excel : Texcel;
  OleApplication : variant;
  OleWorkbook : variant;


implementation


{$R *.dfm}


{********* Crée un fichier excel **********}


procedure Texcel.Creer(nom : string);


        begin
        OleApplication:=CreateOleObject('Excel.Application');
        OleApplication.Visible:=false;
        OleWorkbook:=OleApplication.workBooks.Add;
        OleApplication.ActiveWorkBook.Save;
        OleApplication.Quit;
        OleApplication:=Unassigned;
        end;


end.


Est-il possible d'utiliser un SaveDialog pour réaliser ce que je souhaite ?
Si oui quelqu'un aurait-il un exemple à me proposer ?


Merci d'avance.

5 réponses

moldov Messages postés 165 Date d'inscription mercredi 8 octobre 2003 Statut Membre Dernière intervention 10 novembre 2008
26 avril 2007 à 11:56
Salut,

Voici ce que je te propose,

{********* Crée un fichier excel **********}

procedure Texcel.Creer(nom : string);

        begin
        OleApplication:= CreateOleObject('Excel.Application');
        OleApplication.Visible:=false;
        OleWorkbook:=OleApplication.workBooks.Add;

        if SaveDialog1.Execute then

           if SaveDialog1.Filename <> '' then

            OleApplication.ActiveWorkBook.SaveAs(SaveDialog1.Filename);

  //      OleApplication.ActiveWorkBook.Save;
        OleApplication.Quit;
        OleApplication: =Unassigned;
        end;

end.

Pour info j'ai pas pu tester ce code, car je suis pas sur une machine avec Delphi donc merci de me tenir au courant.
Bonne Prog
3
revsys Messages postés 15 Date d'inscription lundi 13 mars 2006 Statut Membre Dernière intervention 7 juin 2007
26 avril 2007 à 12:19
Merci beaucoup !! Ca marche!!
0
jelume Messages postés 120 Date d'inscription mardi 3 avril 2007 Statut Membre Dernière intervention 15 novembre 2007 1
26 avril 2007 à 21:55
Bsr

A titre d'info, voilà un exemple qui utilise seulement Excel

var
 Wbk: _Workbook;
 Wks: _Worksheet;
 OleTrue, OleFalse, C: OleVariant;
begin
  With CoExcelApplication.Create do
  begin


    DisplayAlerts[GetUserDefaultLCID] := True;


    Wbk := Workbooks.Add(EmptyParam, GetUserDefaultLCID);


    Wks := Wbk.Worksheets.Add(EmptyParam, EmptyParam, EmptyParam, EmptyParam, GetUserDefaultLCID) as  _WorkSheet;


    C := 'B5';
    Wks.Range[C, C].Value := 'Toto';


    OleTrue  := True;
    OleFalse := False;
    Wbk.Close(OleTrue, EmptyParam, OleFalse, GetUserDefaultLCID);


  end;
0
touJava Messages postés 4 Date d'inscription lundi 11 août 2008 Statut Membre Dernière intervention 10 novembre 2008
10 nov. 2008 à 15:27
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="ProgId" content="Word.Document" />
<meta name="Generator" content="Microsoft Word 11" />
<meta name="Originator" content="Microsoft Word 11" />
<link rel="File-List" href="file:///C:%5CDOCUME%7E1%5Cmelki%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml" />
<!--[if gte mso 9]><xml>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:HyphenationZone>21</w:HyphenationZone>
<w:PunctuationKerning/>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
</w:Compatibility>
<w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel>
</w:WordDocument>
</xml><![endif]-->
<!--[if gte mso 9]><xml>
<w:LatentStyles DefLockedState="false" LatentStyleCount="156">
</w:LatentStyles>
</xml><![endif]-->
<style>
<!--
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{mso-style-parent:"";
margin:0cm;
margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:12.0pt;
font-family:"Times New Roman";
mso-fareast-font-family:"Times New Roman";}
a:link, span.MsoHyperlink
{color:blue;
text-decoration:underline;
text-underline:single;}
a:visited, span.MsoHyperlinkFollowed
{color:purple;
text-decoration:underline;
text-underline:single;}
@page Section1
{size:595.3pt 841.9pt;
margin:70.85pt 70.85pt 70.85pt 70.85pt;
mso-header-margin:35.4pt;
mso-footer-margin:35.4pt;
mso-paper-source:0;}
div.Section1
{page:Section1;}
-->
</style>
<!--[if gte mso 10]>
<style>
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:"Tableau Normal";
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.0pt;
font-family:"Times New Roman";
mso-ansi-language:#0400;
mso-fareast-language:#0400;
mso-bidi-language:#0400;}
</style>
<![endif]-->
Bonjour

Je veux faire le même travail que [auteur/REVSYS/718949.aspx revsys.][auteur/REVSYS/718949.aspx ] Par contre moi je voudrais que lorsque la
boite de dialogue TSaveDialog s’ouvre un nom de fichier s’affiche par défaut
dans cette boitte. Si quelqu’un à une aidé Merci.
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
moldov Messages postés 165 Date d'inscription mercredi 8 octobre 2003 Statut Membre Dernière intervention 10 novembre 2008
10 nov. 2008 à 15:58
Salut,
Alors pour forcer un nom de fichier, il faut définir le filtre.

Pour cela une fois que tu as placé ton composant, dans le panneau des propriétés tu as la propriété Filter. Edite la et je te propose un exemple :

- Si tu veux ne faire apparaitre que les fichiers qui se nomment toto.xls alors renseigne "Fichier Toto.xls" dans le champs "Nom du filtre" et "toto.xls" dans "Filtrer"
- Si tu veux ne faire apparaitre que les fichiers qui ont xls comme extension alors renseigne "Fichier Excel" dans le champs "Nom du filtre" et " *.xls " dans "Filtrer"

- Si tu veux ne faire apparaitre tous les fichiers alors renseigne "Tous les fichiers" dans le champs "Nom du filtre" et " *.* " dans "Filtrer"

En espérant avoir été assez clair.
Bonne Prog
0
Rejoignez-nous