Asp.net generates and downloads word in two ways

Source: Internet
Author: User

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.