ASP.NET导出带格式的Word文档
可直接拷贝运行下面的代码测试
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="">
<head id="head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<: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>
作者 孟宪会
上一篇: 干企业真的不容易
推荐阅读
-
asp.net实现导出DataTable数据到Word或者Excel的方法
-
使用PHP导出Word文档的原理和实例
-
python-docx修改已存在的Word文档的表格的字体格式方法
-
word2007格式的文档在word2003中打不开
-
编辑Word文档时格式混乱出错等排版问题的解决方法
-
winform 替换word文档中的字段(包含图片添加),生成导出PDF文件(也可是word文件)
-
Java实现用Freemarker完美导出word文档(带图片)
-
php导出word文档与excel电子表格的简单示例代码
-
ASP.NET Core 2.0集成Office Online Server(OWAS)实现办公文档的在线预览与编辑(支持word\excel\ppt\pdf等格式)
-
用“通配符”批量替换Word文档中的日期格式