为了在powerpoint幻灯片放映中显示excel数据,我们可以将excel作为工作表对象插入到powerpoint,本文我们将讨论如何通过spire将excel工作表作为ole对象插入到幻灯片。为实现此功能,我们需要使用spire.xls加载excel文件并获取数据,然后使用spire.presentation创建ole对象, 所以请下载spire.office并引用相应的dll到应用程序。
c#
//加载excel文档
workbook book = new workbook();
book.loadfromfile(@"c:\users\administrator\desktop\book1.xlsx");
//选择单元格范围并将其保存为图像
image image = book.worksheets[0].toimage(1, 1, 5, 4);
//新建一个powerpoint文档
presentation ppt = new presentation();
//插入图像到powerpoint文档
iimagedata oleimage = ppt.images.append(image);
rectangle rec = new rectangle(60, 60, image.width, image.height);
using (memorystream ms = new memorystream())
{
//将excel数据保存到流
book.savetostream(ms);
ms.position = 0;
//根据excel数据将ole对象插入到powerpoint文档
spire.presentation.ioleobject oleobject = ppt.slides[0].shapes.appendoleobject("excel", ms.toarray(), rec);
oleobject.substituteimagepicturefillformat.picture.embedimage = oleimage;
oleobject.progid = "excel.sheet.12";
}
//保存文档
ppt.savetofile("插入ole.pptx", spire.presentation.fileformat.pptx2013);
vb.net
'加载excel文档
dim book as new workbook()
book.loadfromfile("c:\users\administrator\desktop\book1.xlsx")
'选择单元格范围并将其保存为图像
dim image as image = book.worksheets(0).toimage(1, 1, 5, 4)
'新建一个powerpoint文档
dim ppt as new presentation()
'插入图像到powerpoint文档
dim oleimage as iimagedata = ppt.images.append(image)
dim rec as new rectangle(60, 60, image.width, image.height)
using ms as new memorystream()
'将excel数据保存到流
book.savetostream(ms)
ms.position = 0
'根据excel数据将ole对象插入到powerpoint文档
dim oleobject as spire.presentation.ioleobject = ppt.slides(0).shapes.appendoleobject("excel", ms.toarray(), rec)
oleobject.substituteimagepicturefillformat.picture.embedimage = oleimage
oleobject.progid = "excel.sheet.12"
end using
'保存文档
ppt.savetofile("插入ole.pptx", spire.presentation.fileformat.pptx2013)
截图展示: