js操作excel

来源:岁月联盟 编辑:exp 时间:2012-05-04

js 调用ActiveXObject 总结 收藏

Automation 服务器至少提供一类对象。例如,字处理应用程序可能提供应用程序对象、文档对象和工具栏对象。

 

  要创建 Automation 对象,将新的 ActiveXObject 赋给对象变量:

 

view plaincopy to clipboardprint?

var ExcelSheet;   

ExcelApp = newActiveXObject("Excel.Application");   

ExcelSheet = newActiveXObject("Excel.Sheet");  

var ExcelSheet;

ExcelApp = newActiveXObject("Excel.Application");

ExcelSheet = newActiveXObject("Excel.Sheet"); 

 

  本代码启动创建对象的应用程序(在这种情况下,Microsoft Excel 工作表)。一旦对象被创建,就可以用定义的对象变量在代码中引用它。在下面的例子中,通过对象变量 ExcelSheet 访问新对象的属性和方法和其他Excel 对象,包括 Application 对象和 ActiveSheet.Cells 集合。

 

view plaincopy to clipboardprint?

// 使 Excel 通过 Application 对象可见。 

 ExcelSheet.Application.Visible = true; 

  // 将一些文本放置到表格的第一格中。 

 ExcelSheet.ActiveSheet.Cells(1,1).Value = "This is column A, row1"; 

  // 保存表格。 

 ExcelSheet.SaveAs("C://TEST.XLS"); 

  // 用 Application 对象用 Quit 方法关闭 Excel。 

 ExcelSheet.Application.Quit();

  // 使 Excel 通过 Application 对象可见。

   ExcelSheet.Application.Visible = true;

   // 将一些文本放置到表格的第一格中。

    ExcelSheet.ActiveSheet.Cells(1,1).Value ="This is column A, row 1";

   // 保存表格。

   ExcelSheet.SaveAs("C://TEST.XLS");

   // 用 Application 对象用 Quit 方法关闭 Excel。

   ExcelSheet.Application.Quit();   

 

只有当 Internet 安全性关闭时才能完成在远程服务器中创建对象。要在远程网络计算机创建对象,可以将该计算机的名称传递给ActiveXObject 的 servername 参数。该名称与共享名的机器名部分相同。比如共享名为"//myserver/public" 的网络,servername 是"myserver"。另外,可以用 DNS 格式或 IP 地址指定servername。

 

  下面的代码返回在名为"myserver" 的远程网络计算机上运行的Excel 实例的版本号:

 

view plaincopy to clipboardprint?

function GetAppVersion() 

   var XLApp = new ActiveXObject("Excel.Application","MyServer"); 

   return(XLApp.Version); 

}

function GetAppVersion()

{

   var XLApp = new ActiveXObject("Excel.Application","MyServer");

   return(XLApp.Version);

}

 

如果指定的远程服务器不存在或找不到时将发生错误。

使用JavaScript中的ActiveXObject填充并设置Excel格式2006年12月19日 星期二 下午 05:131.创建一个新Excel表格

 

view plaincopy to clipboardprint?

var XLObj = newActiveXObject("Excel.Application"); 

  var xlBook = XLObj.Workbooks.Add;                         //新增工作簿 

   varExcelSheet = xlBook.Worksheets(1);                   //创建工作表

 varXLObj = new ActiveXObject("Excel.Application");

   var xlBook = XLObj.Workbooks.Add;                         //新增工作簿

   var ExcelSheet = xlBook.Worksheets(1);                   //创建工作表

 

2.保存表格

 

view plaincopy to clipboardprint?

ExcelSheet.SaveAs("C://TEST.XLS");

 ExcelSheet.SaveAs("C://TEST.XLS");

 

3.使 Excel 通过 Application 对象可见

 

view plaincopy to clipboardprint?

ExcelSheet.Application.Visible = true;

 ExcelSheet.Application.Visible = true;

 

4.打印

 

view plaincopy to clipboardprint?

xlBook.PrintOut; 

   或者: 

  ExcelSheet.PrintOut;

 xlBook.PrintOut;

    或者:

   ExcelSheet.PrintOut;

 

5.关闭

 

view plaincopy to clipboardprint?

xlBook.Close(savechanges=false); 

   或者: 

  ExcelSheet.Close(savechanges=false);

 xlBook.Close(savechanges=false);

    或者:

   ExcelSheet.Close(savechanges=false);

 

6.结束进程

 

view plaincopy to clipboardprint?

ExcelSheet.Application.Quit(); 

    或者: 

   XLObj.Quit(); 

   XLObj=null;

ExcelSheet.Application.Quit();

    或者:

   XLObj.Quit();

   XLObj=null;

 

7.页面设置 www.2cto.com

 

view plaincopy to clipboardprint?

ExcelSheet.ActiveSheet.PageSetup.LeftMargin=2/0.035;         //页边距左2厘米 

  ExcelSheet.ActiveSheet.PageSetup.RightMargin = 3/0.035;      //页边距 右3厘米, 

  ExcelSheet.ActiveSheet.PageSetup.TopMargin = 4/0.035;        //页边距 上4厘米, 

  ExcelSheet.ActiveSheet.PageSetup.BottomMargin = 5/0.035;   //页边距 下5厘米 

  ExcelSheet.ActiveSheet.PageSetup.HeaderMargin = 1/0.035;   //页边距 页眉1厘米 

  ExcelSheet.ActiveSheet.PageSetup.FooterMargin = 2/0.035;    //页边距 页脚2厘米 

  ExcelSheet.ActiveSheet.PageSetup.CenterHeader = "页眉中部内容"; 

  ExcelSheet.ActiveSheet.PageSetup.LeftHeader = "页眉左部内容"; 

  ExcelSheet.ActiveSheet.PageSetup.RightHeader = "页眉右部内容"; 

  ExcelSheet.ActiveSheet.PageSetup.CenterFooter = "页脚中部内容"; 

  ExcelSheet.ActiveSheet.PageSetup.LeftFooter = "页脚左部内容"; 

  ExcelSheet.ActiveSheet.PageSetup.RightFooter = "页脚右部内容";

 ExcelSheet.ActiveSheet.PageSetup.LeftMargin=2/0.035;         //页边距左2厘米

    ExcelSheet.ActiveSheet.PageSetup.RightMargin= 3/0.035;      //页边距右3厘米,

   ExcelSheet.ActiveSheet.PageSetup.TopMargin = 4/0.035;        //页边距 上4厘米,

   ExcelSheet.ActiveSheet.PageSetup.BottomMargin = 5/0.035;   //页边距 下5厘米

   ExcelSheet.ActiveSheet.PageSetup.HeaderMargin = 1/0.035;   //页边距 页眉1厘米

   ExcelSheet.ActiveSheet.PageSetup.FooterMargin = 2/0.035;    //页边距 页脚2厘米

   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.对单元格操作,带*部分对于行,列,区域都有相应属性

 

view plaincopy to clipboardprint?

ExcelSheet.ActiveSheet.Cells(row,col).Value= "内容";                //设置单元格内容 

  ExcelSheet.ActiveSheet.Cells(row,col).Borders.Weight = 1;        //设置单元格边框*() 

  ExcelSheet.ActiveSheet.Cells(row,col).Interior.ColorIndex = 1;    //设置单元格底色*(1-黑色, 

      2-白色,3-红色,4-绿色,5-蓝色,6-黄色,7-粉红色,8-天蓝色,9-酱土色..可以多做尝试) 

  ExcelSheet.ActiveSheet.Cells(row,col).Interior.Pattern = 1;         //设置单元格背景样式*(1-无, 

                                                    2-细网格,3-粗网格,4-斑点,5-横线,6-竖线..可以多做尝试) 

  ExcelSheet.ActiveSheet.Cells(row,col).Font.ColorIndex = 1;        //设置字体颜色*(与上相同) 

  ExcelSheet.ActiveSheet.Cells(row,col).Font.Size = 10;                //设置为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;  //水平对齐方式枚举* (1-常规, 

                                   2-靠左,3-居中,4-靠右,5-填充 6-两端对齐,7-跨列居中,8-分散对齐) 

  ExcelSheet.ActiveSheet.Cells(row,col).VerticalAlignment = 2;      //垂直对齐方式枚举*(1-靠上, 

                                                                                 2-居中,3-靠下,4-两端对齐,5-分散对齐) 

   //行,列有相应操作: 

   ExcelSheet.ActiveSheet.Rows(row). 

  ExcelSheet.ActiveSheet.Columns(col). 

  ExcelSheet.ActiveSheet.Rows(startrow+":"+endrow).                   //如Rows("1:5")即1到5行 

  ExcelSheet.ActiveSheet.Columns(startcol+":"+endcol).                //如Columns("1:5")即1到5列 

   //区域有相应操作: 

  XLObj.Range(startcell+":"+endcell).Select; 

      //如Range("A2:H8")即A列第2格至H列第8格的整个区域 

  XLObj.Selection. 

   //合并单元格 

  XLObj.Range(startcell+":"+endcell).MergeCells = true; 

      //如Range("A2:H8")即将A列第2格至H列第8格的整个区域合并为一个单元格 

   或者: 

  XLObj.Range("A2",XLObj.Cells(8, 8)).MergeCells = true;

 ExcelSheet.ActiveSheet.Cells(row,col).Value ="内容";                //设置单元格内容

   ExcelSheet.ActiveSheet.Cells(row,col).Borders.Weight = 1;        //设置单元格边框*()

    ExcelSheet.ActiveSheet.Cells(row,col).Interior.ColorIndex= 1;    //设置单元格底色*(1-黑色,

       2-白色,3-红色,4-绿色,5-蓝色,6-黄色,7-粉红色,8-天蓝色,9-酱土色..可以多做尝试)

   ExcelSheet.ActiveSheet.Cells(row,col).Interior.Pattern = 1;         //设置单元格背景样式*(1-无,

                                                     2-细网格,3-粗网格,4-斑点,5-横线,6-竖线..可以多做尝试)

   ExcelSheet.ActiveSheet.Cells(row,col).Font.ColorIndex = 1;        //设置字体颜色*(与上相同)

   ExcelSheet.ActiveSheet.Cells(row,col).Font.Size = 10;                //设置为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;  //水平对齐方式枚举* (1-常规,

                                    2-靠左,3-居中,4-靠右,5-填充 6-两端对齐,7-跨列居中,8-分散对齐)

   ExcelSheet.ActiveSheet.Cells(row,col).VerticalAlignment = 2;      //垂直对齐方式枚举*(1-靠上,

                                                                                  2-居中,3-靠下,4-两端对齐,5-分散对齐)

   //行,列有相应操作:

   ExcelSheet.ActiveSheet.Rows(row).

   ExcelSheet.ActiveSheet.Columns(col).

   ExcelSheet.ActiveSheet.Rows(startrow+":"+endrow).                   //如Rows("1:5")即1到5行

   ExcelSheet.ActiveSheet.Columns(startcol+":"+endcol).                //如Columns("1:5")即1到5列

   //区域有相应操作:

   XLObj.Range(startcell+":"+endcell).Select;

       //如Range("A2:H8")即A列第2格至H列第8格的整个区域

   XLObj.Selection.

   //合并单元格

   XLObj.Range(startcell+":"+endcell).MergeCells = true;

       //如Range("A2:H8")即将A列第2格至H列第8格的整个区域合并为一个单元格

    或者:

   XLObj.Range("A2",XLObj.Cells(8, 8)).MergeCells = true;

 

9.设置行高与列宽

 

view plaincopy to clipboardprint?

ExcelSheet.ActiveSheet.Columns(startcol+":"+endcol).ColumnWidth= 22; 

       //设置从firstcol到stopcol列的宽度为22 

   ExcelSheet.ActiveSheet.Rows(startrow+":"+endrow).RowHeight =22; 

       //设置从firstrow到stoprow行的宽度为22




摘自 songmin3121的专栏