-6

I have a method where i pass four parameters, like,

public void loggeneration(DateTime datetime, string projectname, int totallines,int sum,int max)
{
}

within the above method, i have to create a excel sheet(if file is present already, just append the data in the last blank row) and insert the data(datetime, projectname, totallines,sum,max) in a row of five columns.

The first row by default, should be the column names.

I have tried my best, could not find the exact solution. Can somebody provide the solution? Thanks in Advance

Annamalai
  • 55
  • 6
  • there is a good example here to get you started [link](https://stackoverflow.com/questions/10100901/creating-simple-excel-sheet-in-c-sharp-with-strings-as-input) – stuicidle Jun 05 '17 at 12:50
  • Possible duplicate of [creating simple excel sheet in c# with strings as input](https://stackoverflow.com/questions/10100901/creating-simple-excel-sheet-in-c-sharp-with-strings-as-input) – jAC Jun 05 '17 at 12:51

1 Answers1

0

Indeed, this question already exists in stack overflow. By the way you can try this simple solution.

  public void loggeneration(DateTime datetime, string projectname, int totallines, int sum, int max)
    {
        // this is the variable to your xls file 
        string path = @"c:\temp\log.xls";

        // This text is added only once to the file.
        if (!File.Exists(path))
        {
            // Create a file to write to.
            using (StreamWriter sw = File.CreateText(path))
            {
                //once file was created insert the columns
                sw.WriteLine("date;projectname;totallines;sum;max");

            }
        }
        // This text is always added, making the file longer over time
        using (StreamWriter sw = File.AppendText(path))
        {
            sw.WriteLine(String.Format("{0};{1};{2};{3};{4}", datetime, projectname, totallines, sum, max));
        }
    }
  • Thank you for your Answer. The entire Data get entered in a single cell. Do you have any idea to insert the data into separate cells of the sheet – Annamalai Jun 06 '17 at 04:32
  • Hi, you are very welcome. Sorry my mistake, use coma separated values extension instead of xls. replace: string path = @"c:\temp\log.xls"; for string path = @"c:\temp\log.csv"; this may work Good Luck! – Herbert Sales Santos Jun 06 '17 at 11:41
  • Thanks for the solution – Annamalai Jun 13 '17 at 06:20