I've written the following simple C# console application to export all the charts from an Excel Workbook. It works just fine unless the chart has not been scrolled to since opening the document, in which case an empty image file is generated.
using Excel = Microsoft.Office.Interop.Excel;
using System;
using System.Diagnostics;
namespace ExcelExporter
{
class ChartExporter
{
const string EXPORT_TO_DIRECTORY = @"C:\Users\Sandy\Desktop\Excel\Charts";
static void Main(string[] args)
{
Excel.Application app = System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application") as Microsoft.Office.Interop.Excel.Application;
ConsoleColor c = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("Export To: ");
Console.ForegroundColor = c;
string exportPath = Console.ReadLine();
if (exportPath == "")
exportPath = EXPORT_TO_DIRECTORY;
Excel.Workbook wb = app.ActiveWorkbook;
foreach (Excel.Worksheet ws in wb.Worksheets)
{
Excel.ChartObjects chartObjects = (Excel.ChartObjects)(ws.ChartObjects(Type.Missing));
foreach (Excel.ChartObject co in chartObjects)
{
Excel.Chart chart = (Excel.Chart)co.Chart;
// app.Goto(co, true);
chart.Export(exportPath + @"\" + chart.Name + ".png", "PNG", false);
}
}
Process.Start(exportPath);
}
}
}
I've made a few failed attempts to scroll to the object; the commented out line towards the bottom of the program (app.Goto(co, true);
) for example only works for ranges. Is there any way to scroll to ChartObject
s, or otherwise ensure that they properly export to images?
For testing, use a workbook with charts 1000+ rows down (far enough that they are unquestionably out of view when the document opens); close and reopen the document before running the program (once scrolled to, the charts will have already been rendered and stored in memory).