Which is Efficient StringBuilder or CommaDelimitedStringCollection
I am creating a CSV file and I am using StringBuilder at the moment I come to a new Class "CommaDelimitedStringCollection".
Can anyone tell me which is betterto use to write the file data. also what d开发者_JAVA百科o you think of using TextWriter.
Thanks
If writing a CSV were a very complex task, I might rely on helpers like CommaDelimitedStringCollection
. However, it isn't complicated, so there's no reason to introduce an extra helper class in here along with the associated perf hit.
If I needed to build a string in memory for some reason, such as implementing a general ToString()
method, I would use StringBuilder
instead of straight concatenation. However, in this case you are writing to a stream of some kind, so you don't need this extra step either.
TextWriter
is an abstract class, but you want the hierarchy of classes therein, specifically StreamWriter
. This class has a number of constructor overloads; one takes a file name, if you want to write to a local file, and another takes a Stream
object, if you want to write directly to a stream (for example Response.OutputStream
in ASP.NET).
I actually did a quick Google search and there don't appear to be any good examples of actually implementing a CSV download, if that's what you're trying to do, so here is one:
void ExportCsvAttachment(IEnumerable<Customer> customers)
{
Response.Clear();
Response.ContentType = "text/plain";
Response.AddHeader("Content-Disposition: attachment;filename=Customers.csv");
using (StreamWriter writer = new StreamWriter(Response.OutputStream))
{
WriteCsv(writer, customers);
}
Response.End();
}
void WriteCsv(TextWriter writer, IEnumerable<Customer> customers)
{
foreach (Customer cust in customers)
{
writer.Write(cust.ID);
writer.Write(',');
WriteCsvString(writer, cust.Name);
writer.Write(',');
WriteCsvString(writer, cust.PhoneNumber);
}
}
void WriteCsvString(TextWriter writer, string s)
{
writer.Write('"');
writer.Write(s.Replace("\"", "\"\""));
writer.Write('"');
}
This uses the most common escaping mechanism for CSV - quotes around strings, another quote as an escape character.
If you just want to export to a local file instead, get rid of all the Response
lines in ExportCsvAttachment
and use the StreamWriter
constructor that takes a path.
Definitely go with a TextWriter for ASP.Net. The trick here is to make sure the textwriter you're thinking about is directly tied to the output/response stream. This way you don't build up your entire contents of your csv response in memory, but rather limit it to the size of the output buffer.
any time you can use a stream, such as textwriter, to push your data to a file you should choose that option.
if you have to build it then i would use stringbuilder. that is what it is for, but you are retaining the product in memeory and incurring performance penalties when appending.
anything to do with a collection should throw up a red flag, UNLESS it's use is unavoidable.
just my 2 pesos.
I have never used the CommaDelimitedStringCollection but after giving it a short glance in the documentation and trying it a bit I would guess that it's purpose is to represent one line in a csv file, maybe I'm wrong about this. But the ToString() give one csv seperated line of text with the values in the collection, this can be very usefull when reading csv files but I'm not sold on its usefullness when building a whole new file with header rows.
Since you probably want to have headers and more than a single line of data I would recommend using the StringBuilder, and my guess is that the CommaDelimitedStringCollection uses stringbuilder to generate it's own string value in ToString()
The full name of the class is System.Configuration.CommaDelimitedStringCollection. This does not appear to be a general-purpose CSV class, or it would be in System.Collections or System.IO.
精彩评论