将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("<br><br><center><font size=4 color='red'>制表人 : {0} &nbsp;&nbsp;上报日期 : {1}</font></centre>", lblReportUser.Text, lblReportTime.Text);
  hw.InnerWriter.Write(print);
  Response.Write(tw.ToString());
  Response.End();
}

 

posted @ 2022-02-09 13:23  NorbertNing  阅读(41)  评论(0)    收藏  举报