[英]Excel/Powerpoint Office Interop - Copy chart from Excel Sheet to Powerpoint Slide
I have an Excel workbook with multiple charts inside. 我有一个Excel工作簿,里面有多个图表。 I have created a for loop that gets the charts in each sheet. 我创建了一个for循环,用于获取每个工作表中的图表。 This works without error. 这可以正常工作。
The issues I am having are I cannot import multiple charts, only one and it imports as a picture not a chart. 我遇到的问题是我不能导入多个图表,只能导入一个,并且它作为图片而不是图表导入。
When I try to import multiple charts, I get a COMException
on this line: 当我尝试导入多个图表时,在此行上出现COMException
:
objChart.Copy();
Full error: 完整错误:
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
Here Is my full method: 这是我的完整方法:
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;
}
Does anyone see any issues with this code? 有人看到此代码有任何问题吗?
UPDATE UPDATE
If I change this line: 如果我更改此行:
objChart.Copy();
to this: 对此:
objChart.CopyPicture();
I have no issues.. What could be causing this? 我没有问题..是什么原因引起的?
I managed to fix this issue by using objChart.CopyPicture();
我设法通过使用objChart.CopyPicture();
来解决此问题objChart.CopyPicture();
but this was causing crashes for some users so I ended up doing this: 但这导致某些用户崩溃 ,所以我最终这样做:
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.