如何在excel中用vba把图表导出为图片格式的文件?

在excel vba中Chart对象的Export方法可以把图表导出为图片。

它的语法如下:

expression .Export(Filename, FilterName, Interactive)

其中expression指的是Chart对象,Filename为导出后的文件的名称,FilterName、Interactive 参数比较少用,可以不填。

vba中可以返回Chart对象的父对象有多种,比如shape对象的Chart属性,ChartObject对象的Chart属性等都可以返回Chart对象。

根据以上的知识,可以通过遍历所有shape对象、ChartObject对象以及Chart对象,把所有图表都导出为独立的图片格式文件。

Sub QQ1722187970()
    Dim oShape As Shape
    Dim oChartObject As ChartObject
    Dim oChart As Chart
    Dim oWK As Worksheet
    Dim sPath As String
    Dim i As Long
    i = 1
    sPath = Excel.ThisWorkbook.Path & "\"
    For Each oWK In Excel.ThisWorkbook.Worksheets
        With oWK
            For Each oShape In .Shapes
                With oShape
                    If .HasChart Then
                        .Chart.Export sPath & i & ".bmp"
                        i = i + 1
                    End If
                End With
            Next
            For Each oChartObject In .ChartObjects
                With oChartObject
                    .Chart.Export sPath & i & ".bmp"
                    i = i + 1
                End With
            Next
        End With
    Next
    For Each oChart In Excel.ThisWorkbook.Charts
        With oChart
            .Export sPath & i & ".bmp"
            i = i + 1
        End With
    Next
    MsgBox "所有图表导出完毕!!!"
End Sub

 

 

 

       

发表评论