public void ExportControl(System.Web.UI.Control source, string DocumentType, string filename) { //设置Http的头信息,编码格式 if (DocumentType == "Excel") { //Excel HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename + ".xls", System.Text.Encoding.UTF8)); HttpContext.Current.Response.ContentType = "application/ms-excel"; } else if (DocumentType == "Word") { //Word .UrlEncode HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename + ".doc", System.Text.Encoding.UTF8)); HttpContext.Current.Response.ContentType = "application/ms-word"; } HttpContext.Current.Response.Charset = "GB2312"; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); HttpContext.Current.Response.ContentType = "application/ms-excel"; HttpContext.Current.Response.Write(""); HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default; //关闭控件的视图状态 source.Page.EnableViewState = false; //初始化HtmlWriter System.IO.StringWriter writer = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(writer); source.RenderControl(htmlWriter); //输出 HttpContext.Current.Response.Write(writer.ToString()); HttpContext.Current.Response.End(); }
我是这样导出的 所有的内容都可以 只有图片无法显示,求解决方法,谢谢!!急急!!
我博客里有个类库,可以操作WORD,能插入图片:
http://blog.csdn.net/durongjian/archive/2010/09/09/5873622.aspx
直接输出是不可以了。