实现功能,将word文档中的内容复制到编辑器上,生成内容在页面上,除去页面上的冗余
现在写我写了这样的一个方法
/// <summary>
///Class1 的摘要说明
/// </summary>
public class Class1
{
public Class1()
{
}
/// <summary>
/// 清理HTML标签的多余
/// </summary>
/// <param name="element">要清理的标签</param>
/// <param name="str">原始文件</param>
/// <returns></returns>
public static string repElement(string element, string str)
{
string old = @"<" + element + "[^>]+>";
string rep = "<" + element + ">";
str = Regex.Replace(str.ToString(), old, rep);
return str;
}
/// <summary>
/// 清除HTML标签
/// </summary>
/// <param name="str">原始文本</param>
/// <param name="element">要清除的标签</param>
/// <returns></returns>
public static string ReMoveElement(string str, string element)
{
string regFront = @"<" + element + "[^>]*>";
string regAfter = "</" + element + ">";
str = Regex.Replace(str, regFront, "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, regAfter, "", RegexOptions.IgnoreCase);
return str;
}
/// <summary>
/// 清理指定字符串,大小写不敏感
/// </summary>
/// <param name="strText">原始文本</param>
/// <param name="strOld">要替换的字符串,支持正则表达式,大小写不敏感</param>
/// <param name="strNew">替换后的字符串</param>
/// <returns></returns>
public static string RegexReplace(string strText, string strOld, string strNew)
{
strText = Regex.Replace(strText, strOld, strNew, RegexOptions.IgnoreCase);
return strText;
}
/// <summary>
/// 清理Word的样式,主要是一些带冒号的标签,如o:p
/// </summary>
/// <param name="strText"></param>
/// <returns></returns>
public static string ClearWordStyle(string strText)
{
string regFront = @"<\w+:[^>]*>";
string regAfter = @"</\w+:[^>]*>";
strText = Regex.Replace(strText, regFront, "", RegexOptions.IgnoreCase);
strText = Regex.Replace(strText, regAfter, "", RegexOptions.IgnoreCase);
return strText;
}
/// <summary>
///实际操作时(清理word标签、样式)
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public string rep(string str)
{
string[] el = new string[] { "p", "div", "table", "tr", "td" };
foreach (string s in el)
{
try
{
str = repElement(str, s);
}
catch
{
continue;
}
}
return str;
}
/// <summary>
///实际操作时(清除word标签)
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public string ReMoveWordStyle(string str)
{
string[] el = new string[] { "span", "strong", "font", "h1", "tbody", "o:p" };
foreach (string s in el)
{
try
{
str = ReMoveElement(str, s);
}
catch
{
continue;
}
}
str = RegexReplace(str, " ", ""); return str;
}
}
在编辑页面 public partial class _Default : System.Web.UI.Page
{ Class1 ds = new Class1();
protected void Page_Load(object sender, EventArgs e)
{ if (!Page.IsPostBack)
{
Editor1.Text = "取值";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
ds.rep(Editor1.Text);
ds.ReMoveWordStyle(Editor1.Text);
Response.Write(Editor1.Text);
}
}(Editor1编辑的自定义控件)
调用方法,为什么无法实现功能,和没有执行方法前一样
现在写我写了这样的一个方法
/// <summary>
///Class1 的摘要说明
/// </summary>
public class Class1
{
public Class1()
{
}
/// <summary>
/// 清理HTML标签的多余
/// </summary>
/// <param name="element">要清理的标签</param>
/// <param name="str">原始文件</param>
/// <returns></returns>
public static string repElement(string element, string str)
{
string old = @"<" + element + "[^>]+>";
string rep = "<" + element + ">";
str = Regex.Replace(str.ToString(), old, rep);
return str;
}
/// <summary>
/// 清除HTML标签
/// </summary>
/// <param name="str">原始文本</param>
/// <param name="element">要清除的标签</param>
/// <returns></returns>
public static string ReMoveElement(string str, string element)
{
string regFront = @"<" + element + "[^>]*>";
string regAfter = "</" + element + ">";
str = Regex.Replace(str, regFront, "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, regAfter, "", RegexOptions.IgnoreCase);
return str;
}
/// <summary>
/// 清理指定字符串,大小写不敏感
/// </summary>
/// <param name="strText">原始文本</param>
/// <param name="strOld">要替换的字符串,支持正则表达式,大小写不敏感</param>
/// <param name="strNew">替换后的字符串</param>
/// <returns></returns>
public static string RegexReplace(string strText, string strOld, string strNew)
{
strText = Regex.Replace(strText, strOld, strNew, RegexOptions.IgnoreCase);
return strText;
}
/// <summary>
/// 清理Word的样式,主要是一些带冒号的标签,如o:p
/// </summary>
/// <param name="strText"></param>
/// <returns></returns>
public static string ClearWordStyle(string strText)
{
string regFront = @"<\w+:[^>]*>";
string regAfter = @"</\w+:[^>]*>";
strText = Regex.Replace(strText, regFront, "", RegexOptions.IgnoreCase);
strText = Regex.Replace(strText, regAfter, "", RegexOptions.IgnoreCase);
return strText;
}
/// <summary>
///实际操作时(清理word标签、样式)
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public string rep(string str)
{
string[] el = new string[] { "p", "div", "table", "tr", "td" };
foreach (string s in el)
{
try
{
str = repElement(str, s);
}
catch
{
continue;
}
}
return str;
}
/// <summary>
///实际操作时(清除word标签)
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public string ReMoveWordStyle(string str)
{
string[] el = new string[] { "span", "strong", "font", "h1", "tbody", "o:p" };
foreach (string s in el)
{
try
{
str = ReMoveElement(str, s);
}
catch
{
continue;
}
}
str = RegexReplace(str, " ", ""); return str;
}
}
在编辑页面 public partial class _Default : System.Web.UI.Page
{ Class1 ds = new Class1();
protected void Page_Load(object sender, EventArgs e)
{ if (!Page.IsPostBack)
{
Editor1.Text = "取值";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
ds.rep(Editor1.Text);
ds.ReMoveWordStyle(Editor1.Text);
Response.Write(Editor1.Text);
}
}(Editor1编辑的自定义控件)
调用方法,为什么无法实现功能,和没有执行方法前一样
http://blog.csdn.net/fengyarongaa/article/details/6563179Editor1.Text = ds.rep(Editor1.Text);
Editor1.Text= ds.ReMoveWordStyle(Editor1.Text);