You need to reference the DLL for Office, in the attachment
Paste the core code (reproduced):
Microsoft.office.interop.word._application Appword = new Microsoft.Office.Interop.Word.ApplicationClass (); Microsoft.office.interop.word._document docfile = null; try {appword.visible = false; Object objtrue = true; Object objfalse = false; Object objtemplate = Server.MapPath (@ "Dot//123.dot");//template Path Object Objdoctype = MICROSOFT.OFFICE.INTEROP.WORD.W Ddocumenttype.wdtypedocument; Docfile = APPWORD.DOCUMENTS.ADD (ref objtemplate, ref objfalse, ref objdoctype, ref objtrue); First step to generate Word Document//define bookmark Variable Object obdd_name = "Dd_name";//Name Object obdd_sex = "Dd_sex";//Sex Do not object obdd_age = "Dd_age";//Age Object Obdd_birthday = "Dd_birthday"; Date of birth Object obdd_nation = "Dd_nation"; National Object obdd_native = "Dd_native"; Hometown//Second step reading data, populating data set//SqlDataReader Dr = xxxxx;//read out the data set//third step to bookmark the value Assigns a bookmark a value of DocFile.Bookmarks.get_Item (ref obdd_name). Range.Text = "name"; Name DocFile.Bookmarks.get_Item (ref obdd_sex). Range.Text = "gender"; DocFile.Bookmarks.get_Item (ref obdd_age). Range.Text = "Age"; The fourth step generates word DateTime dt = DateTime.Now; Object filename = Server.MapPath ("dot//") + "table" + dt. Ticks.tostring () + ". Doc"; Object miss = System.Reflection.Missing.Value; Docfile.saveas (ref filename, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref Miss, ref miss, ref miss, ref miss, ref miss, ref miss); Object missingvalue = Type.Missing; Object donotsavechanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges; Docfile.close (ref donotsavechanges, ref missingvalue, ref missingvalue); Appword.quit (ref miss, ref miss, ref miss); Docfile = null; Appword = null; }catch (Exception ex) {//catch exception, if an exception occurs, empties the instance, exits word, and frees the resource string AA = E.tostring (); Object miss = System.Reflection.Missing.Value; Object missingvalue = Type.Missing; Object donotsavechanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges; Docfile.close (ref donotsavechanges, ref missingvalue, ref missingvalue); Appword.quit (ref miss, ref miss, ref miss); Docfile = null; Appword = null; Throw ex; }
After the server is generated, attach a simple stream to load the code:
1 stringFileName ="table. doc";//file name saved by the client2 stringFilePath = Server.MapPath ("~/download//word//table1.doc");//Path3 4 //download a file as a stream of characters5FileStream fs =NewFileStream (FilePath, FileMode.Open);6 byte[] bytes =New byte[(int) fs. Length];7Fs. Read (Bytes,0, Bytes. Length);8 FS. Close ();9Response.ContentType ="Application/octet-stream";Ten //notifies the browser to download a file instead of opening it OneResponse.AddHeader ("content-disposition","attachment; Filename="+Httputility.urlencode (FileName, System.Text.Encoding.UTF8)); A response.binarywrite (bytes); - Response.Flush (); -Response.End ();
Attach the various Office version DLL libraries:
Each version of the Microsoft.Office.Interop.Word library. rar