The main function is to generate a word report document based on the word template. Pay attention to reference Interop. Word. dll;
First, you must generate a word program object.
Word. Application app = new Word. Application ();
Generate a new file framework based on the template file
File. Copy (TemplateFile, FileName );
Generate a jsonnet object
Ord. Document doc = new Word. Document ();
Open a new document
Doc = app. Documents. Open (ref Obj_FileName, ref missing, ref ReadOnly, ref missing,
Ref missing,
Ref missing, ref Visible,
Ref missing,
Ref missing );
Doc. Activate ();
Position the cursor to the new bookmarks (the bookmarks are defined in the template). The following code outputs a line at the cursor position and press Enter.
// Move the cursor to the bookmarks
For (int bookIndex = 0; bookIndex <5; bookIndex ++)
{
Object BookMarkName = "BookMark" + bookIndex. ToString ();
Object what = Word. WdGoToItem. wdGoToBookmark;
Doc. ActiveWindow. Selection. GoTo (ref what, ref missing, ref missing, ref BookMarkName );
Doc. ActiveWindow. Selection. TypeText ("civilized unit" + bookIndex. ToString () + "zaddd 25 Emy ");
Doc. ActiveWindow. Selection. TypeParagraph ();
}
Close the doc object after the output.
Object IsSave = true;
Doc. Close (ref IsSave, ref missing, ref missing );
The complete example code is as follows:
Using System;
Using System. IO;
Using System. Data;
Using System. Configuration;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
}
Protected void button#click (object sender, EventArgs e)
{
Word. Application app = new Word. Application ();
// Template File
String TemplateFile = @ "D: \ Mywork \ ExcelReportsServer \ ReportServer \ Tempalte \ SmallList.doc ";
// Generate a new file with a template Style
String filename = @ "C: \ Documents and Settings \ Administrator \ Desktop \" + datetime. Now. tostring ("yyyymmddhhmmssfffffff") + ". Doc ";
// Copy the template file to the new file
File. Copy (templatefile, filename );
Word. Document Doc = new word. Document ();
Object obj_filename = filename;
Object visible = false;
Object readonly = false;
Object missing = system. reflection. Missing. value;
// Open the file
Doc = app. Documents. Open (ref Obj_FileName, ref missing, ref ReadOnly, ref missing,
Ref missing,
Ref missing, ref Visible,
Ref missing,
Ref missing );
Doc. Activate ();
// Move the cursor to the bookmarks
For (int bookIndex = 0; bookIndex <5; bookIndex ++)
{
Object bookmarkname = "bookmark" + bookindex. tostring ();
Object what = word. wdgotoitem. wdgotobookmark;
Doc. activewindow. selection. Goto (ref what, ref missing, ref missing, ref bookmarkname );
Doc. activewindow. selection. typetext ("civilized unit" + bookindex. tostring () + "zaddd 25 Emy ");
Doc. activewindow. selection. typeparagraph ();
}
Object issave = true;
Doc. Close (ref issave, ref missing, ref missing );
Response. Write ("<script language = 'javascript '> alert ('template generated successfully! ') </SCRIPT> ");
}
}
Appendix:
Cursor to the position of the bookmarks title
Object bookmarkname = "title ";
Object what = word. wdgotoitem. wdgotobookmark;
Doc. activewindow. selection. Goto (ref what, ref missing, ref missing, ref bookmarkname );
Write text at the current cursor
Doc. activewindow. selection. typetext ("change notification ");
Current cursor line feed
Doc. activewindow. selection. typeparagraph ();
Current cursor setting format (for example, Alignment) Doc. ActiveWindow. Selection. ParagraphFormat. Alignment = Word. WdParagraphAlignment. wdAlignParagraphRight;
Note that ParagraphFormat is used to set the font format.