0

我有一个多维数组,我想将它写入 .csv,当它在 Excel 中显示时,我希望它具有相同的矩阵式顺序。

        string[,] output = new string[,] { {"n=", "128", "256", "512", "1024", "2048", "4096", "8192", "16384", "32768", "65536", "131072", "262144", "524288", "1048576", "2097152", "4194304", "8388608"},
                                           { "Selection", timeSelection128, timeSelection256, timeSelection512, timeSelection1024, timeSelection2048, timeSelection4096, timeSelection8192, timeSelection16384, timeSelection32768, timeSelection65536, timeSelection131072, timeSelection262144, timeSelection524288, timeSelection1048576, timeSelection2097152, timeSelection4194304, timeSelection8388608 },
                                           { "Insertion", timeInsertion128, timeInsertion256, timeInsertion512, timeInsertion1024, timeInsertion2048, timeInsertion4096, timeInsertion8192, timeInsertion16384, timeInsertion32768, timeInsertion65536, timeInsertion131072, timeInsertion262144, timeInsertion524288, timeInsertion1048576, timeInsertion2097152, timeInsertion4194304, timeInsertion8388608 },
                                           { "Merge", timeMerge128, timeMerge256, timeMerge512, timeMerge1024, timeMerge2048, timeMerge4096, timeMerge8192, timeMerge16384, timeMerge32768, timeMerge65536, timeMerge131072, timeMerge262144, timeMerge524288, timeMerge1048576, timeMerge2097152, timeMerge4194304, timeMerge8388608 },
                                           { "Quick", timeQuick128, timeQuick256, timeQuick512, timeQuick1024, timeQuick2048, timeQuick4096, timeQuick8192, timeQuick16384, timeQuick32768, timeQuick65536, timeQuick131072, timeQuick262144, timeQuick524288, timeQuick1048576, timeQuick2097152, timeQuick4194304, timeQuick8388608 } };`

那是我的多维数组,现在我正在做这样的事情...... [5x18]

StringBuilder sb = new StringBuilder();
        for (int x = 0; x < 5; x++)
        {
            for (int y = 0; y < 18; y++)
            {
                sb.AppendLine(string.Join(delimiter, output[x,y]));
            }
            File.AppendAllText(filePath, sb.ToString());
        }

但这只是给了我一个excel文件,第一列中的所有内容一个接一个。我需要数组中的每一行都是 Excel 中的一行,等等。

4

2 回答 2

0

String.Join本身已经加入了一个字符串列表。不需要循环。这应该这样做:

StringBuilder sb = new StringBuilder();
for (int x = 0; x < 5; x++)
    sb.AppendLine(String.Join(delimiter, output[x]));

File.AppendAllText(filePath, sb.ToString());

你可能是对的 - 你不能像我想的那样在多维数组上使用上面的代码。

然后你必须手动完成:

StringBuilder sb = new StringBuilder()
for (int x = 0; x < 5; x++)
{
    StringBuilder lineBuilder = new StringBuilder();
    for (int y = 0; y < 18; y++)
    {
        if (lineBuilder.Count > 0)
            lineBuilder.Append(delimiter);
        lineBuilder.Append(output[x,y]);
    }

    sb.AppendLine(lineBuilder);
}

File.AppendAllText(filePath, sb.ToString());
于 2013-11-11T12:26:39.403 回答
0

好吧,检查输出文件与使用 excel 生成的文件并查看差异。如果没有视觉差异,那么您可以尝试使用sb.Append(string.Concat("bla", "\r\n")).

于 2013-11-11T12:23:11.617 回答