Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export list<float> into excel file

I have a List contains all float values( lets say List<float> avgDifClosedLoop; )

Now to do more process I want to export them in excel. how can I do this through c#?

like image 283
farzin parsa Avatar asked Aug 15 '26 09:08

farzin parsa


2 Answers

You can do this the very complicated way with COM Interop services and write directly to an instance of Excel. With this method you can call worksheet functions as though you were in Excel itself and coding in VBA (but better).

The quick and easy way to do this, and depending on what else you need to do and how many times you need to do it, is to write your list out as a CSV file a la

using (StreamWriter sw = new StreamWriter(outputFile))
{
    List<float> f = new List<float>();
    StringBuilder sb = new StringBuilder();
    foreach (var item in f)
    {
        sb.AppendLine(item.ToString());
    }
    string linetoWrite = String.Join(",", sb);
    sw.WriteLine(linetoWrite);
}

Then just open it in Excel and move on with your life

like image 113
Brad Avatar answered Aug 16 '26 23:08

Brad


As I see it you have two options:

  1. Use Excel Interop.

    This allows you to control exactly where and how the values get output, but it requires Excel to be installed on the system executing the code and more skill in coding.

  2. Write the values to a CSV File.

    Excel can read CSV files by default. This method does not require anything more complex than writing a text file and does not require Excel to be installed on the generating system. However you loose some control over how the files are output into the worksheet.

like image 24
Joshua Drake Avatar answered Aug 16 '26 22:08

Joshua Drake