本帖最后由 SomethingJack 于 2013-12-09 14:08:11 编辑

解决方案 »

  1.   


    给你推荐个东西,你可以看下,学习下,对你这个我觉的有帮助!Jumony Core百度下!
      

  2.   


    给你推荐个东西,你可以看下,学习下,对你这个我觉的有帮助!Jumony Core百度下!
    - - 纯字符串操作实现  不实用第三方 上司的要求。
      

  3.   


    给你推荐个东西,你可以看下,学习下,对你这个我觉的有帮助!Jumony Core百度下!
    - - 纯字符串操作实现  不实用第三方 上司的要求。你们的上方,好牛B!!!!
      

  4.   

    可能是style='width:90px' 也可能直接是'width=90px' 这个可以通过两种方式来求值,如果是style那就取style 这是通过width取的就为空,反之一样;var setWidth1=Col标签的宽度值(style=)
    var setWidth2=Col标签的宽度值(width=)
     $("#tb1 td").each(function() {
                $(this).attr("width", setWidth1/2); //给宽度
            });table嵌套 怎么套的??
      

  5.   

    意思是 使用c#实现。  其实JQ实现比较容易 不过上面要求 谢谢回答,
      

  6.   

    哥们 对我的需求有何看法 - -  其实转换成XML容易实现的多 有层次了。方便的很
    不过现在是纯字符串操作。需求说起来比较简单 实现起来有点小难度唉  我写了一半  卡住了
      

  7.   

    教你一招,用代码先格式化html,这样它每行前面的空格就(层次)固定了,然后再用正则去替换。
      

  8.   

                string s = File.ReadAllText(@"c:\1.txt", Encoding.GetEncoding("gb2312"));
                MatchCollection mc = Regex.Matches(s, @"(?is)<table|<col([^/>]+)|<tr|<td|</table");
                string str = "";
                int grada = 0,n=0;
                List<object> liobj = new List<object>();
                List<string> listr = new List<string>();
                foreach (Match m in mc)
                {
                    if (m.Value == "<table")
                    {
                        grada++;
                    }
                    if (m.Value == "</table")
                    {
                        grada--;
                    }
                    if (m.Value == "<tr")
                    {
                        if (str == "<col")
                        {
                            liobj.Add(listr);
                            listr = new List<string>();
                        }
                    }
                    if (m.Value == "<td")
                    {
                        if (str != m.Value)
                        {
                            n = 0;
                        }
                        else n++;
                        if (liobj.Count > grada-1 )
                        {
                            List<string> _temp = liobj[grada-1] as List<string>;
                            if (_temp.Count > n)
                            {
                                int index = s.IndexOf("<td");
                                if (!string.IsNullOrEmpty(_temp[n]))
                                    s = s.Insert(index + 3, " " + _temp[n]);
                            }
                        }
                         s = s.Insert(s.IndexOf("<td") + 1, " ");
                    }                if (m.Value.IndexOf("<col") == 0)
                    {
                        listr.Add(m.Groups[1].Value.Trim());
                        str = "<col";
                    }
                    else { str = m.Value; }            }
                s = Regex.Replace(s.Replace("< td","<td"),"(?i)<col[^>]*>","");
                Console.WriteLine(s);这个就可以实现了。
      

  9.   

    string s = File.ReadAllText(@"c:\1.txt", Encoding.GetEncoding("gb2312"));
                List<string> liTable = new List<string>();
                for (int i = 0; true; i++)
                {
                    int index = s.LastIndexOf("<table");
                    if (index >= 0)
                    {
                        int end = s.IndexOf("</table>", index);
                        if (end > 0)
                        {
                            liTable.Add(s.Substring(index, end - index + 8));
                            s = s.Remove(index, end - index + 8);
                            s = s.Insert(index, "t_b"+i.ToString());
                        }
                        else break;
                    }
                    else break;
                }
                string toStr = "";
                List<string> liWidth = new List<string>();
                for (int i = liTable.Count - 1; i >= 0; i--)
                {
                    string _temp = liTable[i];
                    string str = "";
                    MatchCollection mc = Regex.Matches(_temp, @"(?is)<col([^/>]+)|<tr|<td");
                    int n = 0;
                    foreach (Match m in mc)
                    {
                        if (m.Value == "<td")
                        {
                            if (str != m.Value)
                            {
                                n = 0;
                            }
                            else n++;
                            if (liWidth.Count > n)
                            {
                                int index = _temp.IndexOf("<td");
                                if (!string.IsNullOrEmpty(liWidth[n]))
                                    _temp = _temp.Insert(index + 3, " " + liWidth[n]);
                            }
                            _temp = _temp.Insert(_temp.IndexOf("<td") + 1, " ");
                        }                    if (m.Value.IndexOf("<col") == 0)
                        {
                            liWidth.Add(m.Groups[1].Value.Trim());
                            str = "<col";
                        }
                        else { str = m.Value; }                }
                    _temp = Regex.Replace(_temp, "(?is)<col[^>]*>", "");
                    if (toStr == "") toStr = _temp;
                    else toStr = toStr.Replace("t_b" + i.ToString(), _temp);
                }
                Console.WriteLine(toStr);
    这是第二种办法,一个个table拆分。
      

  10.   


    十分感谢百忙中抽出时间给我参考代码 但是 你似乎真的想的简单了点。你可能没有测试过我的HTML字符串吧。格式完全错误了。难点1 要考虑嵌套Table 格式可能为<table><col><tr><td><table><col><tr><td></td></tr></table></td></tr></table>
    难点2 考虑Td有属性colspan.跨列
    难点3 合并的时候如何正确替换col和 td  取得两标签位置是关键。在同事前辈帮助下 已经完成的七七八八了 还是有问题 还在调试。 CSDN的各位 不妨可以试试看  绝对不是闹眼子的。 这个东西看上去两三句话确实简单 实现起来并非那么容易的
      

  11.   

    Quote: 引用 15 楼 c02645 的回复:

    string s = File.ReadAllText(@"c:\1.txt", Encoding.GetEncoding("gb2312"));
                List<string> liTable = new List<string>();
                for (int i = 0; true; i++)
                {
                    int index = s.LastIndexOf("<table");
                    if (index >= 0)
                    {
                        int end = s.IndexOf("</table>", index);
                        if (end > 0)
                        {
                            liTable.Add(s.Substring(index, end - index + 8));
                            s = s.Remove(index, end - index + 8);
                            s = s.Insert(index, "t_b"+i.ToString());
                        }
                        else break;
                    }
                    else break;
                }
                string toStr = "";
                List<string> liWidth = new List<string>();
                for (int i = liTable.Count - 1; i >= 0; i--)
                {
                    string _temp = liTable[i];
                    string str = "";
                    MatchCollection mc = Regex.Matches(_temp, @"(?is)<col([^/>]+)|<tr|<td");
                    int n = 0;
                    foreach (Match m in mc)
                    {
                        if (m.Value == "<td")
                        {
                            if (str != m.Value)
                            {
                                n = 0;
                            }
                            else n++;
                            if (liWidth.Count > n)
                            {
                                int index = _temp.IndexOf("<td");
                                if (!string.IsNullOrEmpty(liWidth[n]))
                                    _temp = _temp.Insert(index + 3, " " + liWidth[n]);
                            }
                            _temp = _temp.Insert(_temp.IndexOf("<td") + 1, " ");
                        }                    if (m.Value.IndexOf("<col") == 0)
                        {
                            liWidth.Add(m.Groups[1].Value.Trim());
                            str = "<col";
                        }
                        else { str = m.Value; }                }
                    _temp = Regex.Replace(_temp, "(?is)<col[^>]*>", "");
                    if (toStr == "") toStr = _temp;
                    else toStr = toStr.Replace("t_b" + i.ToString(), _temp);
                }
                Console.WriteLine(toStr);
    这是第二种办法,一个个table拆分。[/quote
    抱歉 又测试了一下  你这个貌似简单的嵌套可以正确替换 不过我上面给的字符串有点问题
      

  12.   

    你这个不用删除td的啊,只是把col里的属性拷至对应的td内嘛,然后再删除col节,不是吗?
      

  13.   


    string s = File.ReadAllText(@"c:\1.txt", Encoding.GetEncoding("gb2312"));
                List<string> liTable = new List<string>();
                for (int i = 0; true; i++)
                {
                    int index = s.LastIndexOf("<table");
                    if (index >= 0)
                    {
                        int end = s.IndexOf("</table>", index);
                        if (end > 0)
                        {
                            liTable.Add(s.Substring(index, end - index + 8));
                            s = s.Remove(index, end - index + 8);
                            s = s.Insert(index, "t_b"+i.ToString());
                        }
                        else break;
                    }
                    else break;
                }
                string toStr = "";
                List<string> liWidth = new List<string>();
                for (int i = liTable.Count - 1; i >= 0; i--)
                {
                    string _temp = liTable[i];
                    string str = "";
                    MatchCollection mc = Regex.Matches(_temp, @"(?is)<col([^/>]+)|<tr|<td");
                    int n = 0;
                    liWidth = new List<string>();
                    foreach (Match m in mc)
                    {
                        if (m.Value == "<td")
                        {
                            if (str != m.Value)
                            {
                                n = 0;
                            }
                            else n++;
                            if (liWidth.Count > n)
                            {
                                int index = _temp.IndexOf("<td");
                                if (!string.IsNullOrEmpty(liWidth[n]))
                                    _temp = _temp.Insert(index + 3, " " + liWidth[n]);
                            }
                            _temp = _temp.Insert(_temp.IndexOf("<td") + 1, " ");
                        }                    if (m.Value.IndexOf("<col") == 0)
                        {
                            liWidth.Add(m.Groups[1].Value.Trim());
                            str = "<col";
                        }
                        else { str = m.Value; }                }
                    _temp = Regex.Replace(_temp, "(?is)<col[^>]*>", "");
                    if (toStr == "") toStr = _temp;
                    else toStr = toStr.Replace("t_b" + i.ToString(), _temp);
                }
                Console.WriteLine(toStr);我觉得这个完全没有问题
      

  14.   

    内容是这样<table width='100%' border='1'>
               <col width='100px'>
               <col width='200px'>
               <tr>
                    <td colspan='2'>
                        <table width='100%' border='1'>
                            <col />
                            <col width='91px' />
                            <col width='92px' />
                            <tr>
                                <td align='left'>
                                    ISBN
                                </td>
                                <td align='left'>
                                    Title
                                </td>
                                <td align='right'>
                                    Price
                                </td>
                                <td align='right'>
                                    AAA
                                </td>
                            </tr>
                            <tr>
                                <td align='left'>
                                    3476896
                                </td>
                                <td align='left'>
                                    My first HTML
                                </td>
                                <td align='right'>
                                    $53
                                </td>
                                <td align='right'>
                                    BB
                                </td>
                            </tr>
                            <tr>
                                <td align='left'>
                                    3476897
                                </td>
                                <td align='left'>
                                    My second HTML
                                </td>
                                <td align='right'>
                                    $56
                                </td>
                                <td align='right'>
                                    CC
                                </td>
                            </tr>
                        </table>
                    </td>
                    <td colspan='2'>
                        <table width='100%' border='1'>
                            <col />
                            <col width='93px' />
                            <col width='94px' />
                            <tr>
                                <td align='left'>
                                    ISBN
                                </td>
                                <td align='left'>
                                    Title
                                </td>
                                <td align='right'>
                                    Price
                                </td>
                                <td align='right'>
                                    AAA
                                </td>
                            </tr>
                            <tr>
                                <td align='left'>
                                    3476896
                                </td>
                                <td align='left'>
                                    My first HTML
                                </td>
                                <td align='right'>
                                    $53
                                </td>
                                <td align='right'>
                                    BB
                                </td>
                            </tr>
                            <tr>
                                <td align='left'>
                                    3476897
                                </td>
                                <td align='left'>
                                    My second HTML
                                </td>
                                <td align='right'>
                                    $56
                                </td>
                                <td align='right'>
                                    CC
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </table>
    出来结果是这样
    <table width='100%' border='1'>
               
               
               <tr>
                    <td width='100px' colspan='2'>
                        <table width='100%' border='1'>
                            
                            
                            
                            <tr>
                                <td align='left'>
                                    ISBN
                                </td>
                                <td width='91px' align='left'>
                                    Title
                                </td>
                                <td width='92px' align='right'>
                                    Price
                                </td>
                                <td align='right'>
                                    AAA
                                </td>
                            </tr>
                            <tr>
                                <td align='left'>
                                    3476896
                                </td>
                                <td width='91px' align='left'>
                                    My first HTML
                                </td>
                                <td width='92px' align='right'>
                                    $53
                                </td>
                                <td align='right'>
                                    BB
                                </td>
                            </tr>
                            <tr>
                                <td align='left'>
                                    3476897
                                </td>
                                <td width='91px' align='left'>
                                    My second HTML
                                </td>
                                <td width='92px' align='right'>
                                    $56
                                </td>
                                <td align='right'>
                                    CC
                                </td>
                            </tr>
                        </table>
                    </td>
                    <td width='200px' colspan='2'>
                        <table width='100%' border='1'>
                            
                            
                            
                            <tr>
                                <td align='left'>
                                    ISBN
                                </td>
                                <td width='93px' align='left'>
                                    Title
                                </td>
                                <td width='94px' align='right'>
                                    Price
                                </td>
                                <td align='right'>
                                    AAA
                                </td>
                            </tr>
                            <tr>
                                <td align='left'>
                                    3476896
                                </td>
                                <td width='93px' align='left'>
                                    My first HTML
                                </td>
                                <td width='94px' align='right'>
                                    $53
                                </td>
                                <td align='right'>
                                    BB
                                </td>
                            </tr>
                            <tr>
                                <td align='left'>
                                    3476897
                                </td>
                                <td width='93px' align='left'>
                                    My second HTML
                                </td>
                                <td width='94px' align='right'>
                                    $56
                                </td>
                                <td align='right'>
                                    CC
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </table>
      

  15.   


    前辈 这个HTML只是一个引子,,真正的需求格式太长 我发不了 你关注一下 我可以私信给您看看!。不耽搁您工作的情况下。