C#導(dǎo)出文本內(nèi)容到word文檔的方法
更新時間:2015年04月25日 16:32:09 作者:gogo
這篇文章主要介紹了C#導(dǎo)出文本內(nèi)容到word文檔的方法,涉及C#操作word文檔的相關(guān)技巧,需要的朋友可以參考下
本文實例講述了C#導(dǎo)出文本內(nèi)容到word文檔的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
<%@ Page Language="C#" AutoEventWireup="true" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
Repeater1.DataSource = new String[] { "測", "孟憲會" };
Repeater1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
String ExportFileName = "孟憲會導(dǎo)出 Word 測試";
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 = "<h2>文章標(biāo)題</h2>";
Header.Controls.Add(header);
Header.RenderControl(textWriter);
this.Repeater1.RenderControl(textWriter);
Response.Write("<html xmlns:v='urn:schemas-microsoft-com:vml' xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:w='urn:schemas-microsoft-com:office:word' xmlns:m='http://schemas.microsoft.com/office/2004/12/omml' xmlns='http://www.w3.org/TR/REC-html40'><head></head><body lang=ZH-CN>" + stringWriter.ToString());
Response.End();
Response.Flush();
}
public override void VerifyRenderingInServerForm(System.Web.UI.Control control)
{
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table cellspacing="1" cellpadding="3" width="96%" align="center" border="1">
</HeaderTemplate>
<FooterTemplate>
</table></FooterTemplate>
<ItemTemplate>
<tr><td>測試?yán)?lt;/td><td>This is a 測試啊</td><td>
<%#Container.DataItem%></td></tr>
</ItemTemplate>
</asp:Repeater>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="導(dǎo)出成Word文檔" />
</form>
</body>
</html>
希望本文所述對大家的C#程序設(shè)計有所幫助。
相關(guān)文章
C# httpwebrequest訪問HTTPS錯誤處理方法
下面小編就為大家?guī)硪黄狢# httpwebrequest訪問HTTPS錯誤處理方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01
C#調(diào)用帶結(jié)構(gòu)體指針Dll的方法
在C#到底該如何安全的調(diào)用這樣的DLL接口函數(shù)呢?本文將詳細(xì)介紹如何調(diào)用各種參數(shù)的方法,對C#結(jié)構(gòu)體指針DLL相關(guān)知識感興趣的朋友一起看看吧2021-07-07
WPF利用RPC調(diào)用其他進(jìn)程的方法詳解
這篇文章主要給大家介紹了關(guān)于WPF利用RPC調(diào)用其他進(jìn)程的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05

