Excel/Powerpoint Office Interop - 将图表从 Excel Sheet 复制到 Powerpoint 幻灯片

Excel/Powerpoint Office Interop - Copy chart from Excel Sheet to Powerpoint Slide

我有一个 Excel 工作簿,里面有多个图表。我创建了一个 for 循环,用于获取每个 sheet 中的图表。这可以正常工作。

我遇到的问题是我无法导入多个图表,只能导入一个,而且它是作为图片而不是图表导入的。

当我尝试导入多个图表时,我在这一行得到 COMException

objChart.Copy();

完整错误:

An exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll but was not handled in user code

Additional information: Exception from HRESULT: 0x800A03EC

这是我的完整方法:

public int ImportExcelChartsFromWorkbookToSlides(int startingSlideIndex, string workbookPath, string[] slideTitles)
{
    int slideIndex = startingSlideIndex;
    int titleIndex = 0;
    EXCL.Application objExclApp = new EXCL.Application();
    EXCL.Workbook objWorkbook = objExclApp.Workbooks.Open(workbookPath, Editable: false);
    foreach (EXCL.Worksheet objSheet in objWorkbook.Worksheets)
    {
        foreach (EXCL.ChartObject objChart in objSheet.ChartObjects())
        {
            AddTitleOnlySlide(slideIndex);
            SetTitleOnlySlideTitle(slideTitles[titleIndex]);

            // Copy Chart from Sheet to Slide
            objChart.Copy();

            PPT.ShapeRange objShapeRange = objSlide.Shapes.Paste();

            // TODO PARAMETER
            objShapeRange.Left = 10;
            objShapeRange.Top = 100;

            slideIndex++;
            titleIndex++;
        }
    }
    return slideIndex;
}

有人看到这段代码有什么问题吗?

更新

如果我更改此行:

objChart.Copy();

对此:

objChart.CopyPicture();

我没有问题..这可能是什么原因造成的?

我设法通过使用 objChart.CopyPicture(); 解决了这个问题,但对于某些用户来说这是 所以我最终这样做了:

string chartPath = $@"{workbookPath.Substring(0, workbookPath.LastIndexOf("\") + 1)}{DateTime.Now.Ticks}.png";
objChart.Chart.Export(chartPath, "PNG", false);
objSlide.Shapes.AddPicture2(chartPath, MsoTriState.msoFalse, MsoTriState.msoTrue, chartPosLeft, chartPosTop, compress: MsoPictureCompress.msoPictureCompressFalse);