It doesn't seem that your program writes the data you need. You probably expect the following output
_Dum,_Vo,_Bi,_Da<NewLine>
<ValueOf_Dum>,<ValueOf_Vo>,<ValueOf_Bi>,<ValueOf_Da><NewLine>
Note that I specifically added <NewLine>
to the output. <NewLine>
stands for the newline character(s), which depending on your OS is either "\n"
(on Unix systems) or "\r\n"
(on Windows systems). In .NET there is also Environment.NewLine
, that returns the platform-specific new line characters. Note that there is no CSV "standard", so it's your choice wich new line characters you want to use. It seems that you are not clear on that, so you first have to make that decision.
The next thing you have to consider is the difference between StringBuilder.Append
and StringBuilder.AppendLine
. Append
simply adds the specified text at the end of the string, while AppendLine
adds the text, followed by the characters returned by Environment.NewLine
.
In your code you consistently use AppendLine
which can not be what you want, since you obviously want to construct lines and add them as a whole. The way you do it, you consistently add new line characters between some of your values, which in CSV means the start of a new record.
You not only inadvertently add new line characters to your file, you are also not consistent in the new line characters you manually add. In the title row you use "\r"
(which is not OK in Unix and in Windows). Later you use "\r\n\n"
(even followed by the new line added by AppendLine
), which does not work well in Unix (because of the "\r"
at the beginning) or in Windows (because of the "\n"
without a preceding "\r"
at the end).
Your next problem is this part of your code:
if (data._Stu == "B" || data._Stu == "E")
{
builder.AppendLine(data._Vo + "," + data._Bi + "," + data._Dau);
};
You are adding the values for _Vo
, _Bi
and _Dau
when _Stu
is "B"
or "E"
. And what do you do when _Stu
has any other value? Nothing! That means you generate an incomplete record there, which is the most likely reason for the problem you describe. You need to make up your mind what should happen in the else case and incorporate it into your program.
In general you seem to not be fully clear on the intended flow of your program, the CSV format and what the instructions you are using are doing in your code. To solve your problem you need to do the following steps:
- Get a clear understanding of the desired output and how a program must look that generates that output.
- Understand the CSV format and the significance of a line break in it.
- Decide about the new line format you want to use (
"\r\n"
, "\n"
or Environment.NewLine
) and use it consistently throughout your program.
- Use
Append
where you want to add just the text and AppendLine
when you want the added text to be followed by Environment.NewLine
.
- Clear up what should happen when
Stu
is not "B"
or "E"
and add this case to your program.
- Step through your program in a debugger to confirm that it is doing exactly what you inted it to do.