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

Writing Data to EXCEL Sheet using C#


C# C# 
Ratings:
2 Comment(s)
 37433  View(s)
Rate this:

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

Nice and simple

By Ely Sanders on   3 years ago

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");


Great

By deve chirs on   2 years ago
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 { }

Don't want to miss updates? Please click the below button!

Follow MindStick