CSharp.Net-Informations.com
   Home      .Net Framework      VB.NET      C#                                                                      About


  How to create Excel file in C#

The following section explains how to create an Excel file programmatically using C#.

Before you create an Excel file in C# , you have to add the Microsoft Excel 12.0 Object Library to you project.

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

Form the following pictures 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 create Excel file in C# - Download
        
C# Tutorial

using System;
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.Add(misValue);

            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            xlWorkSheet.Cells[1, 1] = "http://csharp.net-informations.com";

            xlWorkBook.SaveAs("csharp-Excel.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();

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

            MessageBox.Show("Excel file created , you can find the file c:\\csharp-Excel.xls");
        }

        private void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                MessageBox.Show("Exception Occured while releasing 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 this program , it will create an excel file in you c:

CSharp Excel Related Contents
*     How to open an 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


   Home      VB.NET      C#
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
*     C# DataGridView Tutorial
   Home      VB.NET      C#
More Source Code :   
Mail to :  feedback@net-informations.com
  |  Home   |  VB.NET   |  C#   |  SiteMap   |  Terms of Use   |  About   |
net-informations.com (C) 2010
All Rights Reserved. All other trademarks are property of their respective owners.