导出Excel,world

来源:互联网 发布:cnc数控机床仿真软件 编辑:程序博客网 时间:2024/06/02 06:34

 private void ExpertControl(DocumentType type)
  {
   Response.Clear();
   Response.Charset = "UTF-8";
   Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");

   if (type == DocumentType.Word)
   {
    Response.ContentType = "application/ms-word";
 
    Response.AppendHeader("content-disposition", "attachment;filename=visit.doc");
   }
   else if(type == DocumentType.Excel)
   {
    Response.ContentType = "application/ms-excel";
    Response.AppendHeader("content-disposition", "attachment;filename=visit.xls");
   }
   this.Page.EnableViewState = false;

   //初始化HtmlWriter

   System.IO.StringWriter writer = new System.IO.StringWriter();

   System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(writer);

   this.DataGrid1.RenderControl(htmlWriter);
   //输出

   Response.Write(writer.ToString());

   Response.End();
  }

  //文档类型
  public enum DocumentType
  {
   Word,
   Excel
  }
  

原创粉丝点击