Fichier word

moiii123 Messages postés 19 Statut Membre -  
BloodyAngel Messages postés 1487 Date d'inscription   Statut Contributeur Dernière intervention   -
Bonjour,

Je pose ce message pour avoir un peu d'aide sur du C#.

J'aimerai savoir comment générer des fichiers word notamment depuis un programme.

Par exemple interroger ma base de données et pouvoir intégrer ces résultats directement dans un modèle word prédéfini.

Merci d'avance.

2 réponses

  1. BloodyAngel Messages postés 1487 Date d'inscription   Statut Contributeur Dernière intervention   403
     
    Hello,

    perso je me suis dirigé vers Microsoft.Office.Interop.Word

    Pour te donner une idée très succinte de comment ça fonctionne, voici un exemple :

    object ob = System.Reflection.Missing.Value;
    object filename = @"C:\monfichier.rtf";
    object confirmconversion = false;
    object read_only = false;	
    object ff = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatRTF;	// Format de fichier pour sauvegarde
    object wUnit = Microsoft.Office.Interop.Word.WdUnits.wdLine;		// Unité pour les lignes
    					
    Microsoft.Office.Interop.Word._Application WApp;
    Microsoft.Office.Interop.Word._Document WDoc;
    WApp = new Microsoft.Office.Interop.Word.ApplicationClass();
    WDoc = new Microsoft.Office.Interop.Word.DocumentClass();
    WDoc = WApp.Documents.Add(ref ob, ref ob, ref ob, ref ob); // Création du fichier
    WDoc.SaveAs(ref filename, ref ff, ref ob, ref ob, ref ob, ref ob, ref ob, ref ob, ref ob, ref ob, ref ob, ref ob, ref ob, ref ob, ref ob, ref ob);
    
    						
    // Configuration
    WDoc.PageSetup.BottomMargin = 25;
    WDoc.PageSetup.TopMargin = 25;
    WDoc.PageSetup.LeftMargin = 25;
    WDoc.PageSetup.RightMargin = 25;
    WDoc.PageSetup.HeaderDistance = (float)0.63;
    WDoc.PageSetup.FooterDistance = (float)0.63;
    WApp.Selection.Paragraphs.SpaceAfter = 0;
    WApp.Selection.Paragraphs.SpaceBefore = 0;
    WApp.Selection.ParagraphFormat.SpaceAfter = 0;
    WApp.Selection.ParagraphFormat.SpaceBefore = 0;
    WApp.Selection.ParagraphFormat.LineSpacing = (float)11.05;
    WApp.Selection.ParagraphFormat.LineSpacingRule = WdLineSpacing.wdLineSpaceExactly;
    WApp.Selection.Font.Name = "Courier New";
    WApp.Selection.Font.Size = 8;
    WApp.Selection.Font.Underline = WdUnderline.wdUnderlineSingle;
    WApp.Selection.TypeText(intro);
    WApp.Selection.Font.Underline = WdUnderline.wdUnderlineNone;
    WApp.Selection.TypeText("Ce que je veux écrire") + Environment.NewLine);  // Écriture dans le fichier
    WDoc.Save();
    WApp.Visible = true;		// Affichage du fichier à l'écran	


    Voilà c'est juste pour que tu aies un aperçu d'à quoi ça ressemble.

    Et puis pour le reste tu as la doc : https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.interop.word?redirectedfrom=MSDN&view=word-pia

    Tchuss
    0