VBS操作Excel常见方法dim oExcel,oWb,oSheetSet oExcel= CreateObject("Excel.Application")Set oWb = "E:\其他\新装电话表.xls")Set oSheet = oWb.Sheets("Sheet1")MsgBox oSheet.Range("B2").Value '#提取单元格B2内容'.....3、如果是XP系统,可以使用如下代码Dim objFileDlgSet objFileDlg = CreateObject("monDialog") objFileDlg.Filter = "Excel File (*.xls) |*.xls"If objFileDlg.ShowOpen Thenmsgbox "您选择的文件是:" & objFileDlg.FileName & vbCrLfEnd IfVBS控制Excel的一些常见方法:(一) 使用动态创建的方法首先创建 Excel 对象,使用ComObj:oExcel = CreateObject( "Excel.Application" )1) 显示当前窗口:oExcel.Visible = True2) 更改 Excel 标题栏:oExcel.Caption = "应用程序调用 Microsoft Excel"3) 添加新工作簿:4) 打开已存在的工作簿:"C:\Excel\Demo.xls" )5) 设置第2个工作表为活动工作表:oExcel.WorkSheets(2).Activate或oExcel.WorksSheets( "Sheet2" ).Activate6) 给单元格赋值:oExcel.Cells(1,4).Value = "第一行第四列"7) 设置指定列的宽度(单位:字符个数),以第一列为例:= 58) 设置指定行的高度(单位:磅)(1磅=0.035厘米),以第二行为例: = 1/0.035 ' 1厘米9) 在第8行之前插入分页符:oExcel.WorkSheets(1).Rows(8).PageBreak = 110) 在第8列之前删除分页符:= 011) 指定边框线宽度:"B3:D4" ).Borders(2).Weight = 31-左 2-右 3-顶 4-底 5-斜( \ ) 6-斜( / )12) 清除第一行第四列单元格公式:,4).ClearContents13) 设置第一行字体属性:= "隶书"= clBlue= True= True14) 进行页面设置:a.页眉:= "报表演示"b.页脚:= "第&P页"c.页眉到顶端边距2cm:= 2/0.035d.页脚到底端边距3cm:= 3/0.035e.顶边距2cm:= 2/0.035f.底边距2cm:= 2/0.035g.左边距2cm:= 2/0.035h.右边距2cm:= 2/0.035i.页面水平居中:= 2/0.035j.页面垂直居中:= 2/0.035k.打印单元格网线:= True15) 拷贝操作:a.拷贝整个工作表:b.拷贝指定区域:"A1:E2" ).Copyc.从A1位置开始粘贴:"A1" ).PasteSpeciald.从文件尾部开始粘贴:16) 插入一行或一列:a.b.17) 删除一行或一列:a.b.18) 打印预览工作表:19) 打印输出工作表:20) 工作表保存:if not then21) 工作表另存为:oExcel.SaveAs( "C:\Excel\Demo1.xls" )22) 放弃存盘:= True23) 关闭工作簿:24) 退出 Excel:oExcel.Quit(二) 使用VBS 控制Excle二维图1)选择当第一个工作薄第一个工作表set oSheet=oExcel.Workbooks(1).Worksheets(1) 2)增加一个二维图achart=,100,200,200)3)选择二维图的形态4)给二维图赋值set series=range="sheet1!r2c3:r3c9"series.add range,true5)加上二维图的标题" Excle二维图"6)改变二维图的标题字体大小7)给二维图加下标说明, xlPrimary).HasTitle = True, xlPrimary). = "下标说明"8)给二维图加左标说明, xlPrimary).HasTitle = True, xlPrimary). = "左标说明"9)给二维图加右标说明, xlSecondary).HasTitle = True, xlSecondary). = "右标说明"10)改变二维图的显示区大小= 5= 223= 108如何用vbs把excel的单元格数据写到txt复制代码代码如下:If > 0 Then Filename = WScript.Arguments(0)Set a = CreateObject("Excel.Application")If Filename = "" ThenFilename = a.GetOpenFilename("Excel Files (*.xls), *.xls")If VarType(Filename) = vbBoolean ThenMsgBox "Excel2Txt用于将Excel文件的每个Sheet保存为一个文本文件。
" & vbCr & vbLf & vbCr & vbLf & "用法: Excel2Txt filename.xls 或在对话框中打开Excel文件。
"WScript.QuitEnd IfEnd IfSet w =n = Replace(Replace(, ".xls", ""), ".XLS", "")a.DisplayAlerts = FalseFor Each s In w.Sheetss.SaveAs w.Path & "\" & n & "_" & & ".txt", 20Nexta.Quit把以上代码存为Excel2Txt.vbs双击执行就行了VBS操作Excel复制代码代码如下:Set objExcel = CreateObject("Excel.Application") '建一个exel对象Set objWorkbook = _("E:\DOC\Hewl\领域模型.xls") '打开文件strToBeWrited = "-----------------------------------" & vbcrlf & _"-- Generated by ScriptGenerator ---" & vbcrlf & _"-----------------------------------" & vbcrlf & vbcrlfCount = '取sheet数量Set my = CreateObject("Excel.Sheet") '新建sheet对象For Each my In objWorkbook.WorkSheets '遍历sheetIf = "目录" or = "SecondHandHouse" Then'do nothingElse'Wscript.Echo '获得sheet名字'Wscript.Echo'strToBeWrited = strToBeWrited & "create table " & & vbcrlfstrToBeWrited = strToBeWrited &"/*============================================================= =*/" & vbcrlfstrToBeWrited = strToBeWrited & "/* Table: " & & " */" & vbcrlfstrToBeWrited = strToBeWrited &"/*============================================================= =*/" & vbcrlfstrToBeWrited = strToBeWrited & "create table " & & " (" & vbcrlfrowNum = 3Do 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 = "" ThenstrToBeWrited = strToBeWrited & " default " &my.Cells(rowNum,9).ValueEnd IfstrToBeWrited = strToBeWrited & "," & vbcrlfrowNum = rowNum + 1LoopstrToBeWrited = strToBeWrited & " constraint PK_" & & " primary key (id)" & vbcrlfstrToBeWrited = strToBeWrited & ")" & vbcrlfEnd IfstrToBeWrited = strToBeWrited & vbcrlfNextFor Each my In objWorkbook.WorkSheets '遍历sheetIf = "目录" or = "SecondHandHouse" Then'do nothingElsestrToBeWrited = strToBeWrited & " constraint PK_" & & " primary key (id)" & vbcrlfstrToBeWrited = strToBeWrited & ")" & vbcrlfEnd IfstrToBeWrited = strToBeWrited & vbcrlfNext'写文件set fs =createobject("scripting.filesystemobject")set f = fs.opentextfile("E:\DOC\Hewl\dbscript.sql",2, true)'Wscript.Echo strToBeWritedf.write strToBeWritedf.closeSet f = nothingSet fs = nothingobjExcel.Quit '结束退出复制代码代码如下:Dim ExcelSet Excel = CreateObject("Excel.Application")'不显示提示信息,这样保存的时候就不会提示是否要覆盖原文件Excel.DisplayAlerts=FALSE'调用EXCEL文件的时候不显示Excel.visible=FALSE"D:\test.XLS")'将sheet1设置为活动sheetExcel.workbooks(1).activate'插入行,这条我找MSDN都没找到,最后乱试试出来的,1).Value = Date,2).Value = "row1",3).Value = "comment1",1).Value = Date,4).Value = "row2",7).Value = "comment2"Excel.saveExcel.quitSet Excel = Nothing不用找MSDN,在EXCEL帮助中就能找到,看“编程信息”/“Microsoft Excel Visual Basic 参考”/“属性”/“Q-R”/“Rows 属性”的介绍,和“编程信息”/“Microsoft Excel Visual Basic 参考”/“方法”/“I-L”/“Insert 方法”的介绍,就能明白这条语句的语法。