How to create multiple CSV files from web service execution

4

In my program that calls certain web services, I would like that in the final result different CSV files will be created depending on whether the ProjectID is different

In the following code, in the array filter of projectIDs , it calls all the project numbers, and sends it to the final service call.

At this moment the final file is only one containing the 2 different projects.

What I want to do is that for every projectID that exists, a CSV file is created differently 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();  
        }
    
asked by A arancibia 04.03.2016 в 20:27
source

2 answers

0

There are several solutions, one way would be using File.CreateText() to create the different files:

string outCsvFile = string.Format(@"C:\ExportTasksForAllProjects\{0}.csv", ExportTasksForAllProjects + DateTime.Now.ToString("_yyyyMMdd HHmms"));
String newLine = "";
var stream = File.CreateText(outCsvFile);

   foreach (var item in resultGT3)
            {
               newLine  =      item.ProjectID + "," +
                                item.UserID + "," +
                                item.ProjectTitle + ","+
                                item.StartDate;

                      stream.WriteLine(newLine);

             }
     stream.Close();

With this you can create your .csv files containing the values that are contained within resultGT3 :

Update: To create different files per project:

foreach (WS.ProjectMetaData proj in pr)
       {     
        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);
        ...
        ...
        ...
    
answered by 04.03.2016 / 21:08
source
2

What I would recommend is that you use a library like CsvHelper to generate the export

Then you have your call to the service that you dump in a class that you map with CsvHelper

define a class

public class UserExport{
    public string Id {get;set;}
    //otras propiedades
}

and then you would use

WS.UserData[] userData = client.GetUserData(DataSourceId);

List<UserExport> dataExport = new List<UserExport>();
foreach(var item in userData)
{
   dataExport.Add(new UserExport() { Id = item.NombreProp } );
}


using (TextWriter writer = File.CreateText(outCsvFile)                             
{ 
    var csv = new CsvWriter(writer);
    csv.WriteRecords(dataExport);
}
    
answered by 04.03.2016 в 20:41