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
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sunny SharmaPosted Aug 10, 2013, 4:45 AM
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!