可直接拷贝运行下面的代码测试
ASPX 代码
<%@ 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 = "孟宪会导出 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>文章标题</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>测试啦</td><td>This is a 测试啊</td><td>
<%#Container.DataItem%></td></tr>
</ItemTemplate>
</asp:Repeater>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="导出成Word文档" />
</form>
</body>
</html>
相关文章
- 网页导出成word文档的默认视图方式问题
- MyEclipse工具开发常用快捷键 普通: Ctrl+Shift+L 显示所有快捷键 Ctrl+K 参照选中的词(Word)快速定位到下一个 Ctrl+Shift+K 参照选中的词(Word)快速定位到上一个 Ctrl+O 快速显示OutLine Ctrl+T 快速显示当前类的继承结构 Ctrl+Shift+X 把选中文本全部改为大写 Ctrl+Shift+Y 把选中文本全部改为小写 Ctrl+Shift+F 按照当前的配置文件格式化源代码 Ctrl+L 弹出行定位框以便定位到某一行 Ctrl+Numpad_Divide Ctrl+/(小键盘) 使代码折叠可用 Ctrl+Numpad_Multify Ctrl+*(小键盘) 展开当前类中的所有代码 Ctrl+Shift+Numpad_Divide Ctrl+Shift+/(小键盘) 折叠所有,如果不能全部成功折叠,将光标移到文档最后试着或多按几次折叠当前类中的所有代码 Ctrl+1 快速修正,需要导包的时候出现导包提示界面, Ctrl+Q 定位到
- Mysql数据库导出数据字典文档Word或者HTML的3个工具
- 如何在模板中生成包含现有文本的word文档docx,但是通过asp.net Web表单填写值?
- 将Word文档转化为HTML格式的文档
- asp.net Excel 导入导出错误System.Data.OleDb.OleDbException: 外部表不是预期的格式。
- asp.net下用Aspose.Words for .NET动态生成word文档中的图片或水印的方法(转)
- php导出word格式数据的代码(转)
- 如何在保留格式的同时使用Visual Basic将HTML格式的字符串插入Microsoft Word文档?
- php导出word文档与excel电子表格的简单示例代码