将DataGridview的内容保存到Excel


protected void btnprint_Click(object sender, EventArgs e)
{
  Response.Charset = "GB2312";
  Response.ContentEncoding = System.Text.Encoding.UTF7;
  Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("考勤统计记录.xls", Encoding.UTF8).ToString());

  Response.ContentType = "application/ms-excel";
  this.EnableViewState = false;
  StringWriter tw = new StringWriter();
  HtmlTextWriter hw = new HtmlTextWriter(tw);
  gvSignInfoStatistic.RenderControl(hw);
  string print = string.Format("

制表人 : {0}   上报日期 : {1}", lblReportUser.Text, lblReportTime.Text);   hw.InnerWriter.Write(print);   Response.Write(tw.ToString());   Response.End(); }
C