Numéro d'article: 234774 - Dernière mise à jour: vendredi 11 mai 2007 - Version: 5.2

Comment automatiser Excel à partir d'une page Web HTML à l'aide de JScript

A noterCet article s'applique à un système d'exploitation différent de celui que vous utilisez. Le contenu de l'article qui ne vous concerne peut-être pas est désactivé.

Sommaire

Agrandir tout | Réduire tout

Résumé

Cet article explique comment créer et manipuler un classeur Microsoft Excel à partir une page HTML.

Plus d'informations

Cet article illustre le code JScript qui automatise Excel. JScript n'a aucun moyen interne de création SAFEARRAYs. Lorsque vous utilisez automation, si vous appelez une fonction qui requiert un SAFEARRAY, vous pouvez utiliser VBScript pour créer le SAFEARRAY. Le code ci-dessous illustre cette combinaison de code JScript et VBScript.

Création de l'exemple d'automatisation

  1. Démarrez le Bloc-notes.
  2. Collez le code suivant dans le bloc-notes.
    <HTML>
    <BODY>
    Press the button to start Excel and display quarterly data.
    <SCRIPT LANGUAGE="VBScript">
    Function CreateNamesArray()
    ' Create an array to set multiple values at once.
      Dim saNames(5, 2)
      saNames(0, 0) = "John"
    
      saNames(0, 1) = "Smith"
      saNames(1, 0) = "Tom"
      saNames(1, 1) = "Brown"
      saNames(2, 0) = "Sue"
      saNames(2, 1) = "Thomas"
      saNames(3, 0) = "Jane"
      saNames(3, 1) = "Jones"
      saNames(4, 0) = "Adam"
      saNames(4, 1) = "Johnson"
      CreateNamesArray = saNames
    End Function
    </SCRIPT>
    
    <SCRIPT LANGUAGE="JScript"> 
    function AutomateExcel()
    {
    
       // Start Excel and get Application object.
          var oXL = new ActiveXObject("Excel.Application");
           
          oXL.Visible = true;
          
       // Get a new workbook.
          var oWB = oXL.Workbooks.Add();
          var oSheet = oWB.ActiveSheet;
             
       // Add table headers going cell by cell.
          oSheet.Cells(1, 1).Value = "First Name";
          oSheet.Cells(1, 2).Value = "Last Name";
          oSheet.Cells(1, 3).Value = "Full Name";
          oSheet.Cells(1, 4).Value = "Salary";
             
       // Format A1:D1 as bold, vertical alignment = center.
          oSheet.Range("A1", "D1").Font.Bold = true;
          oSheet.Range("A1", "D1").VerticalAlignment =  -4108; //xlVAlignCenter
             
       // Create an array to set multiple values at once.
    
       // Fill A2:B6 with an array of values (from VBScript).
          oSheet.Range("A2", "B6").Value = CreateNamesArray();
         
       // Fill C2:C6 with a relative formula (=A2 & " " & B2).
          var oRng = oSheet.Range("C2", "C6");
          oRng.Formula = "=A2 & \" \" & B2";
             
       // Fill D2:D6 with a formula(=RAND()*100000) and apply format.
          oRng = oSheet.Range("D2", "D6");
          oRng.Formula = "=RAND()*100000";
          oRng.NumberFormat = "$0.00";
             
       // AutoFit columns A:D.
          oRng = oSheet.Range("A1", "D1");
          oRng.EntireColumn.AutoFit();
             
       // Manipulate a variable number of columns for Quarterly Sales Data.
          DispalyQuarterlySales(oSheet);
            
       // Make sure Excel is visible and give the user control
       // of Excel's lifetime.
          oXL.Visible = true;
          oXL.UserControl = true;
    }
          
    function DispalyQuarterlySales(oWS)
    {
          var iNumQtrs, sMsg, iRet;
    
        // Number of quarters to display data for.
          iNumQtrs = 4;
                   
        // Starting at E1, fill headers for the number of columns selected.
          var oResizeRange = oWS.Range("E1", "E1").Resize(1,iNumQtrs);
          oResizeRange.Formula = "=\"Q\" & COLUMN()-4 & CHAR(10) & \"Sales\"";
             
        // Change the Orientation and WrapText properties for the headers.
          oResizeRange.Orientation = 38;
          oResizeRange.WrapText = true;
             
        // Fill the interior color of the headers.
          oResizeRange.Interior.ColorIndex = 36;
             
        // Fill the columns with a formula and apply a number format.
          oResizeRange = oWS.Range("E2", "E6").Resize(5,iNumQtrs);
          oResizeRange.Formula = "=RAND()*100";
          oResizeRange.NumberFormat = "$0.00";
             
        // Apply borders to the Sales data and headers.
          oResizeRange = oWS.Range("E1", "E6").Resize(6,iNumQtrs);
          oResizeRange.Borders.Weight = 2;  // xlThin
             
        // Add a Totals formula for the sales data and apply a border.
          oResizeRange = oWS.Range("E8", "E8").Resize(1,iNumQtrs);
          oResizeRange.Formula = "=SUM(E2:E6)";
        // 9 = xlEdgeBottom      
          oResizeRange.Borders(9).LineStyle = -4119; //xlDouble
          oResizeRange.Borders(9).Weight = 4; //xlThick
             
        // Add a Chart for the selected data.
    
          oResizeRange = oWS.Range("E2:E6").Resize(5,iNumQtrs);
          var oChart = oWS.Parent.Charts.Add();
          oChart.ChartWizard(oResizeRange, -4100, null, 2);  // -4100 = xl3dColumn
          oChart.SeriesCollection(1).XValues = oWS.Range("A2", "A6");
          for (iRet = 1; iRet <= iNumQtrs; iRet++) {
             oChart.SeriesCollection(iRet).Name = "=\"Q" + iRet + "\"";
          }
          oChart.Location(2, oWS.Name); // 2 = xlLocationAsObject
             
        // Move the chart so as not to cover your data.
          oWS.Shapes("Chart 1").Top = oWS.Rows(10).Top;
          oWS.Shapes("Chart 1").Left = oWS.Columns(2).Left;
    }
    </SCRIPT>
    <P><INPUT id=button1 type=button value="Start Excel" 
              onclick="AutomateExcel"></P>
    </BODY>
    </HTML>
    					
  3. Enregistrez le fichier dans un répertoire de votre choix avec le nom de fichier Excelaut.htm.
  4. Fermez le Bloc-notes et démarrez Internet Explorer.
  5. Dans la barre d'adresses , tapez C:\ path \excelaut.htm , où path est le répertoire vous enregistré le fichier en cours.
  6. Lorsque Internet Explorer charge le fichier, vous voyez une phrase à un bouton. Lorsque vous appuyez sur le bouton, Excel démarre sur ordinateur le client et remplir avec des données.

Références

Cet article parallels autres articles qui décrivent le même processus utilisant différentes langues. Pour plus d'informations, cliquez sur les numéros ci-dessous pour afficher les articles correspondants dans la Base de connaissances Microsoft :
219151  (http://support.microsoft.com/kb/219151/ ) Comment faire pour automatiser Microsoft Excel à partir de Visual Basic
179706  (http://support.microsoft.com/kb/179706/ ) Comment faire pour utiliser MFC pour automatiser Excel et créer et formater un nouveau classeur
219430  (http://support.microsoft.com/kb/219430/ ) Comment faire pour créer/format un classeur Excel en utilisant Automation Visual J ++
231614  (http://support.microsoft.com/kb/231614/ ) Comment faire pour automatiser Excel pour créer un nouveau classeur à partir de Delphi
Pour plus d'informations sur la façon d'automatiser Excel à partir d'une page HTML, cliquez sur le numéro ci-dessous pour afficher l'article correspondant dans la Base de connaissances Microsoft :
198703  (http://support.microsoft.com/kb/198703/ ) Comment faire pour automatiser Excel à partir d'un VBScript côté client
Reportez-vous pour plus d'informations sur l'automation Office, au développement de Microsoft Office prise en charge Adresse suivante :
http://support.microsoft.com/ofd (http://support.microsoft.com/ofd)

Les informations contenues dans cet article s'appliquent au(x) produit(s) suivant(s):
  • Microsoft Office Excel 2007
  • Microsoft Office Excel 2003
  • Microsoft Excel 2002
  • Microsoft Excel 2000 Standard
  • Microsoft Excel 97 Standard
  • Microsoft Internet Explorer 4.0 Édition 128 bits
  • Microsoft Internet Explorer 4.01 Service Pack 2
Mots-clés : 
kbmt kbautomation kbhowto KB234774 KbMtfr
Traduction automatiqueTraduction automatique
IMPORTANT : Cet article est issu du système de traduction automatique mis au point par Microsoft (http://support.microsoft.com/gp/mtdetails). Un certain nombre d?articles obtenus par traduction automatique sont en effet mis à votre disposition en complément des articles traduits en langue française par des traducteurs professionnels. Cela vous permet d?avoir accès, dans votre propre langue, à l?ensemble des articles de la base de connaissances rédigés originellement en langue anglaise. Les articles traduits automatiquement ne sont pas toujours parfaits et peuvent comporter des erreurs de vocabulaire, de syntaxe ou de grammaire (probablement semblables aux erreurs que ferait une personne étrangère s?exprimant dans votre langue !). Néanmoins, mis à part ces imperfections, ces articles devraient suffire à vous orienter et à vous aider à résoudre votre problème. Microsoft s?efforce aussi continuellement de faire évoluer son système de traduction automatique.
La version anglaise de cet article est la suivante: 234774  (http://support.microsoft.com/kb/234774/en-us/ )
L'INFORMATION CONTENUE DANS CE DOCUMENT EST FOURNIE PAR MICROSOFT SANS GARANTIE D'AUCUNE SORTE, EXPLICITE OU IMPLICITE. L'UTILISATEUR ASSUME LE RISQUE DE L'UTILISATION DU CONTENU DE CE DOCUMENT. CE DOCUMENT NE PEUT ETRE REVENDU OU CEDE EN ECHANGE D'UN QUELCONQUE PROFIT.