JavaScript中ActiveXObject对象是启用并返回 Automation 对象的引用。使用方法:
newObj = new ActiveXObject( servername.typename[, location]) ActiveXObject 对象语法有这些部分:其中newObj是必选项。要赋值为 ActiveXObject 的变量名。 servername是必选项。提供该对象的应用程序的名称。 typename是必选项。要创建的对象的类型或类。 location是可选项。创建该对象的网络服务器的名称。
Automation 服务器至少提供一类对象。例如,字处理应用程序可能提供应用程序对象、文档对象和工具栏对象。
要创建 Automation 对象,将新的 ActiveXObject 赋给对象变量:
- var ExcelSheet;
- ExcelApp = new ActiveXObject("Excel.Application");
- ExcelSheet = new ActiveXObject("Excel.Sheet");
本代码启动创建对象的应用程序(在这种情况下,Microsoft Excel 工作表)。一旦对象被创建,就可以用定义的对象变量在代码中引用它。在下面的例子中,通过对象变量 ExcelSheet 访问新对象的属性和方法和其他 Excel 对象,包括 Application 对象和 ActiveSheet.Cells 集合。
-
- ExcelSheet.Application.Visible = true;
-
- ExcelSheet.ActiveSheet.Cells(1,1).Value = "This is column A, row 1";
-
- ExcelSheet.SaveAs("C:\\TEST.XLS");
-
- ExcelSheet.Application.Quit();
只有当 Internet 安全性关闭时才能完成在远程服务器中创建对象。要在远程网络计算机创建对象,可以将该计算机的名称传递给 ActiveXObject 的 servername 参数。该名称与共享名的机器名部分相同。比如共享名为 "\\myserver\public" 的网络,servername 是 "myserver"。另外,可以用 DNS 格式或 IP 地址指定 servername。 下面的代码返回在名为 "myserver" 的远程网络计算机上运行的 Excel 实例的版本号:
- function GetAppVersion()
- {
- var XLApp = new ActiveXObject("Excel.Application", "MyServer");
- return(XLApp.Version);
- }
如果指定的远程服务器不存在或找不到时将发生错误。 使用JavaScript中的ActiveXObject填充并设置Excel格式2006年12月19日 星期二 下午 05:131.创建一个新Excel表格
- var XLObj = new ActiveXObject("Excel.Application");
- var xlBook = XLObj.Workbooks.Add;
- var ExcelSheet = xlBook.Worksheets(1);
- ExcelSheet.SaveAs("C:\\TEST.XLS");
3.使 Excel 通过 Application 对象可见
- ExcelSheet.Application.Visible = true;
- xlBook.PrintOut;
- 或者:
- ExcelSheet.PrintOut;
- xlBook.Close(savechanges=false);
- 或者:
- ExcelSheet.Close(savechanges=false);
- ExcelSheet.Application.Quit();
- 或者:
- XLObj.Quit();
- XLObj=null;
- ExcelSheet.ActiveSheet.PageSetup.LeftMargin= 2/0.035;
- ExcelSheet.ActiveSheet.PageSetup.RightMargin = 3/0.035;
- ExcelSheet.ActiveSheet.PageSetup.TopMargin = 4/0.035;
- ExcelSheet.ActiveSheet.PageSetup.BottomMargin = 5/0.035;
- ExcelSheet.ActiveSheet.PageSetup.HeaderMargin = 1/0.035;
- ExcelSheet.ActiveSheet.PageSetup.FooterMargin = 2/0.035;
- ExcelSheet.ActiveSheet.PageSetup.CenterHeader = "页眉中部内容";
- ExcelSheet.ActiveSheet.PageSetup.LeftHeader = "页眉左部内容";
- ExcelSheet.ActiveSheet.PageSetup.RightHeader = "页眉右部内容";
- ExcelSheet.ActiveSheet.PageSetup.CenterFooter = "页脚中部内容";
- ExcelSheet.ActiveSheet.PageSetup.LeftFooter = "页脚左部内容";
- ExcelSheet.ActiveSheet.PageSetup.RightFooter = "页脚右部内容";
8.对单元格操作,带*部分对于行,列,区域都有相应属性
- ExcelSheet.ActiveSheet.Cells(row,col).Value = "内容";
- ExcelSheet.ActiveSheet.Cells(row,col).Borders.Weight = 1;
- ExcelSheet.ActiveSheet.Cells(row,col).Interior.ColorIndex = 1;
- 2-白色,3-红色,4-绿色,5-蓝色,6-黄色,7-粉红色,8-天蓝色,9-酱土色..可以多做尝试)
- ExcelSheet.ActiveSheet.Cells(row,col).Interior.Pattern = 1;
- 2-细网格,3-粗网格,4-斑点,5-横线,6-竖线..可以多做尝试)
- ExcelSheet.ActiveSheet.Cells(row,col).Font.ColorIndex = 1;
- ExcelSheet.ActiveSheet.Cells(row,col).Font.Size = 10;
- ExcelSheet.ActiveSheet.Cells(row,col).Font.Name = "黑体";
- ExcelSheet.ActiveSheet.Cells(row,col).Font.Italic = true;
- ExcelSheet.ActiveSheet.Cells(row,col).Font.Bold = true;
- ExcelSheet.ActiveSheet.Cells(row,col).ClearContents;
- ExcelSheet.ActiveSheet.Cells(row,col).WrapText=true;
- ExcelSheet.ActiveSheet.Cells(row,col).HorizontalAlignment = 3;
- 2-靠左,3-居中,4-靠右,5-填充 6-两端对齐,7-跨列居中,8-分散对齐)
- ExcelSheet.ActiveSheet.Cells(row,col).VerticalAlignment = 2;
- 2-居中,3-靠下,4-两端对齐,5-分散对齐)
-
- ExcelSheet.ActiveSheet.Rows(row).
- ExcelSheet.ActiveSheet.Columns(col).
- ExcelSheet.ActiveSheet.Rows(startrow+":"+endrow).
- ExcelSheet.ActiveSheet.Columns(startcol+":"+endcol).
-
- XLObj.Range(startcell+":"+endcell).Select;
-
- XLObj.Selection.
-
- XLObj.Range(startcell+":"+endcell).MergeCells = true;
-
- 或者:
- XLObj.Range("A2",XLObj.Cells(8, 8)).MergeCells = true;
9.设置行高与列宽
- ExcelSheet.ActiveSheet.Columns(startcol+":"+endcol).ColumnWidth = 22;
-
- ExcelSheet.ActiveSheet.Rows(startrow+":"+endrow).RowHeight = 22;
-
|