2012-01-18 148 views

回答

1

This article介绍了如何使用VBA将Excel图表导出为图像。

+0

只是为别人所希望创建一个图像注释。如果您使用的是Excel 2010,则可能必须在将图表导出为图像之前激活图表oChart.Activate。 – nickfinity 2012-08-01 19:59:11

+0

您不应该激活图表。 Export方法与图表一起使用,而不是图表对象(这是人们发现他们需要激活图表以使某些工作起作用时的常见问题)。 – 2015-07-18 15:35:20

0

我不得不在我的一个项目中做出类似的事情。它将图表从应用程序转换为运行实时数据的仪表板。 我的解决方案最终被通过ODBC连接到应用程序的数据库和逆向工程一步用VBA

步骤的图表(报告)的生成:

1. do an automatic refresh of data by setting a refresh interval in the Excel Data Query 
2. extract the information from the Excel worksheet with VBA 
3. generate Pivot Tables and Pivot Charts upon the information extracted with VBA 
4. convert the Pivot Charts or Range of Cells(=Tables) to Images and Paste them above the Chart Objects in the Excel Worksheets 
5. cut area(s) from the Worksheets (that contained the chart or tables) by giving dimensions and save these as images 
6. read the images saved at the previous stage via a webpage (depending on its additional content either static-.html or dynamic 
.aspx) 
7. set a refreshment time in the webpage source, and there is the dashboard 

正如你可以在上面看到,我做不是将图表转换为图像,而是将包含图表的图表区域转换为图像。

这是VBA代码,可以帮助你:

Sub Pivot_Chart_And_Table_of_Auftragsart_To_Image() 
'the code for selecting successively the areas of Pivot Table and Pivot Chart and save them as image file types .jpg to a defined directory path 

'below you set the Range(=Area in the Worksheet) you want to export to file 
Dim rgExp As Range 

Set rgExp = Range("A1:E5") 

''' Copy range as picture onto Clipboard 
rgExp.CopyPicture Appearance:=xlScreen, Format:=xlBitmap 
''' Create an empty chart with exact size of range copied 
With ActiveSheet.ChartObjects.Add(Left:=rgExp.Left, Top:=rgExp.Top, _ 
Width:=rgExp.Width, Height:=rgExp.Height) 
.Name = "nameOfTheObjectHerewithGenerated" 
.Border.LineStyle = xlNone 'this removes the border line of the temporary Chart Object 
.Activate 
End With 

''' Paste into chart area, export to file 
ActiveChart.Paste 

ActiveSheet.ChartObjects("nameOfTheObjectHerewithGenerated").Chart.Export "C:\whateverDirectoryYouWish\nameOfImage.jpg"