Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use OpenXML to operate Word documents in C #

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

C # how to use OpenXML to operate Word documents, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

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 opens an word document:

/ / WordprocessingDocument opens a document, and _ filePath is the path to the document

Using (WordprocessingDocument _ doc = WordprocessingDocument.Open (_ filePath, true))

{

/ / the body of the document

Body bod = _ doc.MainDocumentPart.Document.Body

/ / the content section of the document

_ mainDocPart = _ doc.MainDocumentPart

}

Click the options in the word document to open the word development tools mode, and you can add some tags to the document to develop the content in the word document.

Check the development tools option for word documents

Development tools menu bar for word documents

The following is a brief description of how to find a picture tag in a document and replace it.

First, in the word document, add a picture tag, use the development tool = "control =" picture content control, in the pop-up window, add a unique tagged string in the tag bar

After adding the control, in the C# program, through the Linq search of Descendants, find all the tags in the document, and then replace the external pictures according to the tags.

Using (WordprocessingDocument _ doc = WordprocessingDocument.Open (_ filePath, true))

{

/ / the body of the document

Body bod = _ doc.MainDocumentPart.Document.Body

/ / the content section of the document

_ mainDocPart = _ doc.MainDocumentPart

/ / find all markup elements

Var _ sdtList = bod.Descendants ()

/ / find the elements in the document according to the name of the tag

Var _ evEle= _ sdtList. FirstOrDefault (p = > p.SdtProperties.GetFirstChild (). Val.ToString (). Contains ("unique tag"))

/ / find Blip, which is the tag pointing to the file

DocumentFormat.OpenXml.Drawing.Blip ImageBlip = _ evEle.Descendants () .FirstOrDefault ()

/ / find the picture from the document

Var imagePart = (ImagePart) _ mainDocPart.GetPartById (ImageBlip.Embed.Value)

/ / replace with an external picture, and _ basePath is the path of the external image

Using (FileStream fs = File.Open (_ basePath, FileMode.Open))

{

ImagePart.FeedData (fs)

}

}

After reading the above, have you mastered how to use OpenXML to manipulate Word documents in C #? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report