读书人

用repeater控件导出word后只显示HTM

发布时间: 2012-05-16 23:40:10 作者: rapoo

用repeater控件导出word后,只显示HTML代码
前台:
<table class="usertableborder" cellspacing="1" cellpadding="3" width="96%" align="center" border="0">

<asp:Repeater ID="RepeatUser" runat="server" >
<HeaderTemplate>
<tr>
<th>选择</th>
<th>员工编号</th>
<th>员工名称</th>
<th>合同编号</th>
<th>手机</th>
<th>更新</th>

</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td class="usertablerow1"><input type="checkbox" name="chkUserList" id="chkUser" value='<%# Eval("Id") %>' /></td>
<td class="usertablerow1"><%#Eval("ID") %></td>
<td class="usertablerow1"><%#Eval("Name") %></td>
<td class="usertablerow1"><%#Eval("Contract") %></td>
<td class="usertablerow1"><%#Eval("MobilePhone") %></td>
<td class="usertablerow1"><a href=AddUser.aspx?id=<%#Eval("ID") %>>更新</a></td>
</tr>
</ItemTemplate>
</asp:Repeater>

</table>
<font size=2><p align=center><asp:Literal ID="RecordCount" runat="server"></asp:Literal>条记录
共有<asp:Literal ID="PageCount" runat="server"></asp:Literal>页
当前第<asp:Literal ID="Pageindex" runat="server"></asp:Literal>页
<asp:HyperLink ID="FirstPage" runat="server" Text="首页"></asp:HyperLink>
<asp:HyperLink ID="PrevPage" runat="server" Text="上一页"></asp:HyperLink>
<asp:HyperLink ID="NextPage" runat="server" Text="下一页"></asp:HyperLink>
<asp:HyperLink ID="LastPaeg" runat="server" Text="尾页"></asp:HyperLink>
跳转到<asp:Literal ID="Literal1" runat="server"></asp:Literal>页
</p></font>
<table cellspacing="1" cellpadding="3" width="96%" align="center" border="0">

</table>




后台:
#region 导出数据函数
/// <summary>
/// 导出数据函数
/// </summary>
/// <param name="FileType">导出文件MIME类型</param>
/// <param name="FileName">导出文件的名称</param>
private void Export(String FileType, String FileName)
{
Response.Clear();
Response.BufferOutput = true;
//设定输出字符集
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AppendHeader("Content-Disposition", "attachment;filename="
+ HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8));


//设置输出流HttpMiME类型(导出文件格式)
Response.ContentType = FileType;
//关闭ViewState
Page.EnableViewState = false;
System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("ZH-CN", true);
System.IO.StringWriter stringWriter = new System.IO.StringWriter(cultureInfo);
HtmlTextWriter textWriter = new HtmlTextWriter(stringWriter);
this.RepeatUser.RenderControl(textWriter);
//把HTML写回游览器
Response.Write(stringWriter.ToString());
Response.End();
Response.Flush();
}
#endregion

#region 重写
//确认在运行时为指定的 ASP.NET 服务器控件呈现在 HtmlForm 控件中。
//(检验Asp.Net服务器空间是否呈现在HTMLForm控件中)
public override void VerifyRenderingInServerForm(System.Web.UI.Control control)
{
}
#endregion

//#region 导出成word
//protected void btnInputWord_Click(object sender, EventArgs e)
//{
// Export("application/ms-word", "员工评分表.doc");
//}
//#endregion

//#region 导出成Excel
//protected void btnInputExcel_Click(object sender, EventArgs e)
//{
// Export("application/ms-excel", "员工评分表.xls");
//}
//#endregion

protected void Button1_Click(object sender, EventArgs e)
{
Export("application/ms-word", "员工评分表.doc");
}




导出后word内容:
<tr>
<th>选择</th>
<th>员工编号</th>
<th>员工名称</th>
<th>合同编号</th>
<th>手机</th>
<th>更新</th>

</tr>

<tr>
<td class="usertablerow1"><input type="checkbox" name="chkUserList" id="chkUser" value='18' /></td>
<td class="usertablerow1">18</td>
<td class="usertablerow1">张三</td>
<td class="usertablerow1"></td>
<td class="usertablerow1">15011111111</td>
<td class="usertablerow1"><a href=AddUser.aspx?id=18>更新</a></td>
</tr>

<tr>
<td class="usertablerow1"><input type="checkbox" name="chkUserList" id="chkUser" value='19' /></td>
<td class="usertablerow1">19</td>
<td class="usertablerow1">付建勇</td>
<td class="usertablerow1"></td>
<td class="usertablerow1"></td>
<td class="usertablerow1"><a href=AddUser.aspx?id=19>更新</a></td>
</tr>

<tr>
<td class="usertablerow1"><input type="checkbox" name="chkUserList" id="chkUser" value='20' /></td>


<td class="usertablerow1">20</td>
<td class="usertablerow1">邱毅超</td>
<td class="usertablerow1"></td>
<td class="usertablerow1"></td>
<td class="usertablerow1"><a href=AddUser.aspx?id=20>更新</a></td>
</tr>
网页是嵌套在母版页内的,有CSS样式,请问如何让repeater导出后有一个起码的表格可以看,而不是长长的代码;



[解决办法]
解决乱码方法

HTML code
<%@ 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)  {    Response.Clear();    Response.BufferOutput = true;    //设定输出字符集    Response.ContentEncoding = System.Text.UnicodeEncoding.GetEncoding("GB2312");    Response.Charset = "GB2312";    Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("xx.doc", System.Text.Encoding.UTF8));    //设置输出流HttpMiME类型(导出文件格式)    Response.ContentType = "application/ms-word";    //关闭ViewState    Page.EnableViewState = false;    System.IO.StringWriter stringWriter = new System.IO.StringWriter();    HtmlTextWriter textWriter = new HtmlTextWriter(stringWriter);    this.Repeater1.RenderControl(textWriter);    //把HTML写回游览器    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="导出" />    </form></body></html> 

读书人网 >asp.net

热点推荐