如何把label里有html格式的文字导出到一个word文档里,word里的文字要有label里的html格式        public static void tofiles(System.Web.HttpResponse pa, System.Web.UI.WebControls.Label Label, string filename, string filetype)
        {
            System.Web.HttpResponse httpresponse = pa;            httpresponse.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
            if (filetype == "0")
            {
                httpresponse.AppendHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8) + ".doc"); //filename="*.doc"; 
                httpresponse.ContentType = "application/msword";
            }
            else
            {
                httpresponse.AppendHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8) + ".xls"); //filename="*xls"; 
                httpresponse.ContentType = "application/ms-excel";
            }
            System.IO.StringWriter sw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
            HttpUtility.HtmlEncode(Label.Text, hw);            //hw.Write(a);            httpresponse.Output.Write(HttpUtility.HtmlDecode(sw.ToString()));
            httpresponse.End();
        }
这是我写的类,实在搞不出来了,要是有人知道,我给50分

解决方案 »

  1.   

    我现在的word文档打开是这样的:<strong>aaaaaa</strong>我想要的结果是粗体的aaaaaa没有人知道么?问了2帖子了都
      

  2.   

    HttpUtility.HtmlEncode("<html>" + Label.Text + "</html>", hw);
      

  3.   


    /// <summary>
    /// HtmlToOffice 的摘要说明
    /// </summary>
    /// <res>page 页面属性必须为EnableEventValidation = "false"</res>
    public class HtmlToOffice
    {
        /// <summary>
        /// 页面转换成Doc文本文件
        /// </summary>
        /// <param name="pg">页面类,即页面的this指针</param>
        /// <param name="filename">文件名</param>
        /// <param name="str_error">错误信息</param>
        /// <returns>是否成功的标志</returns>
        public static bool HtmlToDoc(Page pg,string filename, out string str_error)
            {
                try
                {
                    pg.Response.Clear();                pg.Response.Buffer = true;
                    pg.Response.Charset = "gb2312";
                    if (filename == "")
                    {
                        filename = "xx";
                    }
                    pg.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");//设置输出流为简体中文
                
                    pg.Response.ContentType = "application/octet-stream"; //设置输出文件类型为DOC文件。
                    pg.Response.AddHeader("Content-Disposition", "attachment; filename=" + filename + ".doc");
                   // pg.EnableViewState = false;
                  
                    System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
                    System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
                    System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
                    pg.RenderControl(oHtmlTextWriter);
                    pg.Response.Write(oStringWriter.ToString());
                    pg.Response.End();
                }
                catch (Exception ex)
                {
                    str_error = ex.Message;
                    throw ex;
                     
                     
                }
                str_error = "正常导出";
                return true;       
            }
      
        /// <summary>
        /// 把字符串转换成Doc文本文件
        /// </summary>
        /// <param name="pg">页面类,即页面的this指针</param>
        /// <param name="stream">要转化的字符串</param>
        /// <param name="filename">文件名</param>
        /// <param name="str_error">错误信息</param>
        /// <returns>是否成功的标志</returns>
        public static bool StrToDoc(Page pg, string stream, string filename, out string str_error)
        {
            try
            {
                pg.Response.Clear();            pg.Response.Buffer = true;
                pg.Response.Charset = "gb2312";
                if (filename == "")
                {
                    filename = "xx";
                }
                pg.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");//设置输出流为简体中文
                
                pg.Response.ContentType = "application/octet-stream"; //设置输出文件类型为DOC文件。
                pg.Response.AddHeader("Content-Disposition", "attachment; filename=" + filename + ".doc");
               // pg.EnableViewState = false;
                System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
                System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
                System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
                oHtmlTextWriter.Write(stream);
              
                pg.Response.Write(oStringWriter.ToString());
                pg.Response.End();
            }
            catch (Exception ex)
            {
                str_error = ex.Message;
                throw ex;
                       }
            str_error = "正常导出";
            return true;
        }
        /// <summary>
        /// 页面转换成Excel文本文件
        /// </summary>
        /// <param name="pg">页面类,即页面的this指针</param>
        /// <param name="filename">文件名</param>
        /// <param name="str_error">错误信息</param>
        /// <returns>是否成功的标志</returns>
        public static bool HtmlToExcel(Page pg, string filename, out string str_error)
        {
            try
            {
                pg.Response.Clear();            pg.Response.Buffer = true;
                pg.Response.Charset = "gb2312";
                if (filename == "")
                {
                    filename = "xx";
                }
                pg.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");//设置输出流为简体中文
                pg.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 
                pg.Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename + ".xls");
              
               // pg.EnableViewState = false;
                System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
                System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
                System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
                pg.RenderControl(oHtmlTextWriter);
                pg.Response.Write(oStringWriter.ToString());
                pg.Response.End();
            }
            catch (Exception ex)
            {
                str_error = ex.Message;
                throw ex;
                      }
            str_error = "正常导出";
            return true;
        }
        /// <summary>
        /// 把字符串转换成Excel文本文件
        /// </summary>
        /// <param name="pg">页面类,即页面的this指针</param>
        /// <param name="stream">要转化的字符串</param>
        /// <param name="filename">文件名</param>
        /// <param name="str_error">错误信息</param>
        /// <returns>是否成功的标志</returns>
        public static bool StrToExcel(Page pg, string stream, string filename, out string str_error)
        {
            try
            {
                pg.Response.Clear();            pg.Response.Buffer = true;
                pg.Response.Charset = "gb2312";
                if (filename == "")
                {
                    filename = "xx";
                }
                pg.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");//设置输出流为简体中文
                pg.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 
                pg.Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename + ".xls");
               
                // pg.EnableViewState = false;
                System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
                System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
                System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
                oHtmlTextWriter.Write(stream);
              
                pg.Response.Write(oStringWriter.ToString());
                pg.Response.End();
            }
            catch (Exception ex)
            {
                str_error = ex.Message;
                throw ex;
                       }
            str_error = "正常导出";
            return true;
        }
    }