I am able to save a single value into excel but I need help to save a full list/array into an excel sheet.
Code I have so far:
var MovieNames = session.Query<Movie>()
.ToArray();
List<string> MovieList = new List<string>();
foreach (var movie in MovieNames)
{
MovieList.Add(movie.MovieName);
}
//If I want to print a single value or a string,
//I can use the following to print/save to excel
// How can I do this if I want to print that entire
//list thats generated in "MovieList"
return File(new System.Text.UTF8Encoding().GetBytes(MovieList), "text/csv", "demo.csv");
If you mean you want to create a
.csvfile with all movie names in one column so you can open it in Excel then simply loop over it:Edit
You can add more columns and get fancier with your output but then you run into the problem that you have check for special characters which need escaping (like
,and"). If you want to do more than just a simple output then I suggest you follow @Darins suggestion and use the FileHelpers utilities. If you can’t or don’t want to use them then this article has an implementation of a csv writer.