articles

Home / DeveloperSection / Articles / Writing Data to EXCEL Sheet using C#

Writing Data to EXCEL Sheet using C#

Writing Data to EXCEL Sheet using C#

Sanjay Singh 84632 05-Jan-2012

Hi Friends, today into this Article I am going to explain how to write data into an excel sheet using C#. First of all right click on References-> Add references-> Choose COM Tab-> Select Microsoft Excel 12.0 Object Library and write the following code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Core;
using Excel= Microsoft.Office.Interop.Excel;
 
namespace EXCEL_DATA
{
    class Program
    {
      
        static void Main(string[] args)
        {
            Excel._Application myExcelApp;
            Excel.Workbooks myExcelWorkbooks;
            Excel.Workbook myExcelWorkbook;
           // Excel ._Worksheet myExccelWorksheetToChange;
            object misValue = System.Reflection.Missing.Value;
 
            myExcelApp = new Excel.ApplicationClass();
             myExcelApp.Visible = true;
 myExcelWorkbooks = myExcelApp.Workbooks;
 String fileName = "D:\\book1.xls";
  myExcelWorkbook = myExcelWorkbooks.Open(fileName, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue,misValue,misValue);                
 
 Excel.Worksheet myExcelWorksheet = (Excel.Worksheet)myExcelWorkbook.ActiveSheet;
 
 String cellFormulaAsString = myExcelWorksheet.get_Range("A2", misValue).Formula.ToString();
 
 myExcelWorksheet.get_Range("A1", misValue).Formula = Console.ReadLine();
                       
        }
    }
} 

Output:

First Enter Something…..

Writing Data to EXCEL Sheet using C#

 

Press ‘Enter’ key, data entered by you will display on Excel Worksheet.

Writing Data to EXCEL Sheet using C#

 

X

c# excel 
Updated 15-Mar-2020

Leave Comment

Comments

Liked By