简体   繁体   English

使用 VSTO 将一系列单元格从 excel 复制到 powerpoint

[英]Copying a range of cells from excel into powerpoint using VSTO

How do I copy a range of cells from Excel to Powerpoint.如何将一系列单元格从 Excel 复制到 Powerpoint。 I want to do the same thing as when you select a range of cells, copy to clipboard and then paste in a slide in powerpoint.我想做与您选择一系列单元格、复制到剪贴板然后粘贴到幻灯片中的幻灯片相同的事情。

I have tried Range.Copy(missing) and Slide.Shapes.Paste this code but it's not working.我试过Range.Copy(missing)Slide.Shapes.Paste这段代码,但它不起作用。 It throws an exception like它抛出一个异常,如

'Shapes (unknown member) : Invalid request. '形状(未知成员):无效请求。 Clipboard is empty or contains data which may not be pasted here.'剪贴板为空或包含可能无法粘贴到此处的数据。

The clipboard does contain data though and googling the error shows that other people are also facing this issue with Excel 2007 which is what I am using.剪贴板确实包含数据,谷歌搜索错误表明其他人也面临这个问题,我正在使用 Excel 2007。

My code looks like this:我的代码如下所示:

PowerPoint.ApplicationClass app = new Microsoft.Office.Interop.PowerPoint.ApplicationClass();
app.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
PowerPoint.Presentation pptPresentation = app.Presentations.Add(Microsoft.Office.Core.MsoTriState.msoTrue);

Excel.Worksheet NewWorksheet = ThisApplication.Sheets[i] as Excel.Worksheet;
// Add a blank slide to the presentation.
PowerPoint.Slide pptSlide = pptPresentation.Slides.Add(1, PowerPoint.PpSlideLayout.ppLayoutBlank);
pptSlide.Name = NewWorksheet.Name;

Excel.Range resultRange = NewWorksheet.UsedRange;
resultRange.Copy(missing); 
pptSlide.Shapes.Paste();

Now I am iterating through the excel sheet's cells one by one, creating a table with the right rows and columns in powerpoint and pasting but it is slow and I have to take care of fitting the data into the slide by changing the font size whereas this is done by powerpoint when you paste the entire range.现在我正在逐个遍历 Excel 工作表的单元格,在 powerpoint 和粘贴中创建一个包含正确行和列的表格,但速度很慢,我必须通过更改字体大小来处理将数据拟合到幻灯片中,而这当您粘贴整个范围时,由 powerpoint 完成。

How do I copy the entire range without iterating through each cell?如何在不遍历每个单元格的情况下复制整个范围?

Any suggestions?有什么建议?

What happens if you use:如果您使用会发生什么:

app.ActiveWindow.View.Paste(); 
// instead of
// pptSlide.Shapes.Paste();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM