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

ASP.NET Discussion :

bug Documents.Open() et ASPX


Sujet :

ASP.NET

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre actif
    Profil pro
    Développeur informatique
    Inscrit en
    Mai 2006
    Messages
    36
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Mai 2006
    Messages : 36
    Par défaut bug Documents.Open() et ASPX
    Bonjour,

    Avant de poster ce message, j'ai cherché sur le forum (et plus généralement sur tout le net) et n'ait évidemment rien trouvé (enfin rien qui fonctionne.

    J'essaie de créer une page web (ASP avec C# en code-behind sur IIS), qui me permette (pour l'instant) d'ouvrir, modifier, enregistrer puis fermer un document word en local sur le serveur.
    Mes versions sont les suivantes : .Net Framework 1.1 SP1, Office 2003 SP2 avec toutes les mises à jour de sécurité, Windows XP SP2 (pour IIS, celui qui va avec), et je code avec Visual Studio .Net 2003 (SP1) (Mes codes sources sont après)

    Avant d'essayer cette manip', j'ai fait une appli windows classique en C#, qui faisait la même chose, et ça marchait sans problème. (sachant que j'ai fait le test avec Excel et Word)
    Quand j'ai copié le code dans le code-behind de mon appli serveur, il marchait très bien avec Excel, mais pas avec Word : le programme tourne en permanence, en bloquant à la commande Documents.Open(ref "test.doc",...); c'est à dire à l'ouverture du fichier.
    Après plein de tentatives, il semblerait que ce problème vienne des droits de l'utilisateur ASPNET (qui exécute la commande), à qui l'on "interdit" de prendre trop de place en mémoire vive.

    J'ai essayé de trouver où on pouvait changer ces fameux droits, sans succès (ah oui une autre chose : il a déjà fallu que je donne l'accès aux objets COM de mon PC au user "ASPNET" pour interagir avec Office).
    Vous pouvez m'aider?
    Ca serait vachement cool.

    Sur ce, mes sources :
    L'objet du délit :
    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
    using System;
    using Microsoft.Office.Interop.Word;
    using Microsoft.Office.Interop.Excel;
     
    namespace TestsExportsEtConnexionBdD
    {
    	/// <summary>
    	/// Description résumée de exportOffice.
    	/// </summary>
    	public class exportOffice
    	{
    		public exportOffice()
    		{
    			//
    			// TODO : ajoutez ici la logique du constructeur
    			//
    		}
     
    		public System.String genererXL()
    		{
    			// Ouverture du Fichier Excel 
    			Microsoft.Office.Interop.Excel.ApplicationClass oExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass(); 
    			oExcelApp.Visible = false; 
    			Workbooks oBooks = oExcelApp.Workbooks;
    			Workbook oBook = oBooks.Open(@"D:\test.xls", System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value); 
    			Worksheet oSheet = (Worksheet)oBook.Worksheets[1];
     
    			// Nettoyage du Fichier
    			for(int row=8; row<10; row++)
    				for(int col=1; col<6; col++)	oSheet.Cells[row,col] = System.DateTime.Now.ToString(); 
    			for(int row=13; row<53; row++)
    				for(int col=1; col<4; col++)	oSheet.Cells[row,col] = System.DateTime.Now.ToString(); 
     
     
    			// Sauvegarde et Fermeture du Fichier
    			oBook.Save();
    			if (oBook != null) 
    				oBook.Close(true, oBook, null); 
    			if (oSheet != null) 
    				System.Runtime.InteropServices.Marshal.ReleaseComObject(oSheet); 
    			oSheet = null; 
    			if (oBook != null) 
    				System.Runtime.InteropServices.Marshal.ReleaseComObject(oBook); 
    			oBook = null; 
    			if (oBooks != null) 
    				System.Runtime.InteropServices.Marshal.ReleaseComObject(oBooks);	
    			oBooks = null; 
    			if (oExcelApp != null) 
    			{ 
    				oExcelApp.Quit(); 
    				System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcelApp);
    				oExcelApp = null; 
    			}  
    			return ("Export Excel fini à ce moment : \n" + System.DateTime.Now.ToString());
    		}
     
    		public System.String genererWord()
    		{
    			// Objets nécessaires à l'API de MS Word
    			object chemin = @"D:\test.doc";
    			//object oXML = "XML";
    			object oFalse = false;
    			object oTrue = true;
    			object oDynamic = 2;
    			object oUn = 1;
    			object oMax = 65535;
    			object oMissing = System.Reflection.Missing.Value;
    			Document oDocument;
     
    			// Ouverture du logiciel Word
    			Microsoft.Office.Interop.Word.ApplicationClass oWordApp = new Microsoft.Office.Interop.Word.ApplicationClass(); 
    			oWordApp.Visible = false;
    			Documents oDocuments = oWordApp.Documents;
     
    			try
    			{
    				// Ouverture du fichier Word
    				oDocument = oDocuments.Open(ref chemin, ref oMissing, ref oFalse, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    				oDocument.Activate();
     
    				// Ecriture du texte
    				// oWordApp.Selection.MoveDown(ref oMissing, ref oUn, ref oMissing); // Permet de déplacer le curseur en bas (en l'occurence, d'un pas)
    				// oWordApp.Selection.MoveEnd(ref oMissing, ref oUn); // Pas compis à quoi ça sert
    				oWordApp.Selection.MoveDown(ref oMissing, ref oMax, ref oMissing);
    				oWordApp.Selection.TypeText(System.DateTime.Now.ToString());
    				oWordApp.Selection.TypeParagraph();
     
    				// Sauvegarde
    				oDocument.Save();
     
    				// Fermeture du fichier Word
    				if (oDocument != null) 
    					oDocument.Close(ref oFalse, ref oMissing, ref oMissing);
    				if (oDocument != null) 
    					System.Runtime.InteropServices.Marshal.ReleaseComObject(oDocument); 
    				oDocument = null;
     
    			} 
    			catch (Exception e) 
    			{
    				return e.ToString();
    			}
     
    			// Fermeture du logiciel Word
    			if (oDocuments != null) 
    				System.Runtime.InteropServices.Marshal.ReleaseComObject(oDocuments);	
    			oDocuments = null;
    			if (oWordApp != null) 
    			{ 
    				oWordApp.Quit(ref oFalse, ref oMissing, ref oMissing);
    				System.Runtime.InteropServices.Marshal.ReleaseComObject(oWordApp);
    				oWordApp = null; 
    			}
    			return ("Export Word fini à ce moment : \n" + System.DateTime.Now.ToString());
    		}
    	}
    }
    La classe de code-behind qui contient le main (quasi-entièrement générée par visual studio) :
    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
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
     
    namespace TestsExportsEtConnexionBdD
    {
    	/// <summary>
    	/// Description résumée de WebForm1.
    	/// </summary>
    	public class WebForm1 : System.Web.UI.Page
    	{
    		protected System.Web.UI.WebControls.Label Label1;
    		protected System.Web.UI.WebControls.Button excelButton;
    		protected System.Web.UI.WebControls.Button wordButton;
    		private exportOffice eo = new exportOffice();
     
    		private void Page_Load(object sender, System.EventArgs e)
    		{
    			// Placer ici le code utilisateur pour initialiser la page
    		}
     
    		#region Code généré par le Concepteur Web Form
    		override protected void OnInit(EventArgs e)
    		{
    			//
    			// CODEGEN : Cet appel est requis par le Concepteur Web Form ASP.NET.
    			//
    			InitializeComponent();
    			base.OnInit(e);
    		}
     
    		/// <summary>
    		/// Méthode requise pour la prise en charge du concepteur - ne modifiez pas
    		/// le contenu de cette méthode avec l'éditeur de code.
    		/// </summary>
    		private void InitializeComponent()
    		{    
    			this.excelButton.Click += new System.EventHandler(this.excelButton_Click);
    			this.wordButton.Click += new System.EventHandler(this.wordButton_Click);
    			this.Load += new System.EventHandler(this.Page_Load);
     
    		}
    		#endregion
     
    		private void excelButton_Click(object sender, System.EventArgs e)
    		{
    			this.Label1.Text = eo.genererXL();
    		}
     
    		private void wordButton_Click(object sender, System.EventArgs e)
    		{
    			this.Label1.Text = eo.genererWord();
    		}
     
     
    	}
    }
    Et le code de mon appli windows classique, qui marche sans problème :
    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
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using Microsoft.Office.Interop.Excel;
    using Microsoft.Office.Interop.Word;
     
    namespace WindowsApplication2
    {
    	/// <summary>
    	/// Description résumée de Form1.
    	/// </summary>
    	public class Form1 : System.Windows.Forms.Form
    	{
    		private System.Windows.Forms.Button button2;
    		private System.Windows.Forms.Button excel;
    		private System.Windows.Forms.Button word;
    		private System.Windows.Forms.Label label1;
    		/// <summary>
    		/// Variable nécessaire au concepteur.
    		/// </summary>
    		private System.ComponentModel.Container components = null;
     
    		public Form1()
    		{
    			//
    			// Requis pour la prise en charge du Concepteur Windows Forms
    			//
    			InitializeComponent();
     
    			//
    			// TODO : ajoutez le code du constructeur après l'appel à InitializeComponent
    			//
    		}
     
    		/// <summary>
    		/// Nettoyage des ressources utilisées.
    		/// </summary>
    		protected override void Dispose( bool disposing )
    		{
    			if( disposing )
    			{
    				if (components != null) 
    				{
    					components.Dispose();
    				}
    			}
    			base.Dispose( disposing );
    		}
     
    		#region Code généré par le Concepteur Windows Form
    		/// <summary>
    		/// Méthode requise pour la prise en charge du concepteur - ne modifiez pas
    		/// le contenu de cette méthode avec l'éditeur de code.
    		/// </summary>
    		private void InitializeComponent()
    		{
    			this.excel = new System.Windows.Forms.Button();
    			this.button2 = new System.Windows.Forms.Button();
    			this.word = new System.Windows.Forms.Button();
    			this.label1 = new System.Windows.Forms.Label();
    			this.SuspendLayout();
    			// 
    			// excel
    			// 
    			this.excel.Location = new System.Drawing.Point(280, 24);
    			this.excel.Name = "excel";
    			this.excel.TabIndex = 0;
    			this.excel.Text = "excel";
    			this.excel.Click += new System.EventHandler(this.button1_Click);
    			// 
    			// button2
    			// 
    			this.button2.Location = new System.Drawing.Point(392, 88);
    			this.button2.Name = "button2";
    			this.button2.TabIndex = 1;
    			this.button2.Text = "stop";
    			this.button2.Click += new System.EventHandler(this.button2_Click);
    			// 
    			// word
    			// 
    			this.word.Location = new System.Drawing.Point(496, 24);
    			this.word.Name = "word";
    			this.word.TabIndex = 2;
    			this.word.Text = "word";
    			this.word.Click += new System.EventHandler(this.word_Click);
    			// 
    			// label1
    			// 
    			this.label1.Location = new System.Drawing.Point(8, 128);
    			this.label1.Name = "label1";
    			this.label1.Size = new System.Drawing.Size(752, 264);
    			this.label1.TabIndex = 3;
    			// 
    			// Form1
    			// 
    			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    			this.ClientSize = new System.Drawing.Size(768, 397);
    			this.Controls.Add(this.label1);
    			this.Controls.Add(this.word);
    			this.Controls.Add(this.button2);
    			this.Controls.Add(this.excel);
    			this.Name = "Form1";
    			this.Text = "Form1";
    			this.ResumeLayout(false);
     
    		}
    		#endregion
     
    		/// <summary>
    		/// Point d'entrée principal de l'application.
    		/// </summary>
    		[STAThread]
    		static void Main() 
    		{
    			System.Windows.Forms.Application.Run(new Form1());
    		}
     
    		private void button1_Click(object sender, System.EventArgs e)
    		{
    			genererXL();
    		}
     
    		private void genererXL()
    		{
    			// Ouverture du Fichier Excel 
    			Microsoft.Office.Interop.Excel.ApplicationClass oExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass(); 
    			oExcelApp.Visible = false; 
    			Workbooks oBooks = oExcelApp.Workbooks;
    			Workbook oBook = oBooks.Open(@"K:\test.xls", System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value); 
    			Worksheet oSheet = (Worksheet)oBook.Worksheets[1];
     
    			// Nettoyage du Fichier
    			for(int row=8; row<10; row++)
    				for(int col=1; col<6; col++)	oSheet.Cells[row,col] = System.DateTime.Now.ToString(); 
    			for(int row=13; row<53; row++)
    				for(int col=1; col<4; col++)	oSheet.Cells[row,col] = System.DateTime.Now.ToString(); 
     
    			// Remplissage du Fichier
     
    			// Sauvegarde et Fermeture du Fichier
    			oBook.Save();
    			if (oBook != null) 
    				oBook.Close(true, oBook, null); 
    			if (oSheet != null) 
    				System.Runtime.InteropServices.Marshal.ReleaseComObject(oSheet); 
    			oSheet = null; 
    			if (oBook != null) 
    				System.Runtime.InteropServices.Marshal.ReleaseComObject(oBook); 
    			oBook = null; 
    			if (oBooks != null) 
    				System.Runtime.InteropServices.Marshal.ReleaseComObject(oBooks);	
    			oBooks = null; 
    			if (oExcelApp != null) 
    			{ 
    				oExcelApp.Quit(); 
    				System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcelApp);
    				oExcelApp = null; 
    			}  
     
    			// Envoie du fichier
     
    			this.label1.Text = "Export Excel fini à ce moment : \n" + System.DateTime.Now.ToString();
    		}
     
    		private void button2_Click(object sender, System.EventArgs e)
    		{
    			this.Close();
    		}
     
    		private void word_Click(object sender, System.EventArgs e)
    		{
    			genererWord();
    		}
     
    		private void genererWord()
    		{
    			// Objets nécessaires à l'API de MS Word
    			object chemin = "K:\\test.doc";
    			object oFalse = false;
    			object oTrue = true;
    			object oDynamic = 2;
    			object oUn = 1;
    			object oMax = 65535;
    			object oMissing = System.Reflection.Missing.Value;
    			Document oDocument;
     
    			// Ouverture du logiciel Word
    			Microsoft.Office.Interop.Word.ApplicationClass oWordApp = new Microsoft.Office.Interop.Word.ApplicationClass(); 
    			oWordApp.Visible = false;
    			Documents oDocuments = oWordApp.Documents;
     
    			try
    			{
    				// Ouverture du fichier Word
    				oDocument = oDocuments.Open(ref chemin, ref oMissing, ref oFalse, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    				oDocument.Activate();
     
    				// Ecriture du texte
    				// oWordApp.Selection.MoveDown(ref oMissing, ref oUn, ref oMissing); // Permet de déplacer le curseur en bas (en l'occurence, d'un pas)
    				// oWordApp.Selection.MoveEnd(ref oMissing, ref oUn); // Pas compis à quoi ça sert
    				oWordApp.Selection.MoveDown(ref oMissing, ref oMax, ref oMissing);
    				oWordApp.Selection.TypeText(System.DateTime.Now.ToString());
    				oWordApp.Selection.TypeParagraph();
     
    				// Sauvegarde
    				oDocument.Save();
     
    				// Fermeture du fichier Word
    				if (oDocument != null) 
    					oDocument.Close(ref oFalse, ref oMissing, ref oMissing);
    				if (oDocument != null) 
    					System.Runtime.InteropServices.Marshal.ReleaseComObject(oDocument); 
    				oDocument = null;
     
    			} 
    			catch (Exception e) 
    			{
    				this.label1.Text = e.ToString();
    			}
     
    			// Fermeture du logiciel Word
    			if (oDocuments != null) 
    				System.Runtime.InteropServices.Marshal.ReleaseComObject(oDocuments);	
    			oDocuments = null;
    			if (oWordApp != null) 
    			{ 
    				oWordApp.Quit(ref oFalse, ref oMissing, ref oMissing);
    				System.Runtime.InteropServices.Marshal.ReleaseComObject(oWordApp);
    				oWordApp = null; 
    			}
    			this.label1.Text = "Export Word fini à ce moment : \n" + System.DateTime.Now.ToString();
    		}
    	}
    }
    Vous remarquerez que les fichiers de l'appli classique sont sur le lecteur K, contrairement aux fichiers exploités par le code-behind : le lecteur K est un lecteur réseau, du coup les applis normales ont accès directement à mon répertoire d'utilisateur sur ce disque, contrairement aux applis lancées par ASPNET

  2. #2
    Membre très actif
    Inscrit en
    Janvier 2004
    Messages
    208
    Détails du profil
    Informations forums :
    Inscription : Janvier 2004
    Messages : 208
    Par défaut
    c'est normal ton probleme, fait un essais en creant un rep virtuel sur ton lecteur ou partage reso dans la racine de ton application web dans IIS.

    les droits asp.net seront attribuée après redemarrage de ton IIS

    ensuite map tes chemins de fichiers.
    tu as la methode HttpContext.Current.Server.MapPath();
    qui donne le chemin physique des fichiers du repertoire virtuel spécifier dans l'application web.

    puis la methode GetFileName() qui renvois le nom et l'extention du fichier du chemin que tu as spécifier

    System.IO.Path.GetFileName()

    bon courage

  3. #3
    Membre actif
    Profil pro
    Développeur informatique
    Inscrit en
    Mai 2006
    Messages
    36
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Mai 2006
    Messages : 36
    Par défaut
    Merci de ta réponse.
    J'ai testé, le code donne ça :
    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
    public System.String genererXL()
    		{
    			// Ouverture du Fichier Excel 
    			Microsoft.Office.Interop.Excel.ApplicationClass oExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass(); 
    			oExcelApp.Visible = false; 
    			Workbooks oBooks = oExcelApp.Workbooks;
    			String chemin = System.Web.HttpContext.Current.Server.MapPath(@"~/test.xls");
    			//String filename = System.IO.Path.GetFileName(hsu);
    			//String retour = "hsu = " + hsu + "\nfilename = " + filename;
    			//return retour;
    			Workbook oBook = oBooks.Open(chemin, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value); 
    			Worksheet oSheet = (Worksheet)oBook.Worksheets[1];
     
    			// Nettoyage du Fichier
    			for(int row=8; row<10; row++)
    				for(int col=1; col<6; col++)	oSheet.Cells[row,col] = System.DateTime.Now.ToString(); 
    			for(int row=13; row<53; row++)
    				for(int col=1; col<4; col++)	oSheet.Cells[row,col] = System.DateTime.Now.ToString(); 
     
     
    			// Sauvegarde et Fermeture du Fichier
    			oBook.Save();
    			if (oBook != null) 
    				oBook.Close(true, oBook, null); 
    			if (oSheet != null) 
    				System.Runtime.InteropServices.Marshal.ReleaseComObject(oSheet); 
    			oSheet = null; 
    			if (oBook != null) 
    				System.Runtime.InteropServices.Marshal.ReleaseComObject(oBook); 
    			oBook = null; 
    			if (oBooks != null) 
    				System.Runtime.InteropServices.Marshal.ReleaseComObject(oBooks);	
    			oBooks = null; 
    			if (oExcelApp != null) 
    			{ 
    				oExcelApp.Quit(); 
    				System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcelApp);
    				oExcelApp = null; 
    			}  
    			return ("Export Excel fini à ce moment : \n" + System.DateTime.Now.ToString());
    		}
     
    		public System.String genererWord()
    		{
    			// Objets nécessaires à l'API de MS Word
    			//object chemin = @"/test.doc";
    			object chemin = System.Web.HttpContext.Current.Server.MapPath(@"~/test.doc");
    			//object oXML = "XML";
    			object oFalse = false;
    			object oTrue = true;
    			object oDynamic = 2;
    			object oUn = 1;
    			object oMax = 65535;
    			object oMissing = System.Reflection.Missing.Value;
    			Document oDocument;
     
    			// Ouverture du logiciel Word
    			Microsoft.Office.Interop.Word.ApplicationClass oWordApp = new Microsoft.Office.Interop.Word.ApplicationClass(); 
    			oWordApp.Visible = false;
    			Documents oDocuments = oWordApp.Documents;
     
    			try
    			{
    				// Ouverture du fichier Word
    				oDocument = oDocuments.Open(ref chemin, ref oMissing, ref oFalse, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    				oDocument.Activate();
     
    				// Ecriture du texte
    				// oWordApp.Selection.MoveDown(ref oMissing, ref oUn, ref oMissing); // Permet de déplacer le curseur en bas (en l'occurence, d'un pas)
    				// oWordApp.Selection.MoveEnd(ref oMissing, ref oUn); // Pas compis à quoi ça sert
    				oWordApp.Selection.MoveDown(ref oMissing, ref oMax, ref oMissing);
    				oWordApp.Selection.TypeText(System.DateTime.Now.ToString());
    				oWordApp.Selection.TypeParagraph();
     
    				// Sauvegarde
    				oDocument.Save();
     
    				// Fermeture du fichier Word
    				if (oDocument != null) 
    					oDocument.Close(ref oFalse, ref oMissing, ref oMissing);
    				if (oDocument != null) 
    					System.Runtime.InteropServices.Marshal.ReleaseComObject(oDocument); 
    				oDocument = null;
     
    			} 
    			catch (Exception e) 
    			{
    				return e.ToString();
    			}
     
    			// Fermeture du logiciel Word
    			if (oDocuments != null) 
    				System.Runtime.InteropServices.Marshal.ReleaseComObject(oDocuments);	
    			oDocuments = null;
    			if (oWordApp != null) 
    			{ 
    				oWordApp.Quit(ref oFalse, ref oMissing, ref oMissing);
    				System.Runtime.InteropServices.Marshal.ReleaseComObject(oWordApp);
    				oWordApp = null; 
    			}
    			return ("Export Word fini à ce moment : \n" + System.DateTime.Now.ToString());
    		}
    Il a fallut que je copie les fichiers utilisés dans le dossier de mon projet (VS .Net 2003 crée un dossier pour chaque projet d'appli ASP), et j'ai pas eu à me servir de la fonction System.IO.Path.GetFileName() (d'où le commentaire dans le code), et il à aussi fallu que je donne les droits à l'utilisateur ASPNET sur lesdits fichiers (en fait je les lui ai donné sur "C:\Inetpub\wwwroot")mais à part ça Excel fonctionne toujours aussi bien.

    En revanche, la première fois, word a planté en prétextant que l'application demendait trop de mémoire.
    Je vous aurai bien copié le message d'erreur, mais cette erreur ne s'est pas reproduite : depuis, j'ai le même problème qu'au premier post : exécution qui bloque à la ligne :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    oDocument = oDocuments.Open(ref chemin, ref oMissing, ref oFalse, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    Bon, hier j'ai oublié de vous donner cette info : je suis allé voir dans mes journaux d'évennements, et dans ce dernier, j'ai vu que ASPNET n'essayais jamais de lancer le processus WINWORD, mais msiexec. Je sais pas si ça peut vous aider...

  4. #4
    Membre actif
    Profil pro
    Développeur informatique
    Inscrit en
    Mai 2006
    Messages
    36
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Mai 2006
    Messages : 36
    Par défaut
    Bon, je vois que mon poste n'a pas attiré les foules (en même temps, les développements en langage proprio...)
    Bref, merci quand même à Nashouille pour sa réponse. Comme j'ai trouvé une réponse à mon problème, je le tag en résolu et je vous poste ma réponse:
    Comme j'ai Visual Studio .Net 2003, j'ai Crystal Reports.
    Et ce petit gadget permet justement de faire des rapports et autres statistiques sur des bases de données, pour ensuite les exporter au format excel, word ou pdf (c'est inclu dans les librairies).
    Par contre, à l'instar du contrôle WinForms qui permet l'export de manière automatique, le contrôle ASP ne le permet pas : il faut créer un bouton et y injecter son code, qui tiens en 5 lignes, c'est pour ça que je vous le mets dans ce post :
    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
    //Ci-dessous, les contrôles que j'ai mis dans mon page load (ne vous en faites pas, c'est juste pour tester mes COTS)
    private void Page_Load(object sender, System.EventArgs e)
    {
    	// Placer ici le code utilisateur pour initialiser la page
     
    	//Ci-dessous, remplissage à partir de la BdD
    	DataMesProduits = new DataSet2(); // using System.Data
    	oleDbConnection1.Open();
    	oleDbDataAdapter1.Fill(DataMesProduits);
    	oleDbConnection1.Close();
     
    	//Ci-dessous, je joins ma "feuille de style" crystal reports avec ma source de données
    	MonEtat = new EtatTuto();
    	MonEtat.SetDataSource(this.DataMesProduits);
    	this.CrystalReportViewer1.ReportSource = MonEtat;
     
    	// Ci-dessous, libération de la mémoire (vive la mémoire libre!)
    	CrystalReportViewer1.RefreshReport();
    	DataMesProduits.Clear();
    	DataMesProduits.Dispose();
    }
     
    // Et ci-dessous, l'event-handler du clic sur le bouton que j'ai créé :
    private void exportPdf_Click(object sender, System.EventArgs e)
    {
    	MemoryStream oStream; // using System.IO
    	oStream = (MemoryStream)MonEtat.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
    	//oStream = (MemoryStream)MonEtat.ExportToStream(CrystalDecisions.Shared.ExportFormatType.Excel);
    	//oStream = (MemoryStream)MonEtat.ExportToStream(CrystalDecisions.Shared.ExportFormatType.WordForWindows);
    	Response.Clear();
    	Response.Buffer = true;
    	Response.ContentType = "application/pdf";
    	//Response.ContentType = "application/vnd.ms-excel";
    	//Response.ContentType = "application/msword";
    	Response.BinaryWrite(oStream.ToArray());
    	Response.End();
    }
    Comme vous pouvez le constater, ça fait toucher au flux de sortie "à la main". Mais comme c'est facile...

    Ah oui, au fait, j'ai trouvé le début de ma réponse ici :
    http://www.c-sharpcorner.com/UploadF...tInASPNET.aspx

    Finalement, j'ai fait que rajouter la partie sur "comment on encode une réponse en word ou excel?"

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

Discussions similaires

  1. [POO] Document.open dans IE7 ne marche plus ?
    Par jgfa9 dans le forum Général JavaScript
    Réponses: 17
    Dernier message: 30/10/2008, 22h58
  2. [VB.Net 2005]Libérer un document open office
    Par DonF dans le forum Windows Forms
    Réponses: 6
    Dernier message: 15/01/2007, 17h39
  3. [WIN32][D7] documents Open Office vers Txt
    Par Benjamin GAGNEUX dans le forum Delphi
    Réponses: 7
    Dernier message: 31/08/2006, 16h41
  4. Ouvrir un document Open office
    Par sibou51 dans le forum Général Conception Web
    Réponses: 2
    Dernier message: 02/03/2006, 19h55
  5. [OleAutomation] TWordApplication.Documents.Open
    Par SQLpro dans le forum API, COM et SDKs
    Réponses: 2
    Dernier message: 21/10/2003, 14h07

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