Fichier word

Fermé
moiii123 Messages postés 19 Date d'inscription mercredi 24 juin 2015 Statut Membre Dernière intervention 13 avril 2016 - Modifié par moiii123 le 13/04/2016 à 13:21
BloodyAngel Messages postés 1479 Date d'inscription mardi 21 juin 2005 Statut Contributeur Dernière intervention 21 juin 2018 - 22 avril 2016 à 05:20
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.
A voir également:

2 réponses

Whismeril Messages postés 19028 Date d'inscription mardi 11 mars 2003 Statut Non membre Dernière intervention 24 avril 2024 931
13 avril 2016 à 21:47
Bonsoir

pour générer des docx, sans même avoir besoin de word sur l'ordi
https://codes-sources.commentcamarche.net/source/101341-exemple-d-utilisation-de-la-librairie-docx

Pour piloter word (le source date un peu mais en mettant la référence de ta version de word ça marche au moins jusqu'à 2007)
https://codes-sources.commentcamarche.net/source/51010-piloter-word-via-microsoft-office-interop-word
0
BloodyAngel Messages postés 1479 Date d'inscription mardi 21 juin 2005 Statut Contributeur Dernière intervention 21 juin 2018 401
22 avril 2016 à 05:20
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