ASP.NET中实现导出ppt文件数据的实例分享

2025-05-29 0 21

 前一段时间因工作需要,需增加ppt数据的导出下载。发现网络上这方面资料并不是很多,零零散散地找到一些相关的资料,经过自己的试验,终于完成相关功能。应博友要求,在此分享下我的经验,不好之处还望大家多多指出。

  在做之前,首先需要添加相关引用Microsoft.Office.Interop.PowerPoint.dll。

?

1
using PowerPoint = Microsoft.Office.Interop.PowerPoint;

  操作PPT代码如下:

复制代码

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73
public void createPPT()

{

try

{

//ppt存储路径

string path = string.Format("{0}/{1}.ppt", Server.MapPath("."), DateTime.Now.Ticks.ToString());

//ppt引用的模版路径

string MyTemplateFile = "d:\\\\test.pot";

PowerPoint.ApplicationClass MyApp;

PowerPoint.Presentations MyPresSet;

PowerPoint._Presentation MyPres;

PowerPoint.Shape objShape;

PowerPoint.Slides objSlides;

PowerPoint._Slide MySlide;

PowerPoint.TextRange objTextRng;

PowerPoint.Table table = null;

MyApp = new PowerPoint.ApplicationClass();

//如果已存在,则删除

if (File.Exists((string)path))

{

File.Delete((string)path);

}

Object Nothing = Missing.Value;

//套用模版

MyPres = MyApp.Presentations.Open(MyTemplateFile, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse);

MyPresSet = MyApp.Presentations;

objSlides = MyPres.Slides;

//创建第一张PPT ppLayoutTitle指定模板首页

MySlide = objSlides.Add(1, PowerPoint.PpSlideLayout.ppLayoutTitle);

//添加一行文字(left:10,top:110,width:700,height:400)

objTextRng = MySlide.Shapes.AddLabel(MsoTextOrientation.msoTextOrientationHorizontal, 10, 110, 700, 400).TextFrame.TextRange;

objTextRng.Text = " PPT";

objTextRng.Font.Color.RGB = 0x66CCFF; //设置字的颜色

objTextRng.Font.Size = 42; //字号

//创建第二张PPT ppLayoutBlank指定无标题页

MySlide = objSlides.Add(2, PowerPoint.PpSlideLayout.ppLayoutBlank);

//插入图片

MySlide.Shapes.AddPicture("1.jpg", MsoTriState.msoFalse, MsoTriState.msoTrue, 110, 140, 500, 300);

//创建第三张PPT ppLayoutTitleOnly指定仅有标题页

MySlide = objSlides.Add(3, PowerPoint.PpSlideLayout.ppLayoutTitleOnly);

objTextRng = MySlide.Shapes[1].TextFrame.TextRange;

objTextRng.Text = "目录";

objTextRng.Font.Size = 32;

//插入图片

MySlide.Shapes.AddPicture("1.jpg", MsoTriState.msoFalse, MsoTriState.msoTrue, 110, 140, 500, 300);

//创建第四张PPT

MySlide = objSlides.Add(3, PowerPoint.PpSlideLayout.ppLayoutBlank);

//添加一个表格

objShape = MySlide.Shapes.AddTable(3, 3, 105, 150, 400, 100);

table = objShape.Table;

for (int i = 1; i <= table.Rows.Count; i++)

{

for (int j = 1; j <= table.Columns.Count; j++)

{

table.Cell(i, j).Shape.TextFrame.TextRange.Font.Size = 12;

table.Cell(i, j).Shape.TextFrame.TextRange.Text = string.Format("[{0},{1}]", i, j);

}

}

//保存格式

PowerPoint.PpSaveAsFileType format = PowerPoint.PpSaveAsFileType.ppSaveAsDefault;

//内容保存

MyPres.SaveAs(path, format, Microsoft.Office.Core.MsoTriState.msoFalse);

//关闭excelDoc文档对象

MyPres.Close();

//关闭excelApp组件对象

MyApp.Quit();

}

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 ASP.NET中实现导出ppt文件数据的实例分享 https://www.kuaiidc.com/102406.html

相关文章

发表评论
暂无评论