CSV formatting has some gotchas. Have you asked yourself these questions:
- Does any of my data have embedded commas?
- Does any of my data have embedded double-quotes?
- Does any of my data have have newlines?
- Do I need to support Unicode strings?
I see several problems in your code above. The comma thing first of all... you are stripping commas:
CsvLine.Append(Format(b.Price, "c").Replace(",", ""))
Why? In CSV, you should be surrounding anything which has commas with quotes:
CsvLine.Append(String.Format(""{0:c}"", b.Price))
(or something like that... my VB is not very good). If you're not sure if there are commas, but put quotes around it. If there are quotes in the string, you need to escape them by doubling them. "
becomes ""
.
b.Title.Replace(""", """")
Then surround this by quotes if you want. If there are newlines in your string, you need to surround the string with quotes... yes, literal newlines are allowed in CSV files. It looks weird to humans, but it's all good.
A good CSV writer requires some thought. A good CSV reader (parser) is just plain hard (and no, regex not good enough for parsing CSV... it will only get you about 95% of the way there).
And then there is Unicode... or more generally I18N (Internationalization) issues. For example, you are stripping commas out of a formatted price. But that's assuming the price is formatted as you expect it in the US. In France, the number formatting is reversed (periods used instead of commas, and vice versa). Bottom line, use culture-agnostic formatting wherever possible.
While the issue here is generating CSV, inevitably you will need to parse CSV. In .NET, the best parser I have found (for free) is Fast CSV Reader on CodeProject. I've actually used it in production code and it is really really fast, and very easy to use!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…