VB操作Excel
Private Sub Command1_Click()Dim zsbexcel As Excel.Application
'----------------------------------------------------------------
Rem (1)
Set zsbexcel = New Excel.Application
'激活EXCEL应用程序
'Set zsbexcel = CreateObject("Excel.Application")
'----------------------------------------------------------------
zsbexcel.Visible = True
Rem (2)
'打开工作簿
Set zsbworkbook = zsbexcel.Workbooks.Open(App.Path & "\11.xlt")
' '新建工作簿
' Set zsbworkbook = zsbexcel.Workbooks.Add(App.Path & "\11.xlt")
'----------------------------------------------------------------
'返回或设置Microsoft Excel自动插入到新工作簿中的工作表数目
zsbexcel.SheetsInNewWorkbook = 1
'----------------------------------------------------------------
'边框设置
With zsbexcel.ActiveSheet.Range("A2:C9").Borders
.LineStyle = xlBorderLineStyleContinuous
.Weight = xlThin
.ColorIndex = 1
End With
'字体设置
With zsbexcel.ActiveSheet.Range("A3:C9").Font
.Size = 14
.Bold = True
.Italic = True
.ColorIndex = 3
End With
zsbexcel.ActiveSheet.Rows.HorizontalAlignment = xlVAlignCenter '水平居中
zsbexcel.ActiveSheet.Rows.VerticalAlignment = xlVAlignCenter '垂直居中
With zsbexcel.ActiveSheet
.Cells(1, 2).Value = "100"
.Cells(2, 2).Value = "200"
.Cells(3, 2).Value = "=SUM(B1:B2)"
.Cells(1, 3).Value = "中国VB网--www.chinavb.net"
.Range("A3:A9") = "50"
End With
'
' '画曲线
' Range("B4:F8").Select
' Charts.Add
' ActiveChart.ChartType = xlColumnClustered
' ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range("B4:F8"), PlotBy:=xlColumns
' ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet1"
'
' With ActiveChart
' .HasTitle = False
' .Axes(xlCategory, xlPrimary).HasTitle = False
' .Axes(xlValue, xlPrimary).HasTitle = False
' End With
'设置打印方向
zsbexcel.ActiveSheet.PageSetup.Orientation = xlPortrait ' xlLandscape
'设置打印纸大小
zsbexcel.ActiveSheet.PageSetup.PaperSize = xlPaperA4
zsbexcel.Caption = "打印预览" '设置预览窗口标题
zsbexcel.ActiveSheet.PrintPreview '打印预览
zsbexcel.ActiveSheet.PrintOut '打印输出
'为了在退出应用程序后EXCEL不提示用户是否保存已修改的文件,需使用如下语句:
zsbworkbook.Save
'使用工作簿的SaveAs方法覆盖现有文件,"覆盖"警告默认为"No",当 DisplayAlerts属性值设置为True时,Excel 选择"Yes"。
zsbexcel.DisplayAlerts = False
zsbworkbook.Close (True)
zsbexcel.DisplayAlerts = True
Set zsbexcel = Nothing
Set zsbworkbook = Nothing
End Sub
Rem
'请问:以上代码执行后好像没有将代码中的11.xlt打开,为什么不能将11.xlt保存到指定的地方,老是被保存我的文档中了!谁能帮我修改一下呢? --------------------编程问答-------------------- zsbworkbook.SaveAs "d:\11.xls" --------------------编程问答-------------------- 如楼上所言. --------------------编程问答--------------------
--------------------编程问答-------------------- 没有指定保存的路径,
参考1楼的方法
补充:VB , 基础类