Writing Data to EXCEL Sheet using C#

 

Here I am going to explain how to write data into 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
  1. Hi,
    nice article for intruducing Excel Interop!

    For ones that are unable to use it, try this C# Excel library that doesn't require Excel application.
    Here is a sample how to read and write Excel file in C#:

    // Load Excel file.
    var workbook = ExcelFile.Load("Book1.xls");

    // Select active worksheet.
    var worksheet = workbook.Worksheets.ActiveWorksheet;

    // Change the value of the cell "A1".
    worksheet.Cells[
    "A1"].Value = "Hello world!";

    // Save the file in XLS format.
    workbook.Save(
    "New Book1.xls");

  1. Hi, all

    I'm currently work with my web application ,what i need  is to create and open Excel files without installing MS Excel on either developer or client machine.So I can't use Microsoft.Office.Interop.Excel in my project.I googled around and found C# Excel component working smoothly. Below is code snippet for createing Excel file.


                //Initialize a new Workboook object
                Workbook workbook = new Workbook();
                //Get the first worksheet
                Worksheet sheet = workbook.Worksheets[0];
                //Write string values in a cell
                sheet.Range["A3"].Text = "Hello";
                //Save workbook to disk
                workbook.SaveToFile("Sample.xls");
                try
                {
                    System.Diagnostics.Process.Start(workbook.FileName);
                }
                catch { }

Leave Comment