In my program that calls certain web services, I would like different CSV files to be created in the final result depending on whether the ProjectID
is different
In the following code, in the array filter it projectIDs
calls all the project numbers, and sends it to the final service call.
Right now the final file is just one file containing the 2 different projects.
What I want to do is that for each projectID that exists, a different CSV file is created and therefore downloaded.
How can I do this?
Here is the code:
Thank you very much.
string ExportTasksForAllProjects = "ExportTasksForAllProjects";
string outCsvFile = string.Format(@"C:\\ExportTasksForAllProjects\\{0}.csv", ExportTasksForAllProjects + DateTime.Now.ToString("_yyyyMMdd HHmms")); //+ DateTime.Now.ToString("_yyyyMMdd HHmms") + "_" + Guid.NewGuid() + );
String newLine = "";
var stream = File.CreateText(outCsvFile);
WS.UserData[] userDataId = client.GetUserData(DataSourceId);
List<string> multiUserIDs = new List<string>();
foreach (var userdata in userDataId)
{
multiUserIDs.Add(userdata.List[0].ToString());
}
foreach (WS.ProjectMetaData proj in pr)
{
string temp = "";
var AllProjectIds = proj.ProjectID;
string[] projectIDs = new string[] { AllProjectIds }; // all projects
WS.TaskEntry[] resultGT3 = client.GetTasks3(projectIDs, multiUserIDs.ToArray());
foreach (var item in resultGT3)
{
newLine = item.ProjectID + "," +
item.UserID + "," +
item.ProjectTitle + "," +
item.StartDate;
stream.WriteLine(newLine);
}
stream.Close();
}
What I would recommend is that you use a library such as CsvHelper to generate the export
So you have your service call that you dump into a class that you map with CsvHelper
you define a class
and then you would use
There are several solutions, one way would be using
File.CreateText()
to create the different files:With this you can create your files
.csv
containing the values that are contained insideresultGT3
:Update: To create different files per project: