public void ExortExcel() //从GridView中导出Excel的函数
{
if (this.Gv.Rows.Count != 0)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls"); //这里是用日期做名称
HttpContext.Current.Response.Charset = "utf-8";
HttpContext.Current.Response.ContentType = "application/excel";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
this.Gv.AllowPaging = false; //GridView不启用分页
Gv.RenderControl(htmlWrite);
HttpContext.Current.Response.Write(stringWrite.ToString());
}
}
private void ToExcel() //从GridView导出Excel函数2 (这两个方法可任选其一)
{
string style = @"<style> .text { mso-number-format:/@; } </script> ";
string Ftile = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() + System.DateTime.Now.Hour.ToString();
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=COM_" + Ftile + ".xls");
// Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
// Response.Write("<meta http-equiv=Content-Type content=/"text/html; charset=GB2312/">");
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.Gvexel.RenderControl(oHtmlTextWriter);//GVDY 是Gridview的ID名称
Response.Write(style);
Response.Write(oStringWriter.ToString());
Response.End();
}
// 解决身份证号的科学计数问题
protected void Gvexel_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)//设置身份证号的格式,Cells[i],i从0开始
e.Row.Cells[7].Attributes.Add("style", "vnd.ms-excel.numberformat:@;");
}
注意事项:
1、如果你的GirdView有分页的话,那么就要注意了,最简单的办法就是再绑定一个同样的GridView,但是记住不要再绑定分页控件了,这样的话,在导出Excel函数里面绑定GridView的时候,就可以绑定没有分页的那个GridView了。
2、如果你只想导出GridView中的若干列的话,你也可以考虑以上方案,即另外绑定一个GrifView。
3、这句话也是必须的。public override void VerifyRenderingInServerForm(Control control)
{ }
===============================以下是.net导出Excel的通用方法==============================
可以适用的C#中的所有数据绑定控件,如repeater,gridView,DataList,还可以用于普通的table显示的数据,即使有分页也不用担心了,
该函数可以避免再重新创建另外一个数据绑定控件,函数本身就能解决分页问题。大家可以参考参考!
/// <summary>
/// 得到导出数据
/// </summary>
/// <returns></returns>
protected string GetContent()
{
string retVal = "";
string head = "编号,游戏,公会名,标题,描述,状态,更新时间,创建时间";
string contentFormat = "";
int recordCount = 0;
int pageCount = 0;
StringBuilder sbContent = new StringBuilder();
DataTable dt = ActivityMgr.GetByCondition(this.gameidx, 0, this.guildName, this.title, this.description,
int.Parse(this.Status), this.StartDate, this.EndDate,
int.MaxValue, 1,out recordCount, out pageCount);
sbContent.AppendLine(head);
if (dt != null && dt.Rows.Count>0)
{
foreach (DataRow dr in dt.Rows)
{
contentFormat = string.Format("{0},{1},{2},{3},{4},{5},{6},{7}",
dr["IDX"],dr["GameName"], dr["GuildName"], dr["Title"], dr["Description"].ToString().Replace(',', ','),
CommonHelper.Get3StatusString(dr["Status"].ToString()), dr["UpdateTime"], dr["RowTime"]);
sbContent.AppendLine(contentFormat);//有换行
}
}
retVal = sbContent.ToString();
return retVal;
}
/// <summary>
/// 导出Excel
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void BtnExport_Click(object sender, EventArgs e)
{
SaveCSV saveFile = new SaveCSV(this.Response);
string FullPath = string.Format(@"ListActivity_{0}.csv", DateTime.Now.ToShortDateString());
string content = GetContent();
bool retVal = saveFile.OutFile(FullPath, content);
if (retVal)
{
this.Alert(@"数据导出成功!");
}
else
{
this.Alert(@"数据导出失败!");
}
}