将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();
}

 

上一篇:dplyr五个核心函数:filter()筛选行 arrange()排列行 select()选择列 mutate()基于现有变量创建新变量列 summarise()计算摘要统计量


下一篇:导出doc