操作Excel的方法

来源:百度文库 编辑:神马文学网 时间:2024/04/28 20:31:32

操作Excel的方法

Excel 2009-01-07 21:22:17 阅读10 评论0   字号: 订阅

 1.添加引用--com---microsoft excel 11.0 object library

  2在需要访问excel的过程中定义

dim exapp as excel.application  '定义excel应用程序
dim exbook as excel.workbook ‘定义工作簿
dim exsheet as excel.worksheet ‘定义工作表
dim exrange as excel.range   '定义工作区域

  3.有了上面的定义,基本上excel的操作就手到擒来了

  exapp.visible=true  '显示excel 程序
  exbook=exapp.workbooks.add()  '添加新工作簿  或exbook=exapp.workbooks.open("薄名")  / 打开已在工作薄
  exsheet=exbook.sheets(n)   '获得第n个工作表的控制句柄,后面就由它处理了
  exsheet.cells(row,col)=值    '对指定单元格赋值,这个操作大量出现哦
  exsheet.range(cells,cells).font       '这个属性也常用到,设置格式就是它了:
  exsheet.range(cells,cells).font.colorindex=3  '设置颜色
exsheet.range(cells,cells).Borders.LineStyle=1'设边框线
exsheet.range(cells,cells).EntireColumn.AutoFit()  '自动列宽  /可以改为自动行高哦
exsheet.range(cells,cells).merge         '合并单元格,复杂的表格这个也用的多哦
exsheet.range(cells,cells).HorizontalAlignment=3  '水平居中  ,当然也可以改为垂直的
exSheet.PageSetup.Orientation = 2   '页面横向

  熟练以上这些,基本上就可以实现一些要求了。

  4。最后是保存文件

  exsheet.saveas("文件名")

  或

  exbook.save()

  5.清理变量

exsheet=nothing
exbook.close
exbook=nothing
exapp.quit
exapp=nothing