Create Excel sheet in C#.net using interop service


Here I am going to explain how to create excel sheet dynamically in c#.net using .net's interop service. Sometimes we need to create excel sheet from provided database's data and we need to save that excel file into a drive. With this interop service we can access each and every functions of Microsoft Excel.

Here I am going to explain how to create one excel file in C#.net using microsoft office interop service.

To Create Excel file follow the below steps :

Step 1 :

Add the Microsoft Excel 12.0 Object Library to your project.

--Create a new website and add a Command Button to the page.
--To add Excel reference library in to your project see below picture.



Step 2 :

Select Add Reference dialogue from Website Menu



Step 3 :

Select Microsoft.Excel 12.0 Object Library and click OK button

Step 4 :

Now add Interop.Excel.dll to your projects bin folder and then add the reference to Interop.Excel.dll

To make reference to Interop.Excel.dll follow the same steps as above but instead of choose COM tab select Browse tab and then you can navigate to your bin folder and then click on Interop.Excel.dll and then click on OK

Download Interop.Excel.dll file.

Then Use the below code to create a simple excel file and save it to c drive :

Code :




protected 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] = "Sample Data";

xlWorkBook.SaveAs("c:\\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);

}

private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;

}
finally
{
GC.Collect();
}
}


Reference: http://dotnetsquare.com/resources/41-create-excel-sheet-in-C-Sharp-using-interop-service


Attachments

  • Create Excel Sheet (42716-20053-CreateExcelFile.zip)
  • Article by Manoranjan Sahoo
    If it helps you then Rate this. Best Regards, Manoranjan Sahoo http://www.dotnetsquare.com Blog: http://blog.msahoo.net

    Follow Manoranjan Sahoo or read 63 articles authored by Manoranjan Sahoo

    Comments

    Guest Author: ravi kumar14 Dec 2012

    sir/mam
    create the excel sheet but it asked for replace the previous excel sheet...
    but i want save in any where in system

    Guest Author: Prajnashree das02 Apr 2013

    How can I fetch column name to the Excel sheet???

    Guest Author: Hania14 Oct 2013

    Try this .NET Excel Library for creating excel file in .NET/VB/NET/C#. Its not a free API but offers free trial so you can try it before purchasing the complete package.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: