日期:2014-05-18  浏览次数:20720 次

web页面导出word文档,解决马上给分
我的defalut.aspx里有个labe1控件,和datagrid控件,我在网上查了很多都只是把datagrid里的数据导出到word,有没有什么方法可以把datagrid以外的控件,即页面显示的所有信息都导入到word,请高手指教,我下面的程序如何修改
  private void ExportDataGrid(string FileType, string FileName) 
  {
  Response.Charset = "GB2312";
  Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");

  Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
  Response.ContentType = FileType;
  this.EnableViewState = false;
  StringWriter tw = new StringWriter();
  HtmlTextWriter hw = new HtmlTextWriter(tw);
  datagrid1.RenderControl (hw );
  Response.Write(tw.ToString());
  Response.End();
  }


  protected void BtEord_Click(object sender, EventArgs e)
  {
  ExportDataGrid("application/ms-word", "Word.doc");

  }

------解决方案--------------------
private void ExportDataGrid(string FileType, string FileName) 

string filePath = Server.MapPath("DocFiles/" + FileName);
System.IO.FileStream myFileStream = new System.IO.FileStream
(filePath, System.IO.FileMode.Create);
System.IO.TextWriter write = new System.IO.StreamWriter(myFileStream, System.Text.Encoding.GetEncoding("gb2312"));
write.WriteLine("<html>");
write.WriteLine("<head>"); 
write.WriteLine("</head>");
write.WriteLine("<body>");
write.WriteLine(lblContent.Text);
write.WriteLine("</body>");
write.WriteLine("</html>");
write.Close();