打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
如何使用 Jscript 从 HTML 网页自动运行 Excel
查看本文应用于的产品
本页
展开全部 | 关闭全部
概要
本文介绍如何从 HTML 网页创建和操作 Microsoft Office Excel 工作簿。
回到顶端
更多信息
本文演示使 Excel 自动运行的 JScript 代码。 JScript 本身不具有创建 SAFEARRAY 的方法。使用自动运行时,如果调用要求 SAFEARRAY 的函数,则可以使用 VBScript 创建 SAFEARRAY。下面的代码演示此 JScript 和 VBScript 代码组合。
回到顶端
生成自动运行示例
启动记事本。
将下面的代码粘贴到记事本中。<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 = saNamesEnd 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>
将文件保存到选择的目录中并使用文件名 Excelaut.htm。
关闭记事本并启动 Internet Explorer。
在“地址”栏中键入 C:\path\excelaut.htm,其中 path 是在其中保存有该文件的目录。
当 Internet Explorer 加载该文件时,将显示一个句子和一个按钮。按该按钮时,Excel 将在客户端计算机上启动并填充数据。
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
在Asp.net用C#建立动态Excel(外文翻译)
终于找到了这些了,excel珍品集锦
C# 将数据导出到Excel汇总
关于js操作word和excel的若干方法汇总(l来自hcleon的专栏)
extjs grid 导出excel
VB中把datagrid数据保存到excel里
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服