Home > Back-end >  StreamWriter is not adding empty cells correctly
StreamWriter is not adding empty cells correctly

Time:06-06

I have a C# program that is supposed to generate an output that looks like this:

Correct output, starting one column away from edge, with two columns between each block

However, my output currently looks like this:

Current output, with seemingly random amounts of columns between each block

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. 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?

CodePudding user response:

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; 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. You might choose something like SpreadsheetML instead. That will let you do things like set the column widths and bold font weight for the headers.

  • Related