Asp.net generates and downloads word in two ways
First, directly generate a word to download Response from a webpage. addHeader ("Content-Disposition", "attachment; filename =" + HttpUtility. urlEncode ("wordname.doc", Encoding. UTF8); Response. contentType = "application/ms-word"; EnableViewState = false; var tw = new StringWriter (); var hw = new HtmlTextWriter (tw); tb1.RenderControl (hw ); // tb1 is the idResponse of the webpage table (div. write (tw. toString (); Response. end (); the second type of word template directly downloads string sPath = System. IO. path. G EtDirectoryName (this. page. request. physicalPath); string a = @ "\ wendang \ sales contract .doc"; string tmpFileName = sPath + a; // @ "E: \ chain medicine \ YK_ERP_HNCC \ ERPCC \ Order \ wendang \ C ++ programming 04737 Questions and Answers 2008 ~ 2012. doc "; # region // method 1 FileInfo tmpFI = new FileInfo (tmpFileName); Response. clear (); Response. clearHeaders (); Response. buffer = false; Response. appendHeader ("Content-Disposition", "attachment; filename =" + HttpUtility. urlEncode (Path. getFileName (tmpFileName), System. text. encoding. UTF8); Response. appendHeader ("Content-Length", tmpFI. length. toString (); Response. contentType = "application/octet-stream"; Response. writeFile (tmpFileName); Response. flush (); Response. end (); // method 2 Response. contentType = "Application/msword"; Response. writeFile (tmpFileName); Response. end (); # endregion