SIGN UP MEMBER LOGIN:    
ARTICLE

Generating Excel Reports using ASP.NET and C#

Posted by Sushmita Kumari Articles | Office Development December 29, 2005
This article and code snippet shows how to generate Excel reports from ASP.NET using Excel objects in .NET Framework.
Reader Level:

When we talk about reporting, Crystal Reports and Excel are very common reporting formats to generate and view reports. Many developers may think, Excel reporting means writing macros and do some Office/Excel VBA programming. Those days are long gone.

.NET Framework class library provides Office Interoperability class that can be used as other .NET classes to generate Excel reports. All Excel and Office Interoperability related functionality is defined in Excel and Microsoft.Office.Core namespaces.

The following code shows how to use Microsoft Excel objects from Microsoft Visual Studio using C# .NET.

using Excel ;
using
Microsoft.Office.Core;
#region
Variable Decleration
private
Excel.Application oExcelApp;
private
Excel.Workbooks oBooks;
private
Excel._Workbook oBook;
private
Excel._Worksheet oSheet;
private
Excel.Range oexcelRange ;
private
Excel.Characters ocharacter ;
private object
oMissing;
oMissing = system.Reflection.Missing.Value;
//To specify the optional values for the method available inside the Excel Namespace.
#endregion

The following code shows how to use Excel and other objects. This code creates a new Excel.Application, gets WorkBooks, and open it. After that, it creates Worksheets.

oExcelApp=new Excel.Application();
oExcelApp.Visible =
false
;
oBooks = m_oExcelApp.Workbooks;
oMissing = System.Reflection.Missing.Value;
oExcelApp.Visible =
false
;
oBook=oBooks.Open("File Name",oMissing,oMissing,oMissing,oMissing,oMissing,oMissing, oMissing,oMissing,oMissing,oMissing,oMissing,oMissing);
oExcelApp.Visible =
false
;
oExcelApp.UserControl =
false
;
oSheet = (Excel._Worksheet)oBook.Worksheets["sheet Name"];
oSheet.Visible = Excel.XlSheetVisibility.xlSheetVisible;
oSheet.Unprotect(oMissing);

The following code shows how to use Excel.Range object:

oexcelRange =((Excel.Range)m_oSheet.Cells[4,1]);

The following code shows how to use Excel.Characters. In this way we can do the manipulation of the partial text of a particular cell.

ocharacter = oexcelRange.get_Characters ("1", "5");
//1 and 5 is the start and end index of the selected text of the cell which is the value of the excelRangeobject.
ocharacter.Font.ColorIndex=Excel.XlColorIndex.xlColorIndexAutomatic;
ocharacter.Font.Bold=
true;

Login to add your contents and source code to this article
share this article :
post comment
 

hi give me your email id

Posted by syed hasan Aug 20, 2011

Hi,
I am unable to get this namespace
using Microsoft.Office.Core;

Posted by Manish Garg Oct 22, 2010

Hey im new to this could u please tell me this

ive got a table and any updated to this is captures in a new table2.

all I new is to generate any update's made in an excel.


how do i do this.

i only need to show the updates in the excel .


thanks for the help.

Posted by justin russ Oct 15, 2010

Hey im new to this could u please tell me this

ive got a table and any updated to this is captures in a new table2.

all I new is to generate any update's made in an excel.


how do i do this.

i only need to show the updates in the excel .


thanks for the help.

Posted by justin russ Oct 14, 2010

Hey im new to this could u please tell me this

ive got a table and any updated to this is captures in a new table2.

all I new is to generate any update's made in an excel.


how do i do this.

i only need to show the updates in the excel .


thanks for the help.

Posted by justin russ Oct 14, 2010
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Gauge for SharePoint
Become a Sponsor