Skip to content

Save values in to Microsoft Excel sheet

KRISHNACHANDRAN NAIR

Asked Aug 9, 2013, 10:45 PM EDT1 answer
Share
LinkedInX
I have to save values in array in to Microsoft Excel sheet.I am using Visual studio 2010 and Microsoft office 2007 kindly give any help

Regards Krishnachandran

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Sunny  Sharma
    0
    Hi Krishnachandran,

    Add reference to Microsoft.Office.Interop.Excel & System.Refelection in your project and use the code below:
    ---------------------------------------------------------------
    Missing missing = Missing.Value; //Add reference to System.Reflection
    Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application(); //Add reference to Microsoft.Office.Interop.Excel
    Workbook wkb = ExcelApp.Workbooks.Add(missing);
    Worksheet wks = (Worksheet) wkb.Sheets[1];
    string[] myValues = new[] { "Value1", "Valu2", "Value3", "Value4" };
    for (int i = 1; i <= myValues.Length;i++)
    {
    ((Range)wks.Cells[i, 1]).Value = myValues[i];
    }

    wks.SaveAs("D:\\myFileName", missing, missing, missing, missing, missing, missing, missing, missing, missing);
    wks = null;
    wkb = null;
    ExcelApp.Quit();
    --------------------------------------------------------------------

    Happy Coding :)
    Cheers!
    0