C#導(dǎo)出文本內(nèi)容到word文檔的方法
更新時(shí)間:2015年04月25日 16:32:09 作者:gogo
這篇文章主要介紹了C#導(dǎo)出文本內(nèi)容到word文檔的方法,涉及C#操作word文檔的相關(guān)技巧,需要的朋友可以參考下
本文實(shí)例講述了C#導(dǎo)出文本內(nèi)容到word文檔的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
<%@ Page Language="C#" AutoEventWireup="true" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
Repeater1.DataSource = new String[] { "測(cè)", "孟憲會(huì)" };
Repeater1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
String ExportFileName = "孟憲會(huì)導(dǎo)出 Word 測(cè)試";
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>測(cè)試?yán)?lt;/td><td>This is a 測(cè)試啊</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>
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
C# httpwebrequest訪問(wèn)HTTPS錯(cuò)誤處理方法
下面小編就為大家?guī)?lái)一篇C# httpwebrequest訪問(wèn)HTTPS錯(cuò)誤處理方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-01-01
C#生成單頁(yè)靜態(tài)頁(yè)簡(jiǎn)單實(shí)例
這篇文章主要介紹了C#生成單頁(yè)靜態(tài)頁(yè)簡(jiǎn)單實(shí)例,是一個(gè)非常實(shí)用的技巧,需要的朋友可以參考下2014-10-10
C#調(diào)用帶結(jié)構(gòu)體指針Dll的方法
在C#到底該如何安全的調(diào)用這樣的DLL接口函數(shù)呢?本文將詳細(xì)介紹如何調(diào)用各種參數(shù)的方法,對(duì)C#結(jié)構(gòu)體指針DLL相關(guān)知識(shí)感興趣的朋友一起看看吧2021-07-07
WPF利用RPC調(diào)用其他進(jìn)程的方法詳解
這篇文章主要給大家介紹了關(guān)于WPF利用RPC調(diào)用其他進(jìn)程的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05
C#程序啟動(dòng)項(xiàng)的設(shè)置方法
這篇文章主要為大家詳細(xì)介紹了C#程序啟動(dòng)項(xiàng)的設(shè)置方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11

