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內容
'.....
三、若是是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 '結束退出