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
Save values in to Microsoft Excel sheet
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.
Sunny SharmaRank #223 · 11.1K pointsPosted Aug 10, 2013, 12:45 AM EDT
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!