首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

怎么把label里有html格式的文字导出到一个word文档里,word里的文字要有label里的html格式?很着急答对者50分

2012-01-31 
如何把label里有html格式的文字导出到一个word文档里,word里的文字要有label里的html格式?急~~很着急!!答

如何把label里有html格式的文字导出到一个word文档里,word里的文字要有label里的html格式?急~~很着急!!答对者50分
如何把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分

[解决办法]
HttpUtility.HtmlEncode( " <html> " + Label.Text + " </html> ", hw);
------解决方案--------------------



/// <summary>
/// HtmlToOffice 的摘要说明
/// </summary>
/// <remarks> page 页面属性必须为EnableEventValidation = "false " </remarks>
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;


}
}

热点排行