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
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 14, 2010

 

Hi Sushmita,

in a senario i need to generate multiple excel files based on a column value ,

EmpNo - EmpName - Dept - Sal - DateOfBirth - Skills - Experience

EmpNo

EmpName

Dept

Sal

DateOfBirth

 Skills

 Experience










in the above  i need to generate different excel files based on the Dept

and i have to save the excel files as the dept name using a ASP.NET application and in the excell i need to protect the EmpNo - EmpName - Dept - Sal (not to allow edit or even not giving any provision to add a new row) now I need to allow the other three columns to edit.



Here I have some problem with the excel I added the dll reference COM Object


Microsoft Excel 12.0 object library VER :1.6

and in my system I have office 20007

I need to generate the file with older excel format which has to support office 2000-2003


but here it is generating .xlsx file, and protection /AllowEditRange is also working fine if I use xlWorkBook.Close() function instead of

xlWorkBook.SaveAs()


in the xlWorkBook.SaveAs() case its saving in older file format but AllowEditRange is not allowing the columns to edit


my req is need to generate .XLS file in office 2000-2003 ,

and specific columns should be editable


and while saving using SaveAs() Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel5 is saving file in old format where as

Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel12 its saving in .XLSX frmat


Code is here :


AllowEditRanges ranges = xlWorkSheet.Protection.AllowEditRanges;

ranges.Add("A1", (Range)xlApp.get_Range((object)"G2", (object)"J" + (rowCtr - 1).ToString()), Type.Missing);

xlWorkSheet.Protect(Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,

Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

string strFilePath = HttpContext.Current.Server.MapPath("~/UploadFileLocation/DownloadExcel/" );

xlWorkBook.SaveAs(strFilePath + FileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel5, misValue, misValue, misValue, misValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);

xlWorkBook.Close( true, Type.Missing, Type.Missing);

xlApp.Quit(); releaseObject(xlWorkSheet); releaseObject(xlWorkBook); releaseObject(xlApp);


Regards
Phanindra TSK

Posted by Phanindra TSK Sep 09, 2010

ThankYou This comment is helpfull for me.

Posted by Piyush Chandrala Aug 05, 2010
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter