First create Doc Template
Again, add this focus method to the related pages in the project
Key methods:
private String Printword () {try {string templatepath = _savedocpath + "
/template/rpmb.doc ";
The generated document path FilePath = _savedocpath + "/hzm/"; if (!
Directory.Exists (FilePath)) directory.createdirectory (FilePath); if (!
File.exists (TemplatePath)) return "error: The server does not have a template";
FilePath = FilePath + DateTime.Now.ToString ("YYYYMMDDHHMMSS") + ". Doc";
Copy a copy of File.Copy (TemplatePath, FilePath, true);
object omissing = System.Reflection.Missing.Value;
App = new Word.Application (); App.
Visible = false;
Object fileName = FilePath; doc = App.
Documents.Open (ref fileName, ref omissing, ref omissing, ref omissing, ref omissing, ref omissing,
ref omissing, ref omissing, ref omissing, ref omissing, ref omissing, ref omissing, ref omissing, ref omissing, ref omissing, ref omissing);
if (doc = = null) {return "error: Server does not open word"; //determine how to Bookmark//Add doc bookmarks if Doc. Bookmarks.exists ("KJJBJNF")) {Doc. bookmarks["KJJBJNF"]. Range.Text = DateTime.Now.ToString ("Yyyymmddhhmmss").
ToString () + "title"; } word.tables TABS1 = Doc.
Tables; if (tabs1!= null && TABS1.
Count > 0) {word.table Dt_word = tabs1[1];
String strSQL = "SELECT * from Stuinfo";
DataTable Dt_stu = runsqlreturntable (strSQL);
The default starts at line 2nd, and the header int rowidex = 2 in the 1th behavior template; foreach (DataRow row in dt_stu.rows) {Object miss = System.Reflection.Missing .
Value;DT_WORD.ROWS.ADD (Miss); The data that will be queried from the database can be//1 by cyclic loading. Add data to a cell Dt_word.cell (Rowidex, 1). Range.Text = row["No"].
ToString (); 2. Sets the alignment of the font in this cell Dt_word.cell (Rowidex, 1).
Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft; Dt_word.cell (Rowidex, 2). Range.Text = row["Name".
ToString (); Dt_word.cell (Rowidex, 3). Range.Text = row["Sex"].
ToString (); Dt_word.cell (Rowidex, 4). Range.Text = row["Age"].
ToString (); 3. Merge cell merge (rows, columns)//dt_word.cell (Rowidex, 3).
Merge (Dt_word.cell (Rowidex, 4));
rowidex++; } doc.
Save ();
Quitword (); catch (Exception ex) {QuitWord (); Return "ERROR:" + ex.
message;
Return "true"; }
3. Export can be.
Additional Source http://download.csdn.net/detail/hugaozhuang/6652495