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…..
Press ‘Enter’ key, data entered by you will display on Excel Worksheet.
X
deve chirs
13-Jul-2014I'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.
Ely Sanders
21-Aug-2013Hi,
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#: