c#将2D数组转换为html表格

本文关键字:html 表格 转换 数组 2D | 更新日期: 2023-09-27 18:05:31

我有一个二维字符串数组,我想把它转换成一个html表。当我运行下面的代码,它需要很多时间,因为数组的巨大长度。

  string data = string.Empty;
        string table = string.Empty;
        #region new code
        data += "<div class='"tab-content'">";

        table = " <table class='"table data-table'">";
        table += "<tbody>";
        for (int row = 1; row < Data.GetLength(0); row++)
        {
            table += "<tr>";
            for (int column = 1; column < Data.GetLength(1); column++)
            {
                try
                {
                    table += "<td>" + Data[row, column].ToString() + "</td>";
                }
                catch
                {
                    table += "<td></td>";
                }
            }
            table += "</tr>";
        }
        table += "</tbody>";

        table += "</table>";

c#将2D数组转换为html表格

尝试使用StringBuilder。当你这样做时,你会创建多个字符串实例因为字符串是不可变的

根据try { }爆发的频率,我会尝试事先诊断索引是否合法。当Try-Catch发生很多次时,真的很昂贵。

[Edit:]等等,它不应该是索引,你正在检索边界。这个Try-Catch是一个null检查的替代品吗?