简体   繁体   English

StreamWriter 未正确添加空单元格

[英]StreamWriter is not adding empty cells correctly

I have a C# program that is supposed to generate an output that looks like this:我有一个 C# 程序应该生成如下所示的 output:

正确的输出,从边缘一列开始,每个块之间有两列

However, my output currently looks like this:然而,我的 output 目前看起来是这样的:

当前输出,每个块之间的列数看似随机

This is what my code looks like at the moment:这就是我的代码目前的样子:

System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);

using (StreamWriter writer = new StreamWriter(outputPath, false, new UTF8Encoding(true)))
{
    writer.WriteLine("");
    writer.WriteLine("");

    writer.WriteLine(String.Join(",", ",", "H2 2020", ",", ",", ",", "H1 2021", ",", ",", ",", "H2 2021"));
    writer.WriteLine(String.Join(",", ",", "Searches", "Impressions", "Net Cost", ",", ",", "Searches", "Impressions", "Net Cost", ",", ",", "Searches", "Impressions", "Net Cost" ));

    foreach (OutputRow outputRow in outputRows)
    {
        writer.WriteLine(String.Join(",", outputRow.Brand, outputRow.SearchesA, outputRow.ImpressionsA, outputRow.NetCostA, ",", ",", outputRow.Brand, outputRow.SearchesB, outputRow.ImpressionsB, outputRow.NetCostB, ",", ",", outputRow.Brand, outputRow.SearchesC, outputRow.ImpressionsC, outputRow.NetCostC));                    
    }
} 

After reading other answers on here about skipping cells/adding empty cells in a CSV writer, all one should have to do is add a string with a comma where they want to have an empty cell.在这里阅读了关于在 CSV 作者中跳过单元格/添加空单元格的其他答案后,所有需要做的就是在他们想要空单元格的地方添加一个带逗号的字符串。 As you can see, I've gotten the top two rows to look acceptable even though the amount of strings of commas does not match up with the amount of empty cells between the strings of words.如您所见,即使逗号字符串的数量与单词字符串之间的空单元格数量不匹配,我也让前两行看起来可以接受。 My question is: What can I do to add the correct amount of empty cells to make this look like the correct output I've posted and why do my comma strings not generate the correct amount of empty cells?我的问题是:我该怎么做才能添加正确数量的空单元格,使它看起来像我发布的正确 output,为什么我的逗号字符串不能生成正确数量的空单元格?

When using String.Join() , the first argument is the separator, which is added between each of the additional string arguments. Don't try to include other commas as part of the separated strings;使用String.Join()时,第一个参数是分隔符,添加在每个附加字符串 arguments 之间。不要尝试将其他逗号作为分隔字符串的一部分; use empty strings as placeholders instead.使用空字符串作为占位符。 It will be MUCH easier to count the cells that way.这样计数细胞会容易得多。

using (StreamWriter writer = new StreamWriter(outputPath, false, new UTF8Encoding(true)))
{
    writer.WriteLine("");
    writer.WriteLine("");

    //                                A   B   C          D   E   F   G   H   I          J   K   L   M   N   O
    writer.WriteLine(String.Join(",", "", "", "H2 2020", "", "", "", "", "", "H1 2021", "", "", "", "", "", "H2 2021"));

     //                               A   B   C           D              E           F   G   H   I           J              K           L   M   N   O           P              Q
    writer.WriteLine(String.Join(",", "", "", "Searches", "Impressions", "Net Cost", "", "", "", "Searches", "Impressions", "Net Cost", "", "", "", "Searches", "Impressions", "Net Cost" ));

    foreach (OutputRow outputRow in outputRows)
    {
        writer.WriteLine(String.Join(",", "", outputRow.Brand, outputRow.SearchesA, outputRow.ImpressionsA, outputRow.NetCostA, "", "", outputRow.Brand, outputRow.SearchesB, outputRow.ImpressionsB, outputRow.NetCostB, "", "", outputRow.Brand, outputRow.SearchesC, outputRow.ImpressionsC, outputRow.NetCostC));                    
    }
}

But CSV seems like a generally poor fit for this data.但是 CSV 似乎不太适合这个数据。 You might choose something like SpreadsheetML instead.您可能会选择类似SpreadsheetML的东西。 That will let you do things like set the column widths and bold font weight for the headers.这将使您可以执行诸如设置标题的列宽和粗体字重之类的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM