请高手指明:gridview的数据到导出Excel时,没有数据(为空白内容)。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了请高手指明:gridview的数据到导出Excel时,没有数据(为空白内容)。相关的知识,希望对你有一定的参考价值。

昨天,实现的好好的,今天就导出为空白内容了。是怎么回事呀???
代码如下:按钮调用toExcelClk()方法;gridview中有数据哟!

private void ToExcel(Control ctl, string FileName)

HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + "" + FileName);
ctl.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
htmlTextWriter hw = new HtmlTextWriter(tw);
ctl.RenderControl(hw);
Response.Write("<html><head><meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">");
HttpContext.Current.Response.Write(tw.ToString());
Response.Write("</body></html>");
HttpContext.Current.Response.End();

private void toExcelClk()

gvCard.AllowPaging = false;
gvCard.AllowSorting = false;
gvCard.DataBind();
gvCard.Columns[3].Visible = false;
gvCard.Columns[4].Visible = false;
gvCard.Columns[6].Visible = false;
ToExcel(gvCard, "score.xls");
gvCard.AllowPaging = true;
gvCard.AllowSorting = true;
gvCard.DataBind();

该方法只是把asp.net页面保存成html页面只是把后缀改为xlc不过excel可以读取,接下连我看看还有别的方式能导出数据,并利用模版生成。
下面是代码
县新建一个asp.ne的tweb应用程序把代码粘贴进去就好了
html页面代码
<%@ Page language="c#" Codebehind="OutExcel.aspx.cs" AutoEventWireup="false" Inherits="eMeng.Exam.OutPutExcel" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>OutPutExcel</title>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:datagrid id="DataGrid1" runat="server">
<Columns>
<asp:BoundColumn></asp:BoundColumn>
</Columns>
</asp:datagrid>
<P>
<asp:Label id="Label1" runat="server">文件名:</asp:Label>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
<asp:button id="Button1" runat="server" Text="输出到Excel"></asp:button></P>
</form>
</body>
</HTML>
接下来是cs页面里的代码
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace eMeng.Exam

/// <summary>
/// OutPutExcel 的摘要说明。
/// </summary>
public class OutPutExcel : System.Web.UI.Page

protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Label Label1;
private DataSet myDS =new DataSet();
private void Page_Load(object sender, System.EventArgs e)

// 在此处放置用户代码以初始化页面
if(!Page.IsPostBack)

Data_Load();//调用方法填充表格



/// <summary>
/// 创建数据源
/// </summary>
/// <returns>DataView</returns>
private void Data_Load()

//数据库连接字符串Catalog为指定的数据库名称,DataSource为要连接的SQL服务器名称
string myConn ="User Id=sa;Password=sa;Initial Catalog=test;Data Source=zxb;Connect Timeout=20";
//查询字符串
string mysqlstr="SELECT * FROM fy";
//连接数据库操作
SqlConnection myConnection = new SqlConnection(myConn);
//执行SQL语句操作
SqlDataAdapter myDataAdapter = new SqlDataAdapter(mySQLstr,myConnection);
//打开数据库
myConnection.Open();
//向DataSet填充数据,填充数据库服务器中test库中的fy表
myDataAdapter.Fill(myDS,"fy");
//向DastaGrid填充数据
DataGrid1.DataSource=myDS;
DataGrid1.DataBind();

/// <summary>
/// 输出到Excel
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button1_Click(object sender, System.EventArgs e)

if(TextBox1.Text=="")

Response.Write("<SCRIPT language=javascript>");
Response.Write("window.alert('请输入文件名');");
Response.Write("</SCRIPT>");

else

Response.Clear();
Response.Buffer= true;
Response.Charset="GB2312"; //设置了类型为中文防止乱码的出现
Response.AppendHeader("Content-Disposition","attachment;filename="+TextBox1.Text+".xls"); //定义输出文件和文件名
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
this.EnableViewState = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN",true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.DataGrid1.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());


#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)

//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()

this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

#endregion
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)

if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)

e.Item.Cells[0].Attributes.Add("style","vnd.ms-excel.numberformat:@");
e.Item.Cells[3].Attributes.Add("style","vnd.ms-excel.numberformat:¥#,###.00");




还在继续研究别的方式
<%@ Page language="c#" Codebehind="OutExcel.aspx.cs" AutoEventWireup="false" Inherits="eMeng.Exam.OutPutExcel" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>OutPutExcel</title>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:datagrid id="DataGrid1" runat="server">
<Columns>
<asp:BoundColumn></asp:BoundColumn>
</Columns>
</asp:datagrid>
<P>
<asp:Label id="Label1" runat="server">文件名:</asp:Label>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
<asp:button id="Button1" runat="server" Text="输出到Excel"></asp:button></P>
</form>
</body>
</HTML>
接下来是cs页面里的代码
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace eMeng.Exam

/// <summary>
/// OutPutExcel 的摘要说明。
/// </summary>
public class OutPutExcel : System.Web.UI.Page

protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Label Label1;
private DataSet myDS =new DataSet();
private void Page_Load(object sender, System.EventArgs e)

// 在此处放置用户代码以初始化页面
if(!Page.IsPostBack)

Data_Load();//调用方法填充表格



/// <summary>
/// 创建数据源
/// </summary>
/// <returns>DataView</returns>
private void Data_Load()

//数据库连接字符串Catalog为指定的数据库名称,DataSource为要连接的SQL服务器名称
string myConn ="User Id=sa;Password=sa;Initial Catalog=test;Data Source=zxb;Connect Timeout=20";
//查询字符串
string mySQLstr="SELECT * FROM fy";
//连接数据库操作
SqlConnection myConnection = new SqlConne

夜_一少
参考技术A 我给你一个例子,你看看吧 gvStatistic是一个GridView

/// <summary>
/// 导出为Excel
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnOut_Click(object sender, EventArgs e)

//如果GridView分页的话,则先去掉分页,否则只能导出当前页的内容
int pageIndex = this.gvStatistic.PageIndex;
this.gvStatistic.AllowPaging = false;
这个方法是绑定GridView的数据
this.StatisticBind();

//以下为导出Excel部分
HttpContext.Current.Response.Charset="UTF8";
HttpContext.Current.Response.ContentEncoding=System.Text.Encoding.UTF8;
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename="+System.Web.HttpUtility.UrlEncode("统计数据", System.Text.Encoding.UTF8) + ".xls");
gvStatistic.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
gvStatistic.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();

//导出完后,设置GridView分页,并且仍然显示原页数
this.gvStatistic.AllowPaging = true;
this.gvStatistic.PageIndex = pageIndex;
this.StatisticBind();


//下面这个方法里虽然没有啥内容,但你也要写上
public override void VerifyRenderingInServerForm(Control control)


// Confirms that an HtmlForm control is rendered for

参考技术B http://blog.csdn.net/yupengcheng_net/archive/2009/12/28/5090239.aspx

希望可以对你有帮助

用asp.net 把GridView中的所有数据导出到Excel中没问题,根据条件查询的信息导出到Excel中 怎么整???

先查询你想要的结果绑定后,然后导出 不就行了? 如果是在已经绑定好的里面 再筛选的话,不妨加个checkbox 参考技术A 你把根据条件查询的信息也绑定在导出的方法里面,代码我也有。

以上是关于请高手指明:gridview的数据到导出Excel时,没有数据(为空白内容)。的主要内容,如果未能解决你的问题,请参考以下文章

把GridView1的数据导出到excel的sheet1把GridView2的数据导出到excel的sheet2.

将 Gridview 导出到 Excel:无法导出显示的正确数据

用asp.net 把GridView中的所有数据导出到Excel中没问题,根据条件查询的信息导出到Excel中 怎么整???

将GridView导出到Excel,并在网页上显示完全相同的样式

将 GridView 导出到 Excel 2007

怎样将C#生成的数据导出