ASP. NET export formatted Word documents

Source: Internet
Author: User

You can directly copy and run the following code for testing.
ASPX code
<% @ Page Language = "C #" AutoEventWireup = "true" %>

<Script runat = "server">

Protected void Page_Load (object sender, EventArgs e)
Repeater1.DataSource = new String [] {"test", "Meng xianhui "};
Repeater1.DataBind ();

Protected void button#click (object sender, EventArgs e)
String ExportFileName = "Meng Xian will export the Word test ";
If (Request. Browser. Browser. IndexOf ("MSIE")>-1)
ExportFileName = HttpUtility. UrlEncode (ExportFileName, System. Text. UnicodeEncoding. GetEncoding ("GB2312 "));
ExportFileName + = ". doc ";
Response. Clear ();
Response. BufferOutput = true;
Response. ContentEncoding = System. Text. Encoding. GetEncoding ("GB2312 ");
Response. Charset = "GB2312 ";
Response. AppendHeader ("Content-Disposition", "attachment; filename =" + ExportFileName );
Response. ContentType = "application/ms-word ";
Page. EnableViewState = false;

System. IO. StringWriter stringWriter = new System. IO. StringWriter ();
HtmlTextWriter textWriter = new HtmlTextWriter (stringWriter );
Literal header = new Literal ();
Header. Text = "Header. Controls. Add (header );
Header. RenderControl (textWriter );
This. Repeater1.RenderControl (textWriter );
Response. write ("Response. End ();
Response. Flush ();
Public override void VerifyRenderingInServerForm (System. Web. UI. Control control)
<Html xmlns = "">
<Head id = "Head1" runat = "server">
<Title> </title>
<Form id = "form1" runat = "server">
<Asp: Repeater ID = "Repeater1" runat = "server">
<Table cellspacing = "1" cellpadding = "3" width = "96%" align = "center" border = "1">
</Table> </FooterTemplate>
<Tr> <td> tested </td> <td> This is a test </td> <td>
<% # Container. DataItem %> </td> </tr>
</Asp: Repeater>
<Asp: Button ID = "Button1" runat = "server" OnClick = "button#click" Text = "export to Word Document"/>



Author: Meng xianhui

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: 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.