다음을 통해 공유


JScript를 사용하여 HTML 웹 페이지에서 Excel을 자동화하는 방법

요약

이 문서에서는 HTML 페이지에서 Microsoft Office Excel 통합 문서를 만들고 조작하는 방법을 보여 줍니다.

추가 정보

이 문서에서는 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 입력합니다. 여기서 경로 는 파일을 저장한 디렉터리입니다.

  6. Internet Explorer에서 파일을 로드하면 단추가 있는 문장 하나가 표시됩니다. 단추를 누르면 클라이언트 컴퓨터에서 Excel이 시작되고 데이터로 채워지게 됩니다.

참조

이 문서에서는 다른 언어를 사용하여 동일한 프로세스를 설명하는 다른 문서와 유사합니다. 자세한 내용은 다음 문서 번호를 클릭하여 Microsoft 기술 자료의 문서를 확인합니다.

Visual Basic에서 Microsoft Excel을 자동화하는 방법 219151

179706 MFC를 사용하여 Excel을 자동화하고 새 통합 문서를 만들고 서식을 지정하는 방법

HTML 페이지에서 Excel을 자동화하는 방법에 대한 자세한 내용은 다음 문서 번호를 클릭하여 Microsoft 기술 자료의 문서를 봅니다.

198703 클라이언트 쪽 VBScript에서 Excel을 자동화하는 방법

Office Automation에 대한 자세한 내용은 다음 Microsoft Office 개발 지원 웹 사이트를 참조하세요. Microsoft 지원