解决方案 »

  1.   

    这个纵横一起合并,咋弄呢。我自己写了个拼接字符串的横向合并方法,感觉效率不高
    string TableHtml(DataTable dt)
        {
            System.Text.StringBuilder html = new System.Text.StringBuilder();
            foreach (DataRow row in dt.Rows)
            {
                html.Append("<tr>");
                //重复次数
                int repeatTimes = 1;
                //重复字段
                string repeatString = string.Empty;
                for (int i = 0;i < row.ItemArray.Length; i++)
                {
                    if (repeatString == string.Empty)
                    {
                        if (row.ItemArray[i].ToString() == string.Empty)
                            repeatTimes++;
                        repeatString = row.ItemArray[i].ToString();
                        continue;
                    }
                    if (row.ItemArray[i].ToString() == repeatString)
                    {
                        repeatTimes++;
                        if (i == row.ItemArray.Length - 1)
                        {
                            html.Append(string.Format("<td colspan=\"{0}\">{1}</td>", repeatTimes, repeatString));
                            repeatTimes = 1;
                            repeatString = string.Empty;
                        }
                    }
                    else
                    {
                        if (repeatTimes > 1)
                        {
                            if (i == row.ItemArray.Length-1)
                            {
                                html.Append(string.Format("<td colspan=\"{0}\">{1}</td>", repeatTimes+1, repeatString));
                                repeatTimes = 1;
                                repeatString = string.Empty;
                                continue;
                            }
                            html.Append(string.Format("<td colspan=\"{0}\">{1}</td>", repeatTimes, repeatString));
                            repeatTimes = 1;
                        }
                        else
                        {
                            if (i == row.ItemArray.Length-1)
                            {
                                html.Append(string.Format("<td>{0}</td>", repeatString));
                                html.Append(string.Format("<td>{0}</td>", row.ItemArray[i].ToString()));
                                repeatString = string.Empty;
                                continue;
                            }
                            html.Append(string.Format("<td>{0}</td>", repeatString));
                        }
                        repeatString = row.ItemArray[i].ToString();
                    }
                }
                html.Append("</tr>");
            }
            return html.ToString();
        }
      

  2.   

    发现一个bug,当字段为NULL或者String.Empty 的时候会出问题,自己画蛇添足了string TableHtml(DataTable dt)
        {
            System.Text.StringBuilder html = new System.Text.StringBuilder();
            foreach (DataRow row in dt.Rows)
            {
                html.Append("<tr>");
                //重复次数
                int repeatTimes = 1;
                //重复字段
                string repeatString = string.Empty;
                for (int i = 0;i < row.ItemArray.Length; i++)
                {
                    string NowString = row.ItemArray[i] == null ? string.Empty : row.ItemArray[i].ToString();
                    if (NowString == repeatString)
                    {
                        repeatTimes++;
                        if (i == row.ItemArray.Length - 1)
                        {
                            html.Append(string.Format("<td colspan=\"{0}\">{1}</td>", repeatTimes, repeatString));
                            repeatTimes = 1;
                            repeatString = string.Empty;
                        }
                    }
                    else
                    {
                        if (repeatTimes > 1)
                        {
                            if (i == row.ItemArray.Length-1)
                            {
                                html.Append(string.Format("<td colspan=\"{0}\">{1}</td>", repeatTimes+1, repeatString));
                                repeatTimes = 1;
                                repeatString = string.Empty;
                                continue;
                            }
                            html.Append(string.Format("<td colspan=\"{0}\">{1}</td>", repeatTimes, repeatString));
                            repeatTimes = 1;
                        }
                        else
                        {
                            if (i == row.ItemArray.Length-1)
                            {
                                html.Append(string.Format("<td>{0}</td>", repeatString));
                                html.Append(string.Format("<td>{0}</td>", NowString));
                                repeatString = string.Empty;
                                continue;
                            }
                            html.Append(string.Format("<td>{0}</td>", repeatString));
                        }
                        repeatString = NowString;
                    }
                }
                html.Append("</tr>");
            }
            return html.ToString();
        }