CSharp.Net-Informations.com

  How to open an Excel file in C#




   Categories

    HOME
    VB.NET
    CSHARP


   














The following section explains how to open and read an Excel file through C#.

For open or read an Excel file in C# , first you have to add the Microsoft Excel 12.0 Object Library in you project.

Create a new project and add a Command Button to the Form.

Form the following images you can find how to add Excel reference library in your project.

Select Reference Dialogue from Project menu

scsharp-excel-reference

Select Microsoft Excel 12.0 Object Library and click OK button

csharp-excel-library

Copy and paste the following source code in your C# project file.


         C# Source Code Download           Print Source Code
         How to open an Excel file in C# - Download
        
C# Tutorial

using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel; 

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Excel.Application xlApp ;
            Excel.Workbook xlWorkBook ;
            Excel.Worksheet xlWorkSheet ;
            object misValue = System.Reflection.Missing.Value;

            xlApp = new Excel.ApplicationClass();
            xlWorkBook = xlApp.Workbooks.Open("csharp.net-informations.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            MessageBox.Show(xlWorkSheet.get_Range("A1","A1").Value2.ToString());

            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();

            releaseObject(xlWorkSheet);
            releaseObject(xlWorkBook);
            releaseObject(xlApp);
        }

        private void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                MessageBox.Show("Unable to release the Object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
        } 
    }
}
		

Note : You have to add Microsoft.Office.Interop.Excel to your source code.

using Excel = Microsoft.Office.Interop.Excel;

When you execute the C# source code , it will open the excel file and read the content in the cell A1

CSharp Excel Related Contents
*     How to create Excel file in C#
*     How to read an Excel file in CSharp
*     How to format an Excel file using C#
*     How to insert a picture in excel from C# App
*     How to insert a background picture in excel
*     How to create Excel Chart from C#
*     How to export excel chart from C#
*     How to excel chart in C# picturebox
*     C# data validation input box in excel file
*     How to read from an Excel file using OLEDB
*     How to insert data to Excel file using OLEDB
*     How to update data in Excel file using OLEDB
*     How to export databse to excel file
*     How to export DataGridView to excel file

CSharp Related Topics
*     An overview of Microsoft CSharp
*     C# Language Tutorial
*     C# Statements Tutorial
*     C# Collection Tutorial
*     C# String Tutorial
*     C# File Operations Tutorial
*     C# Excel Tutorial
*     C# Crystal Reports Tutorial
*     CSharp Communication Tutorial
*     C# Ado.Net Tutorial and Source Code
*     C# ADO.NET data Providers Tutorial
*     C# Dataset Tutorial
*     C# DataAdapater Tutorial
*     Csharp DataView Tutorial
*     Csharp Remoting Tutorial
*     C# XML Tutorial
Search here for more CSharp Source Code :

  |  Home   |  SiteMap   |  About   |
net-informations.com (C) 2010 All Rights Reserved