How to use OpenXML to operate PPT documents in C #
Today, I will talk to you about how to use OpenXML to operate PPT documents in C#. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
OpenXML is an official Microsoft open source operation Word, Excel and PPT SDK, developed with C #, using this kind of library can easily operate the three main types of office documents, and do not need to install Office programs.
The following code replaces some text and pictures in the document to open an PPT document:
The open method of PresentationDocument. _ outfile is the path of the file, and then opens it in a writable manner.
Using (PresentationDocument presentationDocument = PresentationDocument.Open (_ outfile, true))
{
Try
{
/ / SlideParts is every presentation page in the ppt document
SlideParts _ sdataParts = presentationDocument.PresentationPart.SlideParts
/ / get the first document presentation page
Var _ sdataPart = _ sdataParts.FirstOrDefault ()
/ / find where the text in the demo page contains forecasthour
Var chageText = _ sdataPart.Slide.Descendants () .FirstOrDefault (p = > p.Text.Contains ("forecasthour"))
/ / replace the text with the current date
ChageText.Text = _ dt.ToString ("yyyy-MM-dd HH:mm")
/ / find the list of pictures in the demo page
Var _ imgParts = _ sdataPart.ImageParts
/ / find the picture whose name is image7.png, and replace the file from the outside
ImagePart _ firImgPart = _ imgParts.FirstOrDefault (p = > p.Uri.ToString () .Contains ("image7.png"))
If (_ firImgPart! = null)
{
Using (FileStream fs = File.Open ("Picture File path", FileMode.Open))
{
/ / replace the picture file
_ firImgPart.FeedData (fs)
}
}
}
Catch (Exception ex1)
{
}
}
After reading the above, do you have any further understanding of how to use OpenXML to manipulate PPT documents in C #? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.