文章編號: 234774 - 上次校閱: 2006年2月16日 - 版次: 4.2

如何使用 JScript 從 HTML 網頁自動化 Excel

系統提示本文適用於您使用的作業系統之外的作業系統。與您不相關的文章內容已停用。

在此頁中

全部展開 | 全部摺疊

結論

本文將告訴您,如何從 HTML 網頁建立和管理 Microsoft Excel 活頁簿。

其他相關資訊

本文說明可自動化 Microsoft Excel 的 JScript 程式碼。JScript 沒有建立 SAFEARRAY 的內部工具。使用自動化時,如果您呼叫需要 SAFEARRAY 的函數,就可以使用 VBScript 來建立 SAFEARRAY。下列程式碼示範 JScript 和 VBScript 程式碼的組合。

建立自動化範例

  1. 啟動 [記事本]。
  2. 將下列程式碼貼到 [記事本] 中:
    <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. 以 Excelaut.htm 做為檔名,將檔案儲存至您選擇的目錄。
  4. 關閉 [記事本],並啟動 Internet Explorer。
  5. [網址] 列中,輸入 C:\path\excelaut.htm,其中的 path 就是存放檔案的目錄。
  6. 當 Internet Explorer 載入檔案時,您將會看到一個含有按鈕的句子。當您按下按鈕,將會啟動用戶端電腦上的 Excel 並填入資料。

?考

本文與使用不同語言來說明同一個程序的其他文件相同。如需詳細資訊,請參閱「Microsoft 知識庫」中的下列文件:
219151? (http://support.microsoft.com/kb/219151/ ) 如何從 Visual Basic 將 Microsoft Excel 自動化
179706? (http://support.microsoft.com/kb/179706/ ) How to use MFC to automate Excel and create and format a new workbook
219430? (http://support.microsoft.com/kb/219430/ ) How To Create/Format an Excel Workbook Using Visual J++ Automation
231614? (http://support.microsoft.com/kb/231614/ ) How To Automate Excel to Create a New Workbook from Delphi
如需有關使用 VBScript 從 HTML 網頁自動化 Excel 的詳細資訊,請參閱下列「Microsoft 知識庫」文件:
198703? (http://support.microsoft.com/kb/198703/ ) How To Automating Excel From Client-Side VBScript
如需有關 Office 自動化的詳細資訊,請造訪 Microsoft Office Development 支援網站:
http://support.microsoft.com/ofd (http://support.microsoft.com/ofd)

這篇文章中的資訊適用於:
  • Microsoft Excel 2000 Standard Edition
  • Microsoft Excel 2002 Standard Edition
  • Microsoft Excel 97 Standard Edition
  • Microsoft Internet Explorer 4.0 128-Bit Edition
  • Microsoft Internet Explorer 4.01 Service Pack 2
  • Microsoft Office Excel 2003
關鍵字:?
kbhowto kbautomation KB234774
Microsoft及(或)其供應商不就任何在本伺服器上發表的文字資料及其相關圖表資訊的恰當性作任何承諾。所有文字資料及其相關圖表均以「現狀」供應,不負任何擔保責任。Microsoft及(或)其供應商謹此聲明,不負任何對與此資訊有關之擔保責任,包括關於適售性、適用於某一特定用途、權利或不侵權的明示或默示擔保責任。Microsoft及(或)其供應商無論如何不對因或與使用本伺服器上資訊或與資訊的實行有關而引起的契約、過失或其他侵權行為之訴訟中的特別的、間接的、衍生性的損害或任何因使用而喪失所導致的之損害、資料或利潤負任何責任。