Manipulating Word Components-Spire.doc Introduction
"Bo Master" anti-bone Aberdeen "original address" http://www.cnblogs.com/liqingwen/p/5898368.html
Order
This plan in a few days simple introduction under the component Spire.xls, suddenly found that the park friend took the lead in releasing an article, since the XLS has appeared, in order to avoid the suspicion of plagiarism, Bo Master can only take the first step to use Spire.doc simple introduction of the DOC operation, the following is through the WinForm program execution code to complete the introduction.
Native environment: WIN10 x64, VS 2015, MS Office 2016.
Directory
- NuGet Package Installation Dll files
- The beginning does not say "Hello world", read all poetry also in vain
- Document Content Retrieval
- Document content Substitution
Introduced
This is one of the components developed by E-iceblue company Spire.doc, which is designed for developers to create, read, write, convert and print Word document files conveniently, and it doesn't require you to install MS Office to work with Word.
One, NuGet package installation Dll files
Figure 1-1 Opening the NuGet Package Manager
Figure 1-2 3 more reference files after installation
Second, the beginning does not say "Hello world", read all poetry also in vain
1. Create a blank "demo1.docx" file first
Figure 2-1
2. Write a few lines of code
1 Public Partial classForm1:form2 {3 PublicForm1 ()4 {5 InitializeComponent ();6 }7 8 Private voidButton1_Click (Objectsender, EventArgs e)9 {Ten //OpenWord Document One varDocument =NewDocument (@"Demo1.docx", fileformat.docx); A - //take the first part - varSection = document. sections[0]; the - //take the first paragraph - varParagraph = section. paragraphs[0]; - + //Append String -Paragraph. AppendText ("Hello world!"); + A //Save as a. docx file at Const stringFileName =@"Demo1-1.docx"; - document. SaveToFile (FileName, fileformat.docx); - - //Start the file - Process.Start (fileName); - } in}
Figure 2-2
"Remarks" don't forget to introduce namespaces, OH: using Spire.doc;
Above is to add "Hello world!" to an empty Word document, this time to create a new one that contains "Hello world!" The contents of the document. Of course the effect is the same as figure 2-2.
1 Private voidButton1_Click (Objectsender, EventArgs e)2 {3 //Create a Word document4 varDocument =NewDocument ();5 6 //Create a new section7 varSection =document. AddSection ();8 9 //create a new paragraphTen varParagraph =Section . Addparagraph (); One A //Append String -Paragraph. AppendText ("Hello world!"); - the //Save as a. doc file - Const stringFileName =@"Demo1-1.doc"; - document. SaveToFile (FileName, fileformat.doc); - + //Start the file - Process.Start (fileName); +}
Third, document content retrieval
First in the "Demo2.docx" in the "Pipa line", the start of the text box in the input "silence wins sound" for retrieval.
1 Private voidButton1_Click (Objectsender, EventArgs e)2 {3 //Load Demo2.docx4 varDocument =NewDocument (@"Demo2.docx", fileformat.docx);5 6 //find all matching strings7textselection[] Textselections = document. Findallstring ( This. TextBox1.Text,false,false);8 9 //Modify Background colorTen foreach(TextSelection selectioninchtextselections) One { ASelection. Getasonerange (). Characterformat.textbackgroundcolor =Color.gray; - } - the //Save File - Const stringFileName =@"Demo2-1.docx"; - document. SaveToFile (FileName, fileformat.docx); - + //Start the file - Process.Start (fileName); +}
Figure 3-1
Iv. Replacement of document content
Let's try to make a simple change to the code on the basis of three.
1 Document. Replace (this this. TextBox2.Text,false,false);
Figure 4-1
[. NET] Operations Word Components-Spire.doc Introduction