C# 清除Html Table中的css样式

public class HtmlTableHandler
{
    /// <summary>
    /// 将html table中的CSS样式清除
    /// </summary>
    /// <param name="src">要清除的字符串</param>
    /// <returns>清除后的字符串</returns>
    public string StrHandler(string src)
    {
        int startIndex = 0;
        string tempStr;
        Dictionary<string, List<DataRowProperties>> vs = new Dictionary<string, List<DataRowProperties>>();
        while (GetContent(src, ref startIndex, "<table", "</table>", out tempStr))
        {
            vs[tempStr] = Table2List(tempStr);
        }
        if (vs.Count > 0)
        {
            int i = 1;
            foreach (KeyValuePair<string, List<DataRowProperties>> kv in vs)
            {
                src = src.Replace(kv.Key, Ls2Str(kv.Value));
                i++;
            }
        }
        return src;
    }
    private List<DataRowProperties> Table2List(string tableStr)
    {
        List<DataRowProperties> result = new List<DataRowProperties>();
        int startIndex = 0;
        string trStr;
        while (GetContent(tableStr, ref startIndex, "<tr", "</tr>", out trStr))
        {
            DataRowProperties tempLs = new DataRowProperties();
            int tempIndex = 0;
            string tempStr = string.Empty;
            if (trStr.Contains("</th>"))
            {
                ;
                while (GetContent(trStr, ref tempIndex, "<th", "</th>", out tempStr))
                {
                    DataProperties dp = new DataProperties();
                    dp.Value = GetDd(tempStr, ">", "</th>");
                    if (tempStr.Contains("rowspan"))
                    {
                        int ir = tempStr.IndexOf("rowspan");
                        dp.RowSpan = GetDd(tempStr.Substring(ir), "\"", "\"");
                    }
                    if (tempStr.Contains("colspan"))
                    {
                        int ir = tempStr.IndexOf("colspan");
                        dp.ColSpan = GetDd(tempStr.Substring(ir), "\"", "\"");
                    }
                    tempLs.Value.Add(dp);
                }
            }
            else
            {
                while (GetContent(trStr, ref tempIndex, "<td", "</td>", out tempStr))
                {
                    DataProperties dp = new DataProperties();
                    dp.Value = GetDd(tempStr, ">", "</td>");
                    if (tempStr.Contains("rowspan"))
                    {
                        int ir = tempStr.IndexOf("rowspan");
                        dp.RowSpan = GetDd(tempStr.Substring(ir), "\"", "\"");
                    }
                    if (tempStr.Contains("colspan"))
                    {
                        int ir = tempStr.IndexOf("colspan");
                        dp.ColSpan = GetDd(tempStr.Substring(ir), "\"", "\"");
                    }
                    tempLs.Value.Add(dp);
                }
            }
            result.Add(tempLs);
        }
        return result;
    }
    private bool GetContent(string src, ref int startIndex, string startStr, string endStr, out string result)
    {
        if (string.IsNullOrEmpty(startStr) || string.IsNullOrEmpty(endStr) || string.IsNullOrEmpty(src) || src.Length <= startIndex + startStr.Length)
        {
            result = string.Empty;
            return false;
        }
        startIndex = src.IndexOf(startStr, startIndex);
        if (startIndex < 0)
        {
            result = string.Empty;
            return false;
        }
        if (src.Length <= startIndex + startStr.Length)
        {
            result = string.Empty;
            return false;
        }
        int endIndex = src.IndexOf(endStr, startIndex + startStr.Length);
        if (endIndex < 0 || endIndex <= startIndex)
        {
            result = string.Empty;
            return false;
        }
        result = src.Substring(startIndex, endIndex - startIndex + endStr.Length);
        startIndex = endIndex + endStr.Length;
        return true;
    }
    private string GetDd(string strSrc, string startStr, string endStr)
    {
        if (string.IsNullOrEmpty(strSrc) || string.IsNullOrEmpty(startStr) || string.IsNullOrEmpty(endStr))
        {
            return string.Empty;
        }
        if (strSrc.Length > startStr.Length + endStr.Length)
        {
            int startIndex = strSrc.IndexOf(startStr);
            if (startIndex < 0)
            {
                return string.Empty;
            }
            if (strSrc.Length <= startIndex + startStr.Length)
            {
                return string.Empty;
            }
            int endIndex = strSrc.IndexOf(endStr, startIndex + startStr.Length);
            if (endIndex < 0)
            {
                return string.Empty;
            }
            return strSrc.Substring(startIndex + startStr.Length, endIndex - startIndex - startStr.Length);
        }
        return string.Empty;
    }
    private string Ls2Str(List<DataRowProperties> data)
    {
        StringBuilder builder = new StringBuilder();
        builder.AppendLine("<table>");
        if (data != null && data.Count > 0)
        {
            for (int i = 0; i < data.Count; i++)
            {
                if (i == 0)
                {
                    builder.AppendLine("<thead><tr>");
                    foreach (DataProperties s in data[i].Value)
                    {
                        string rs = (string.IsNullOrEmpty(s.RowSpan) ? string.Empty : $"rowspan=\"{s.RowSpan}\" ") + (string.IsNullOrEmpty(s.ColSpan) ? string.Empty : $"colspan=\"{s.ColSpan}\"");
                        if (string.IsNullOrEmpty(rs.Trim()))
                        {
                            builder.Append($"<th>{s.Value}</th>");
                        }
                        else
                        {
                            builder.Append($"<th {rs.Trim()}>{s.Value}</th>");
                        }

                    }
                    builder.AppendLine("</tr></thead>");
                }
                else
                {
                    if (i == 1)
                    {
                        builder.AppendLine("<tbody>");
                    }

                    builder.AppendLine("<tr>");
                    foreach (DataProperties s in data[i].Value)
                    {
                        string rs = (string.IsNullOrEmpty(s.RowSpan) ? string.Empty : $"rowspan=\"{s.RowSpan}\" ") + (string.IsNullOrEmpty(s.ColSpan) ? string.Empty : $"colspan=\"{s.ColSpan}\"");
                        if (string.IsNullOrEmpty(rs.Trim()))
                        {
                            builder.Append($"<td>{s.Value}</td>");
                        }
                        else
                        {
                            builder.Append($"<td {rs.Trim()}>{s.Value}</td>");
                        }
                    }
                    builder.AppendLine("</tr>");

                    if (i == data.Count - 1)
                    {
                        builder.AppendLine("</tbody>");
                    }
                }
            }
        }
        builder.AppendLine("</table>");
        return builder.ToString();
    }
    public class DataRowProperties
    {
        public List<DataProperties> Value = new List<DataProperties>();
    }
    public class DataProperties
    {
        /// <summary>
        /// td数据内容
        /// </summary>
        public string Value { get; set; }
        /// <summary>
        /// 跨多少行
        /// </summary>
        public string RowSpan { get; set; }
        /// <summary>
        /// 跨多少列
        /// </summary>
        public string ColSpan { get; set; }
    }
}

评论