Friday, April 24, 2015

C# Excel VBA Side by Side Putting a value in a cell

To enter a value in cell, use the value property of range object.  A range object can consist of a cell or range of cells.  One example is the following

C#

Prior to running this code, you will need to  follow these steps in 


using Excel=Microsoft.Office.Interop.Excel;
    class Program
    {
        static void Main(string[] args)
        {
            Excel.Application objExcel=new Excel.Application();
            //Excel.Worksheet objWks;
            try
            {  
                objExcel.Visible=true;
                var objWb = objExcel.Workbooks.Add();

                Excel.Worksheet objWks = objWb.Worksheets[1] as Excel.Worksheet;
                
                objWks.Cells[1, 1].value = 1;
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if (objExcel !=null)
                {
                    objExcel=null;
                }
            }
        }
    }

No comments: