以下导入代码应该是gv1表数据,但导出的结果却是整个网页,请各位大侠指点下(注:我的gridview是做在用户控件里的,用于sharepoint网站)protected void btn_Excel_Click(object sender, EventArgs e)
{
try
{
string xlsFile = "Excel";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(xlsFile, System.Text.Encoding.UTF8) + ".xls"); HttpContext.Current.Response.Charset = "GB2312";
Response.ContentEncoding = Encoding.GetEncoding("UTF-8");
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
this.gv1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
catch (Exception ex)
{
Response.Write("<script>alert('" + ex.Message + "')</script>");
}
}
{
try
{
string xlsFile = "Excel";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(xlsFile, System.Text.Encoding.UTF8) + ".xls"); HttpContext.Current.Response.Charset = "GB2312";
Response.ContentEncoding = Encoding.GetEncoding("UTF-8");
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
this.gv1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
catch (Exception ex)
{
Response.Write("<script>alert('" + ex.Message + "')</script>");
}
}
解决方案 »
- dataset数据源这样用有错吗
- 搜索时出现:出错了:常规网络错误。请检查您的网络文档。¥¥¥那应该怎么解决呢?
- 散尽所有分,求视频发布服务器,请大家帮忙了
- 请问如何实现地图漫游功能就是鼠标点击地图,地图放大的效果??在线等待
- 一道关于asp.net的选择题,求解???
- asp.net(vb)中登陆的问题
- 怎样在需要输入多行文字时又要去掉TextBox或TextArea右边的上下滚动条??急急!!!
- 关于repeater和ajax的问题?急
- 第一次用水晶报表,请教个小问题?
- 能不能利用datagrid实现客户端输入?
- Application_Error
- 初学者?JS日历控件如何在VS2008中使用
C# code45 private void Export(string FileType, string FileName)
46 {
47 GridView.AllowPaging = false;
48 GridView.AllowSorting = false;
49
50 BindData();
51
52 Response.Clear();
53 Response.Charset = "GB2312";
54 Response.ContentEncoding = System.Text.Encoding.UTF7;
55 Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
56 Response.ContentType = FileType;
57 this.EnableViewState = false;
58 StringWriter tw = new StringWriter();
59 HtmlTextWriter hw = new HtmlTextWriter(tw);
60 GridView.RenderControl(hw);
61 Response.Write(tw.ToString());
62 Response.End();
63 }
64
65 public override void VerifyRenderingInServerForm(Control control)
66 {
67
68 }
namespace Common
{
/// <summary>
/// 数据绑定委托
/// </summary>
public delegate void delegateBindData(); /// <summary>
/// Excel处理类
/// </summary>
public class ExcelProcess
{
public delegateBindData delbinddata; /// <summary>
/// 绑定数据
/// </summary>
public void BindData()
{
if(delbinddata!=null)
delbinddata();
} /// <summary>
/// GridView导出数据到Excel
/// </summary>
/// <param name="Charset">输出流的HTTP字符集</param>
/// <param name="ContentEncoding">输出流的HTTP字符集编码</param>
/// <param name="FileName">导出的文件名</param>
/// <param name="ContentType">文件类型 默认application/ms-excel</param>
/// <param name="gvList">GridView控件</param>
public void ToExcel(string Charset, Encoding ContentEncoding, string FileName, string ContentType, System.Web.UI.WebControls.GridView gvList)
{
if (string.IsNullOrEmpty(Charset))
Charset = "GB2312";
if (ContentEncoding == null)
ContentEncoding = Encoding.GetEncoding("GB2312");
if(string.IsNullOrEmpty(ContentType))
ContentType = "application/ms-excel"; System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.Buffer = true;
System.Web.HttpContext.Current.Response.Charset = Charset;
System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + FileName + "");
// 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
System.Web.HttpContext.Current.Response.ContentEncoding = ContentEncoding;
System.Web.HttpContext.Current.Response.ContentType = ContentType;//设置输出文件类型为excel文件。
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); gvList.AllowPaging = false;
BindData(); gvList.RenderControl(oHtmlTextWriter);
System.Web.HttpContext.Current.Response.Output.Write(oStringWriter.ToString());
System.Web.HttpContext.Current.Response.Flush();
System.Web.HttpContext.Current.Response.End(); gvList.AllowPaging = true;
BindData();
}
/// <summary>
/// GridView导出数据到Excel
/// </summary>
/// <param name="FileName">导出的文件名</param>
/// <param name="gvList">GridView控件</param>
public void ToExcel(string FileName, System.Web.UI.WebControls.GridView gvList)
{
ToExcel(string.Empty, null, FileName, string.Empty, gvList);
}
}
}
/// <summary>
/// 此方法不可少(以确认在运行时为指定的ASP.NET 服务器控件呈现HtmlForm控件)
/// </summary>
public override void VerifyRenderingInServerForm(Control control)
{ }
protected void btnExportDetail_Click(object sender, EventArgs e)
{
ExcelProcess ep = new ExcelProcess();
ep.delbinddata = new delegateBindData(BindData);
ep.ToExcel("UTF-8", Encoding.UTF8, "test.xls", "application/ms-excel", gvDetail);
}
public void BindData()
{
//绑定数据
}