0

I have a StringBuilder object which I have built with comma seperated values, which I would like to save as a .csv file.

I know how to stream the data as CSV, but how can I physically save the data as a .csv file, on the server?

Currently I stream the CSV like the following, where sb is StringBuilder:

Response.Clear()
Response.AddHeader("Content-Disposition", "attachment;filename=List.csv")
Response.ContentType = "text/csv"
Response.Write(sb.ToString)
Response.End()
Curtis
  • 101,612
  • 66
  • 270
  • 352

3 Answers3

0

I assume you already know how to create csv, so check this question that shows how to save stream to file. As you're using ASP.NET just make sure you have the appropriate permissions to modify.

Conclusion

As you want to make the download available to user(instead of save it in the server) and csv is basically a text file check out this

Community
  • 1
  • 1
thiagoleite
  • 503
  • 1
  • 3
  • 11
0

It's not different than writing a normal file. If you want to save the file to a folder in your application, you should use Server.MapPath("~\MyCsvFiles") to get the physical path of the file.

Paolo Tedesco
  • 55,237
  • 33
  • 144
  • 193
  • do you want to save to disk or send to the user for download? The above only is used to get the correct path to the server. Check [this](http://www.codeproject.com/KB/aspnet/textfile.aspx) – thiagoleite Dec 07 '11 at 19:14
0

Depending on the version of IIS, ASPNET or NETWORK SERVICE needs write permissions on the server folder you are writing to; then just write the CSV file out with StreamWriter or your preferred method. If you really want an Excel file, then consider using EPPlus.

Paul
  • 1,483
  • 14
  • 32