VB控制Excel工作薄实例精选二

1、获取Excel数据图表到VB的PictureBox控件中

'先引用对象库:Microsoft Excel 11.0 Object Library
Option Explicit
Dim xlExcel As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet

Private Sub Command1_Click()
        On Error GoTo Errhandler
        CommonDialog1.Filter = "Excel(*.xls)|*.xls|AllFile(*.*)|*.*"
        CommonDialog1.FilterIndex = 1
        CommonDialog1.ShowOpen
        Set xlExcel = New Excel.Application
        xlExcel.Workbooks.Open CommonDialog1.FileName
        Set xlBook = xlExcel.Workbooks(CommonDialog1.FileTitle)
        Application.Visible = True
        Set xlSheet = xlBook.Worksheets("Sheet1") '指定Sheet表
        xlSheet.ChartObjects(2).Chart.copyPicture '读取第2个数据图表到剪贴板
        Picture1.Picture = Clipboard.GetData      '粘贴数据到图片框
        Clipboard.Clear                           '清除剪贴板数据
Errhandler:
        Exit Sub
End Sub

Private Sub Form_Unload(Cancel As Integer)
    On Error Resume Next
    xlBook.Close
    xlExcel.Quit
    Set xlSheet = nothing
    Set xlBook = nothing
    Set xlExcel = nothing
End Sub

效果图如下:

2、插入图片到Excel表格中

'先引用对象库:Microsoft Excel 11.0 Object Library
Option Explicit
Dim xlExcel As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet

Private Sub Command1_Click()
        On Error GoTo Errhandler
        CommonDialog1.Filter = "Excel(*.xls)|*.xls|AllFile(*.*)|*.*"
        CommonDialog1.FilterIndex = 1
        CommonDialog1.ShowOpen
        Set xlExcel = New Excel.Application
        xlExcel.Workbooks.Open CommonDialog1.FileName
        Set xlBook = xlExcel.Workbooks(CommonDialog1.FileTitle)
        Application.Visible = True
        Application.displayAlerts = False '不提示保存对话框
        Set xlSheet = xlBook.Worksheets("Sheet1") '指定Sheet表
        xlBook.Sheets("Sheet1").Select  '指定Sheet表
        ActiveSheet.Pictures.Insert("F:\资料\My Pictures\20056158712694.jpg").Select  '插入图片
        xlBook.Save
Errhandler:
        Exit Sub
End Sub

Private Sub Form_Load()
    Picture1.Picture = LoadPicture("F:\资料\My Pictures\20056158712694.jpg")
End Sub

Private Sub Form_Unload(Cancel As Integer)
    On Error Resume Next
    xlBook.Close
    xlExcel.Quit
    Set xlSheet = nothing
    Set xlBook = nothing
    Set xlExcel = nothing
End Sub


效果图如下:

3、获取Excel表格中的Ole对象(获取图像)

'先引用对象库:Microsoft Excel 11.0 Object Library
Option Explicit
Dim xlExcel As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet

Private Sub Command1_Click()
        On Error GoTo Errhandler
        CommonDialog1.Filter = "Excel(*.xls)|*.xls|AllFile(*.*)|*.*"
        CommonDialog1.FilterIndex = 1
        CommonDialog1.ShowOpen
        Set xlExcel = New Excel.Application
        xlExcel.Workbooks.Open CommonDialog1.FileName
        Set xlBook = xlExcel.Workbooks(CommonDialog1.FileTitle)
        Application.Visible = True
        Application.displayAlerts = False '不提示保存对话框
        Set xlSheet = xlBook.Worksheets("Sheet1") '指定Sheet表
        xlBook.Sheets("Sheet1").Select  '指定Sheet表
        xlSheet.Shapes(3).copyPicture '读取编号为3的图片。需要注意的是,该命令不仅能获取数据图表,也能获取图片。
        '或者xlSheet.OLEObjects(3).copyPicture
        Picture1.Picture = Clipboard.GetData      '粘贴数据到图片框
        Clipboard.Clear                           '清除剪贴板数据
Errhandler:
        Exit Sub
End Sub

Private Sub Form_Unload(Cancel As Integer)
    On Error Resume Next
    xlBook.Close
    xlExcel.Quit
    Set xlSheet = nothing
    Set xlBook = nothing
    Set xlExcel = nothing
End Sub


效果图如下:

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...