真是蛋疼,pdf,word,excel的数据下载功能做完了,现在又增加了ppt的数据下载功能需求,google了一下,发现这方面的资料不多,在一些国外网站上可以零零散散地找到一些相关的资料,稍作整理,在此给出一个示例。本程序调用了MS的Com组件,因此请先添加引用,笔者在本机安装的是office 2003完整版,添加的引用是:Microsoft PowerPoint 11.0 Object Library。
using MsPPT = Microsoft.Office.Interop.PowerPoint;
asp.net操作PowerPoint程序示例如下:
private void CreatePPT()
{
MsPPT.Application appPowerPoint = null;
MsPPT.Presentations ppt = null;
MsPPT._Presentation presentation = null;
MsPPT.Slides slides = null;
MsPPT._Slide slide = null;
MsPPT.TextRange textRange = null;
MsPPT.Shape shape = null;
MsPPT.Shapes shapes = null;
MsPPT.Table table = null;
string fileName = string.Format("{0}/{1}.ppt", Server.MapPath("."), DateTime.Now.Ticks.ToString());
string strTemplate = "C:\\Program Files\\Microsoft Office\\Templates\\Presentation Designs\\Blends.pot";
try
{
appPowerPoint = new MsPPT.ApplicationClass();
appPowerPoint.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
ppt = appPowerPoint.Presentations;
// 根据模板创建新的演示文稿
presentation = ppt.Open(strTemplate,
Microsoft.Office.Core.MsoTriState.msoFalse,
Microsoft.Office.Core.MsoTriState.msoTrue,
Microsoft.Office.Core.MsoTriState.msoTrue);
slides = presentation.Slides;
// 为幻灯片添加文本
slide = slides.Add(1, Microsoft.Office.Interop.PowerPoint.PpSlideLayout.ppLayoutTitle);
textRange = slide.Shapes[1].TextFrame.TextRange;
textRange.Text = "我的示例幻灯片";
/// 新建一张幻灯片
slide = slides.Add(2, Microsoft.Office.Interop.PowerPoint.PpSlideLayout.ppLayoutText);
textRange = slide.Shapes[1].TextFrame.TextRange;
textRange.Text = "第二张幻灯片";
// 添加一幅图片
slide.Shapes.AddPicture(" http://www.baidu.com/img/baidu_logo.gif",
Microsoft.Office.Core.MsoTriState.msoFalse,
Microsoft.Office.Core.MsoTriState.msoTrue, 100, 150, 270, 129);
// 添加一个表格
shapes = slide.Shapes;
shape = shapes.AddTable(3, 3, 105, 320, 400, 100);
table = shape.Table;
for (int i = 0; i < table.Rows.Count; i )
{
for (int j = 0; j < table.Columns.Count; j )
{
table.Cell(i 1, j 1).Shape.TextFrame.TextRange.Font.Size = 12;
table.Cell(1 1, j 1).Shape.TextFrame.TextRange.Text = string.Format("[{0},{1}]", i 1, j 1);
}
}
// 保存PPT
presentation.SaveAs(fileName,
Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsDefault,
Microsoft.Office.Core.MsoTriState.msoFalse);
}
catch (Exception ex)
{
throw ex;
}
finally
{
presentation.Close();
appPowerPoint.Assistant.Visible = false;
appPowerPoint.Quit();
GC.Collect();
}
}
{
MsPPT.Application appPowerPoint = null;
MsPPT.Presentations ppt = null;
MsPPT._Presentation presentation = null;
MsPPT.Slides slides = null;
MsPPT._Slide slide = null;
MsPPT.TextRange textRange = null;
MsPPT.Shape shape = null;
MsPPT.Shapes shapes = null;
MsPPT.Table table = null;
string fileName = string.Format("{0}/{1}.ppt", Server.MapPath("."), DateTime.Now.Ticks.ToString());
string strTemplate = "C:\\Program Files\\Microsoft Office\\Templates\\Presentation Designs\\Blends.pot";
try
{
appPowerPoint = new MsPPT.ApplicationClass();
appPowerPoint.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
ppt = appPowerPoint.Presentations;
// 根据模板创建新的演示文稿
presentation = ppt.Open(strTemplate,
Microsoft.Office.Core.MsoTriState.msoFalse,
Microsoft.Office.Core.MsoTriState.msoTrue,
Microsoft.Office.Core.MsoTriState.msoTrue);
slides = presentation.Slides;
// 为幻灯片添加文本
slide = slides.Add(1, Microsoft.Office.Interop.PowerPoint.PpSlideLayout.ppLayoutTitle);
textRange = slide.Shapes[1].TextFrame.TextRange;
textRange.Text = "我的示例幻灯片";
/// 新建一张幻灯片
slide = slides.Add(2, Microsoft.Office.Interop.PowerPoint.PpSlideLayout.ppLayoutText);
textRange = slide.Shapes[1].TextFrame.TextRange;
textRange.Text = "第二张幻灯片";
// 添加一幅图片
slide.Shapes.AddPicture(" http://www.baidu.com/img/baidu_logo.gif",
Microsoft.Office.Core.MsoTriState.msoFalse,
Microsoft.Office.Core.MsoTriState.msoTrue, 100, 150, 270, 129);
// 添加一个表格
shapes = slide.Shapes;
shape = shapes.AddTable(3, 3, 105, 320, 400, 100);
table = shape.Table;
for (int i = 0; i < table.Rows.Count; i )
{
for (int j = 0; j < table.Columns.Count; j )
{
table.Cell(i 1, j 1).Shape.TextFrame.TextRange.Font.Size = 12;
table.Cell(1 1, j 1).Shape.TextFrame.TextRange.Text = string.Format("[{0},{1}]", i 1, j 1);
}
}
// 保存PPT
presentation.SaveAs(fileName,
Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsDefault,
Microsoft.Office.Core.MsoTriState.msoFalse);
}
catch (Exception ex)
{
throw ex;
}
finally
{
presentation.Close();
appPowerPoint.Assistant.Visible = false;
appPowerPoint.Quit();
GC.Collect();
}
}
recole: 在保存PPT之前加入以下代码,隐藏那些边框就行了
// Microsoft.Office.Interop.PowerPoint.Slides slides
foreach (_Slide s in slides)
{
if (string.IsNullOrEmpty(s.Shapes.Placeholders[1].TextFrame.TextRange.Text))
{
s.Shapes.Placeholders[1].Visible = Microsoft.Office.Core.MsoTriState.msoFalse;
}
if (string.IsNullOrEmpty(s.Shapes.Placeholders[2].TextFrame.TextRange.Text))
{
s.Shapes.Placeholders[2].Visible = Microsoft.Office.Core.MsoTriState.msoFalse;
}
}