Monday, February 6, 2012

Create Excel file using ASP.Net

Create Excel file  using C#
Here I am going to explain how to create an Excel file from XML file using Microsoft’s Excel 12.0 Object Library to you project. At first you have to create reference of Microsoft Excel 12.2 object library.

Step 1)Go to Project Tab Add reference

Step 2)Goto COM tab  Microsoft Excel 12.2 object library à OK.
Now you have added successfully the Microsoft object library in your project.

Step 3)Add the following namespace in your ASP.Net page
using Excel = Microsoft.Office.Interop.Excel;

Step 4)Add the following coding in button’s onclick event of your page.

            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] = "Sourav Kayal";
 
            xlWorkBook.SaveAs("D://filename.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);
 
            Response.write ("Excel file created in D drive.");
        }


2 comments:

  1. i tried ur suggesstion but it's still not working

    ReplyDelete
  2. I also tried but not working in iis 7

    ReplyDelete