Reputation: 297
I am trying to get a handle on an excel graph and save it through MATLAB.
So far I have managed to cycle through sheets and get a handle on the graph object but I can't save it.
Does anyone know how to export the object as a jpg
or png
or other graphic format?
I tried the code below using the SaveAs
method but it doesn't work
Workbook = Workbooks.Open(['file.name]);
Sheets = Workbook.Sheets;
for i = 1:Sheets.Count
Activesheet = get(Sheets, 'Item',i);
for j = 1:Activesheet.ChartObjects.Count
obj = Activesheet.ChartObjects(j);
obj.SaveAs('asfasfa.jpg')
end
end
Upvotes: 3
Views: 1475
Reputation: 11812
The excel Chart
objects have an Export
method.
Microsoft documentation : Chart.Export Method (Excel)
For the example, I created a simple excel file (named test_save_chart.xlsx
) with a chart in the first sheet. To export this chart as a PNG
picture is pretty straightforward:
xfile = 'test_save_chart.xlsx' ;
exl = actxserver('excel.application'); %// Create a COM server
exlFile = exl.Workbooks.Open( [pwd '\' xfile] ); %'// Open the file
chartobj = exlFile.Sheets.Item('Sheet1').ChartObjects(1) ; %// get a handle to the chart object
chartobj.Chart.Export('C:\TEMP\StackExchange\testChartExport.png','PNG') %// export to PNG
In your case, inside your loop, it is as simple as:
chartobj = Activesheet.ChartObjects(j) ;
chartobj.Chart.Export('your_filename_here.png','PNG')
Since you are doing that in a loop, you will have to generate the file name dynamically (otherwise you are going to overwrite the same file over and over again).
Note that png
format is recommended over jpg
, specially for "line art" style of picture like excel chart produce.
Upvotes: 5