2012-01-18 130 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"