33
44using ( FileStream inputFileStream = new FileStream ( Path . GetFullPath ( "Data/Template.docx" ) , FileMode . Open , FileAccess . ReadWrite ) )
55{
6- //Open the template Word document.
7- using ( WordDocument document = new WordDocument ( inputFileStream , FormatType . Docx ) )
8- {
9- //Retrieve the first section of the document.
10- IWSection section = document . LastSection ;
11- //Get the first table in the section.
12- WTable table = section . Body . Tables [ 0 ] as WTable ;
13- //Access the specific cells by their indices.
14- WTableCell cell1 = table [ 1 , 1 ] ;
15- WTableCell cell2 = table [ 2 , 2 ] ;
16- //Clear the contents of the first cell.
17- cell1 . ChildEntities . Clear ( ) ;
18- //Add a new paragraph with content to the first cell.
19- cell1 . AddParagraph ( ) . AppendText ( "Adventure" ) ;
20- //Clear the contents of the second cell.
21- cell2 . ChildEntities . Clear ( ) ;
22- //Add a new paragraph with content to the second cell.
23- cell2 . AddParagraph ( ) . AppendText ( "Cycle" ) ;
24- //Save the modified document.
25- using ( FileStream outputFileStream = new FileStream ( Path . GetFullPath ( "Output/Result.docx" ) , FileMode . Create , FileAccess . Write ) )
26- {
27- document . Save ( outputFileStream , FormatType . Docx ) ;
28- }
29- }
6+ //Open the template Word document.
7+ using ( WordDocument document = new WordDocument ( inputFileStream , FormatType . Docx ) )
8+ {
9+ //Retrieve the first section of the document.
10+ IWSection section = document . LastSection ;
11+ //Get the first table in the section.
12+ WTable table = section . Body . Tables [ 0 ] as WTable ;
13+ //Access the instance of the cell (second row, second cell).
14+ WTableCell cell1 = table [ 1 , 1 ] ;
15+ //Access the instance of the cell (third row, third cell).
16+ WTableCell cell2 = table [ 2 , 2 ] ;
17+ //Clear the contents of the cell (second row, second cell).
18+ cell1 . ChildEntities . Clear ( ) ;
19+ //Add a new paragraph with content to the cell (second row, second cell).
20+ cell1 . AddParagraph ( ) . AppendText ( "Adventure" ) ;
21+ //Clear the contents of the cell (third row, third cell).
22+ cell2 . ChildEntities . Clear ( ) ;
23+ //Add a new paragraph with content to the cell (third row, third cell).
24+ cell2 . AddParagraph ( ) . AppendText ( "Cycle" ) ;
25+ //Save the modified document.
26+ using ( FileStream outputFileStream = new FileStream ( Path . GetFullPath ( "Output/Result.docx" ) , FileMode . Create , FileAccess . Write ) )
27+ {
28+ document . Save ( outputFileStream , FormatType . Docx ) ;
29+ }
30+ }
3031}
0 commit comments