Exporting Excel Charts as Images

2020-07-18 08:24发布

问题:

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 ChartObjects, 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).

回答1:

VBA, but same idea would likely work for C#...

Dim co As ChartObject, sht As Worksheet, x As Long
x = 1
Set sht = ThisWorkbook.Sheets("Sheet1")
For Each co In sht.ChartObjects
    Application.Goto co.TopLeftCell, True
    co.Chart.Export "C:\_stuff\test\chart" & x & ".jpg", "JPG", False
    x = x + 1
Next co


回答2:

I found the following worked for me

            foreach (Excel.ChartObject co in chartObjects)
            {
                co.Select();
                Excel.Chart chart = (Excel.Chart)co.Chart;
                chart.Export(exportPath + @"\" + chart.Name + ".png", "PNG", false);
            }