Instance sharing of exporting ppt file data in ASP. NET
- 2021-07-09 07:53:39
- OfStack
In the first period of time, due to work needs, it is necessary to increase the export and download of ppt data. It is found that there are not many data in this area on the network, and some related data are found piecemeal. After my own experiments, the related functions are finally completed. At the request of bloggers, I would like to share my experience here, and I hope you can point out the disadvantages.
Before you do this, you need to add the relevant reference Microsoft. Office. Interop. PowerPoint. dll.
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
Operation PPT code is as follows:
Copy code
public void createPPT()
{
try
{
//ppt Storage path
string path = string.Format("{0}/{1}.ppt", Server.MapPath("."), DateTime.Now.Ticks.ToString());
//ppt Referenced template path
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();
// Delete if it already exists
if (File.Exists((string)path))
{
File.Delete((string)path);
}
Object Nothing = Missing.Value;
// Apply template
MyPres = MyApp.Presentations.Open(MyTemplateFile, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse);
MyPresSet = MyApp.Presentations;
objSlides = MyPres.Slides;
// Create the 1 Zhang PPT ppLayoutTitle Specify Template Home Page
MySlide = objSlides.Add(1, PowerPoint.PpSlideLayout.ppLayoutTitle);
// Add 1 Line text ( 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; // Set the color of the word
objTextRng.Font.Size = 42; // Size
// Create the 2 Zhang PPT ppLayoutBlank Specify an untitled page
MySlide = objSlides.Add(2, PowerPoint.PpSlideLayout.ppLayoutBlank);
// Insert a picture
MySlide.Shapes.AddPicture("1.jpg", MsoTriState.msoFalse, MsoTriState.msoTrue, 110, 140, 500, 300);
// Create the 3 Zhang PPT ppLayoutTitleOnly Specify only title pages
MySlide = objSlides.Add(3, PowerPoint.PpSlideLayout.ppLayoutTitleOnly);
objTextRng = MySlide.Shapes[1].TextFrame.TextRange;
objTextRng.Text = " Directory ";
objTextRng.Font.Size = 32;
// Insert a picture
MySlide.Shapes.AddPicture("1.jpg", MsoTriState.msoFalse, MsoTriState.msoTrue, 110, 140, 500, 300);
// Create the 4 Zhang PPT
MySlide = objSlides.Add(3, PowerPoint.PpSlideLayout.ppLayoutBlank);
// Add 1 Tables
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);
}
}
// Save format
PowerPoint.PpSaveAsFileType format = PowerPoint.PpSaveAsFileType.ppSaveAsDefault;
// Content preservation
MyPres.SaveAs(path, format, Microsoft.Office.Core.MsoTriState.msoFalse);
// Shut down excelDoc Document object
MyPres.Close();
// Shut down excelApp Component object
MyApp.Quit();
}