How to create Excel file in asp.net

Description:-

The following C# code example shows how to use COM interop to create an Excel file. Before going to create new Excel file programmatically in C#, you must have Excel installed on your system for this code to run properly.

Excel Library

To access the object model from Visual C# .NET, you have to add the Microsoft Excel 12.0 Object Library to you project.
Create a new project and add a Command Button to your C# Form.

How to use COM Interop to Create an Excel Spreadsheet

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

Select reference dialogue from Project menu

Select Microsoft Excel 12.0 Object Library and click OK button
 First we have to initialize the Excel application Object.

Excel.Application xlApp = new
Microsoft.Office.Interop.Excel.Application();

Before creating new Excel Workbook, you should check whether Excel is installed in your system.

if (xlApp == null)
{
    MessageBox.Show("Excel is not properly installed!!");
    return;
}

Then create new Workbook

xlWorkBook = xlApp.Workbooks.Add(misValue);

After creating the new Workbook, next step is to write content to worksheet

xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet.Cells[1, 1] = "Sheet 1 content";

In the above code we write the data in the Sheet1, If you want to write data in sheet 2 then you should code like this..

xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(2);
xlWorkSheet.Cells[1, 1] = "Sheet 2 content";

Save Excel file (SaveAs() method)t

After write the content to the cell, next step is to save the excel file in your system.

xlWorkBook.SaveAs("your-file-name.xls");

How to properly clean up Excel interop objects

Finally, we have to properly clean up Excel interop objects or release Excel COM objects. Here we write a function to clean up the Excel Application, Excel Workbook and Excel Worksheet Objects.

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();
    }
}

Source Code:-

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

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        } 
        private void button1_Click(object sender, EventArgs e)
        {
            Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); 
            if (xlApp == null)
            {
                MessageBox.Show("Excel is not properly installed!!");
                return;
            } 
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            object misValue = System.Reflection.Missing.Value; 
            xlWorkBook = xlApp.Workbooks.Add(misValue);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            xlWorkSheet.Cells[1, 1] = "Sheet 1 content"; 
            xlWorkBook.SaveAs("d:\\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 d:\\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 D:   

Related Posts

Previous
Next Post »

Thanks for comments.....