c#如何将控件text内容生成到excel文件中
1个回答
2015-03-25
展开全部
这个很简单啦,我在项目中使用了,你把我下面这段代码copy到你项目中:
注解:rpt_pro是Repeater控件名,你改成你自己的,另外,你在页面中加一个导出按钮
<asp:Button ID="btnInputExcel" runat="server" Width="50" Text="导 出" onclick="btnInputExcel_Click"/>
/// <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);
rpt_pro.RenderControl(textWriter);
//把HTML写回游览器
Response.Write(stringWriter.ToString());
Response.End();
Response.Flush();
}
//确认在运行时为指定的 ASP.NET 服务器控件呈现在 HtmlForm 控件中。
//(检验Asp.Net服务器空间是否呈现在HTMLForm控件中)
public override void VerifyRenderingInServerForm(System.Web.UI.Control control)
{
}
#region 导出成Excel
protected void btnInputExcel_Click(object sender, EventArgs e)
{
Export("application/ms-excel", "员工评分表.xls");
}
#endregion
导出成word的话,你可以另外加一个按钮
#region 导出成Excel
protected void btnInputWord_Click(object sender, EventArgs e)
{
Export("application/ms-word", "员工评分表.doc");
}
#endregion
注解:rpt_pro是Repeater控件名,你改成你自己的,另外,你在页面中加一个导出按钮
<asp:Button ID="btnInputExcel" runat="server" Width="50" Text="导 出" onclick="btnInputExcel_Click"/>
/// <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);
rpt_pro.RenderControl(textWriter);
//把HTML写回游览器
Response.Write(stringWriter.ToString());
Response.End();
Response.Flush();
}
//确认在运行时为指定的 ASP.NET 服务器控件呈现在 HtmlForm 控件中。
//(检验Asp.Net服务器空间是否呈现在HTMLForm控件中)
public override void VerifyRenderingInServerForm(System.Web.UI.Control control)
{
}
#region 导出成Excel
protected void btnInputExcel_Click(object sender, EventArgs e)
{
Export("application/ms-excel", "员工评分表.xls");
}
#endregion
导出成word的话,你可以另外加一个按钮
#region 导出成Excel
protected void btnInputWord_Click(object sender, EventArgs e)
{
Export("application/ms-word", "员工评分表.doc");
}
#endregion
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询