VBS操作Excel常见方法
dim oexcel,owb,osheet
set oexcel= createobject("excel.application")
set owb = oexcel.workbooks.open("e:\其他\新装电话表.xls")
set osheet = owb.sheets("sheet1")
msgbox osheet.range("b2").value '#提取单元格b2内容
'.....
3、如果是xp系统,可以使用如下代码
dim objfiledlg
set objfiledlg = createobject("useraccounts.commondialog")
objfiledlg.filter = "excel file (*.xls) |*.xls"
if objfiledlg.showopen then
msgbox "您选择的文件是:" & objfiledlg.filename & vbcrlf
end if
vbs控制excel的一些常见方法:
(一) 使用动态创建的方法
首先创建 excel 对象,使用comobj:
oexcel = createobject( "excel.application" )
1) 显示当前窗口:
oexcel.visible = true
2) 更改 excel 标题栏:
oexcel.caption = "应用程序调用 microsoft excel"
3) 添加新工作簿:
oexcel.workbooks.add
4) 打开已存在的工作簿:
oexcel.workbooks.open( "c:\excel\demo.xls" )
5) 设置第2个工作表为活动工作表:
oexcel.worksheets(2).activate
或
oexcel.workssheets( "sheet2" ).activate
6) 给单元格赋值:
oexcel.cells(1,4).value = "第一行第四列"
7) 设置指定列的宽度(单位:字符个数),以第一列为例:
oexcel.activesheet.columns(1).columnswidth = 5
8) 设置指定行的高度(单位:磅)(1磅=0.035厘米),以第二行为例:
oexcel.activesheet.rows(2).rowheight = 1/0.035 ' 1厘米
9) 在第8行之前插入分页符:
oexcel.worksheets(1).rows(8).pagebreak = 1
10) 在第8列之前删除分页符:
oexcel.activesheet.columns(4).pagebreak = 0
11) 指定边框线宽度:
oexcel.activesheet.range( "b3:d4" ).borders(2).weight = 3
1-左 2-右 3-顶 4-底 5-斜( \ ) 6-斜( / )
12) 清除第一行第四列单元格公式:
oexcel.activesheet.cells(1,4).clearcontents
13) 设置第一行字体属性:
oexcel.activesheet.rows(1).font.name = "隶书"
oexcel.activesheet.rows(1).font.color = clblue
oexcel.activesheet.rows(1).font.bold = true
oexcel.activesheet.rows(1).font.underline = true
14) 进行页面设置:
a.页眉:
oexcel.activesheet.pagesetup.centerheader = "报表演示"
b.页脚:
oexcel.activesheet.pagesetup.centerfooter = "第&p页"
c.页眉到顶端边距2cm:
oexcel.activesheet.pagesetup.headermargin = 2/0.035
d.页脚到底端边距3cm:
oexcel.activesheet.pagesetup.headermargin = 3/0.035
e.顶边距2cm:
oexcel.activesheet.pagesetup.topmargin = 2/0.035
f.底边距2cm:
oexcel.activesheet.pagesetup.bottommargin = 2/0.035
g.左边距2cm:
oexcel.activesheet.pagesetup.leftmargin = 2/0.035
h.右边距2cm:
oexcel.activesheet.pagesetup.rightmargin = 2/0.035
i.页面水平居中:
oexcel.activesheet.pagesetup.centerhorizontally = 2/0.035
j.页面垂直居中:
oexcel.activesheet.pagesetup.centervertically = 2/0.035
k.打印单元格网线:
oexcel.activesheet.pagesetup.printgridlines = true
15) 拷贝操作:
a.拷贝整个工作表:
oexcel.activesheet.used.range.copy
b.拷贝指定区域:
oexcel.activesheet.range( "a1:e2" ).copy
c.从a1位置开始粘贴:
oexcel.activesheet.range.( "a1" ).pastespecial
d.从文件尾部开始粘贴:
oexcel.activesheet.range.pastespecial
16) 插入一行或一列:
a. oexcel.activesheet.rows(2).insert
b. oexcel.activesheet.columns(1).insert
17) 删除一行或一列:
a. oexcel.activesheet.rows(2).delete
b. oexcel.activesheet.columns(1).delete
18) 打印预览工作表:
oexcel.activesheet.printpreview
19) 打印输出工作表:
oexcel.activesheet.printout
20) 工作表保存:
if not oexcel.activeworkbook.saved then
oexcel.activesheet.printpreview
21) 工作表另存为:
oexcel.saveas( "c:\excel\demo1.xls" )
22) 放弃存盘:
oexcel.activeworkbook.saved = true
23) 关闭工作簿:
oexcel.workbooks.close
24) 退出 excel:
oexcel.quit
(二) 使用vbs 控制excle二维图
1)选择当第一个工作薄第一个工作表
set osheet=oexcel.workbooks(1).worksheets(1)
2)增加一个二维图
achart=osheet.chartobjects.add(100,100,200,200)
3)选择二维图的形态
achart.chart.charttype=4
4)给二维图赋值
set series=achart.chart.seriescollection
range="sheet1!r2c3:r3c9"
series.add range,true
5)加上二维图的标题
achart.chart.hastitle=true
achart.chart.charttitle.characters.text=" excle二维图"
6)改变二维图的标题字体大小
achart.chart.charttitle.font.size=18
7)给二维图加下标说明
achart.chart.axes(xlcategory, xlprimary).hastitle = true
achart.chart.axes(xlcategory, xlprimary).axistitle.characters.text = "下标说明"
8)给二维图加左标说明
achart.chart.axes(xlvalue, xlprimary).hastitle = true
achart.chart.axes(xlvalue, xlprimary).axistitle.characters.text = "左标说明"
9)给二维图加右标说明
achart.chart.axes(xlvalue, xlsecondary).hastitle = true
achart.chart.axes(xlvalue, xlsecondary).axistitle.characters.text = "右标说明"
10)改变二维图的显示区大小
achart.chart.plotarea.left = 5
achart.chart.plotarea.width = 223
achart.chart.plotarea.height = 108
如何用vbs把excel的单元格数据写到txt
if wscript.arguments.count > 0 then filename = wscript.arguments(0)
set a = createobject("excel.application")
if filename = "" then
filename = a.getopenfilename("excel files (*.xls), *.xls")
if vartype(filename) = vbboolean then
msgbox "excel2txt用于将excel文件的每个sheet保存为一个文本文件。" & vbcr & vblf & vbcr & vblf & "用法: excel2txt filename.xls 或在对话框中打开excel文件。"
wscript.quit
end if
end if
set w = a.workbooks.open(filename)
n = replace(replace(w.name, ".xls", ""), ".xls", "")
a.displayalerts = false
for each s in w.sheets
s.saveas w.path & "\" & n & "_" & s.name & ".txt", 20
next
a.quit
把以上代码存为excel2txt.vbs双击执行就行了
vbs操作excel
set objexcel = createobject("excel.application") '建一个exel对象
set objworkbook = objexcel.workbooks.open _
("e:\doc\hewl\领域模型.xls") '打开文件
strtobewrited = "-----------------------------------" & vbcrlf & _
"-- generated by scriptgenerator ---" & vbcrlf & _
"-----------------------------------" & vbcrlf & vbcrlf
count = objworkbook.worksheets.count '取sheet数量
set my = createobject("excel.sheet") '新建sheet对象
for each my in objworkbook.worksheets '遍历sheet
if my.name = "目录" or my.name = "secondhandhouse" then
'do nothing
else
'wscript.echo my.name '获得sheet名字
'wscript.echo my.rows.count
'strtobewrited = strtobewrited & "create table " & my.name & vbcrlf
strtobewrited = strtobewrited & "/*==============================================================*/" & vbcrlf
strtobewrited = strtobewrited & "/* table: " & my.name & " */" & vbcrlf
strtobewrited = strtobewrited & "/*==============================================================*/" & vbcrlf
strtobewrited = strtobewrited & "create table " & my.name & " (" & vbcrlf
rownum = 3
do until my.cells(rownum,1).value = ""
'wscript.echo "samaccountname: " & my.cells(rownum, 2).value
strtobewrited = strtobewrited & " " & my.cells(rownum,2).value & " " & my.cells(rownum,3).value & " not null"
if not my.cells(rownum,9).value = "" then
strtobewrited = strtobewrited & " default " & my.cells(rownum,9).value
end if
strtobewrited = strtobewrited & "," & vbcrlf
rownum = rownum + 1
loop
strtobewrited = strtobewrited & " constraint pk_" & my.name & " primary key (id)" & vbcrlf
strtobewrited = strtobewrited & ")" & vbcrlf
end if
strtobewrited = strtobewrited & vbcrlf
next
for each my in objworkbook.worksheets '遍历sheet
if my.name = "目录" or my.name = "secondhandhouse" then
'do nothing
else
strtobewrited = strtobewrited & " constraint pk_" & my.name & " primary key (id)" & vbcrlf
strtobewrited = strtobewrited & ")" & vbcrlf
end if
strtobewrited = strtobewrited & vbcrlf
next
'写文件
set fs =createobject("scripting.filesystemobject")
set f = fs.opentextfile("e:\doc\hewl\dbscript.sql",2, true)
'wscript.echo strtobewrited
f.write strtobewrited
f.close
set f = nothing
set fs = nothing
objexcel.quit '结束退出
dim excel
set excel = createobject("excel.application")
'不显示提示信息,这样保存的时候就不会提示是否要覆盖原文件
excel.displayalerts=false
'调用excel文件的时候不显示
excel.visible=false
excel.workbooks.open("d:\test.xls")
'将sheet1设置为活动sheet
excel.workbooks(1).activate
'插入行,这条我找msdn都没找到,最后乱试试出来的
excel.activesheet.rows(1).insert
excel.activesheet.cells(1,1).value = date
excel.activesheet.cells(1,2).value = "row1"
excel.activesheet.cells(1,3).value = "comment1"
excel.activesheet.rows(2).insert
excel.activesheet.cells(2,1).value = date
excel.activesheet.cells(2,4).value = "row2"
excel.activesheet.cells(2,7).value = "comment2"
excel.save
excel.quit
set excel = nothing
excel.activesheet.rows(1).insert
不用找msdn,在excel帮助中就能找到,看“编程信息”/“microsoft excel visual basic 参考”/“属性”/“q-r”/“rows 属性”的介绍,和“编程信息”/“microsoft excel visual basic 参考”/“方法”/“i-l”/“insert 方法”的介绍,就能明白这条语句的语法。
因为在excel的vba中,“rows”、“columns”、“cells”属性返回的都是range对象,所以对它们的应用可以等同range对象的应用。
例如:你在excel的vba编辑器中可以这样写
cells(1,1).value="abc"
cells(1,1).wraptext=false
在写这些语句时,你应该注意到,在写完“cells(1,1).”之后,并没有弹出应该弹出的属性/方法列表,但是这些语句确实可以正常运行。
我的方法是:在excel中录制宏,然后在excel的vba编辑器中修改语句,调试运行无误后再粘贴到vbs语句中,进行适当的修改。
上一篇: 机器智能比大数据牛逼多了