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");
ocharacter.Font.ColorIndex=Excel.XlColorIndex.xlColorIndexAutomatic;
ocharacter.Font.Bold=true;

syed hasanPosted Aug 20, 2011, 7:14 AM
hi give me your email id
Manish GargPosted Oct 22, 2010, 6:54 AM
Hi, I am unable to get this namespace using Microsoft.Office.Core;
justin russPosted Oct 14, 2010, 2:12 PM
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.
Phanindra TSKPosted Sep 9, 2010, 12:13 PM
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
Piyush ChandralaPosted Aug 5, 2010, 1:21 AM
ThankYou This comment is helpfull for me.
ravi singhPosted Feb 5, 2010, 12:36 AM
i am getting error in this code ... ..can u plzz help me .. Error 1 Cannot create an instance of the abstract class or interface 'Excel.Application' D:\excelReport\WindowsApplication1\logic\CreateExcelDoc.cs 26 23 ExcelReport Error 2 'Excel.Application.Workbooks(object)' is a 'method', which is not valid in the given context D:\excelReport\WindowsApplication1\logic\CreateExcelDoc.cs 28 32 ExcelReport Error 3 Cannot apply indexing with [] to an expression of type 'method group' D:\excelReport\WindowsApplication1\logic\CreateExcelDoc.cs 29 46 ExcelReport Error 4 Cannot apply indexing with [] to an expression of type 'method group' D:\excelReport\WindowsApplication1\logic\CreateExcelDoc.cs 43 13 ExcelReport Error 5 'Excel.Worksheet' does not contain a definition for 'get_Range' D:\excelReport\WindowsApplication1\logic\CreateExcelDoc.cs 44 41 ExcelReport Error 6 'Excel.Range' does not contain a definition for 'Merge' D:\excelReport\WindowsApplication1\logic\CreateExcelDoc.cs 45 29 ExcelReport Error 7 'object' does not contain a definition for 'Color' D:\excelReport\WindowsApplication1\logic\CreateExcelDoc.cs 49 46 ExcelReport Error 8 'object' does not contain a definition for 'Color' D:\excelReport\WindowsApplication1\logic\CreateExcelDoc.cs 52 46 ExcelReport Error 9 'object' does not contain a definition for 'Color' D:\excelReport\WindowsApplication1\logic\CreateExcelDoc.cs 55 46 ExcelReport Error 10 'object' does not contain a definition for 'Color' D:\excelReport\WindowsApplication1\logic\CreateExcelDoc.cs 58 46 ExcelReport Error 11 'object' does not contain a definition for 'Color' D:\excelReport\WindowsApplication1\logic\CreateExcelDoc.cs 61 46 ExcelReport Error 12 'Excel.Range.Borders(object)' is a 'method', which is not valid in the given context D:\excelReport\WindowsApplication1\logic\CreateExcelDoc.cs 69 29 ExcelReport Error 13 'object' does not contain a definition for 'Bold' D:\excelReport\WindowsApplication1\logic\CreateExcelDoc.cs 70 34 ExcelReport Error 14 'object' does not contain a definition for 'Color' D:\excelReport\WindowsApplication1\logic\CreateExcelDoc.cs 74 38 ExcelReport Error 15 'object' does not contain a definition for 'Color' D:\excelReport\WindowsApplication1\logic\CreateExcelDoc.cs 78 38 ExcelReport Error 16 Cannot apply indexing with [] to an expression of type 'method group' D:\excelReport\WindowsApplication1\logic\CreateExcelDoc.cs 84 13 ExcelReport Error 17 'Excel.Worksheet' does not contain a definition for 'get_Range' D:\excelReport\WindowsApplication1\logic\CreateExcelDoc.cs 85 41 ExcelReport Error 18 'Excel.Range.Borders(object)' is a 'method', which is not valid in the given context D:\excelReport\WindowsApplication1\logic\CreateExcelDoc.cs 86 29 ExcelReport
steven balderramaPosted Jan 19, 2010, 2:35 PM
I am looking for using Excel, etc. but cant find it, how do i get that, what do i need to download???
keluarPosted Oct 5, 2007, 4:24 AM
I use VS2005,Excel2003,WinXP-sp2 and create asp.net web application project to open the excel file. I want to open excel application. If I use visual development server type from project propertes,I get it. But if I use IIS Web server type ,I can't get. How can i get to open excel application by web page? I want to use by IIS Web Server option type. Codes: Excel.Application excelApp = new Excel.ApplicationClass(); excelApp.Visible = true; Excel.Workbook newWorkbook = excelApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet); string workbookPath = "C:/text.xls"; Excel.Workbook excelWorkbook = Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath, 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false); Pls Reply , Thanks in advance.
DeepaliPosted Jun 5, 2007, 4:44 AM
Hi, nice aritcle... can u plz tell me how to do this in a web application.
Shanmuka KumarPosted Jan 3, 2006, 9:30 AM
Hi Sushmita, I have read your article.It is fine.But i think we can do this in either of the two ways using: 1. Oledb 2. StringWriter Thanks, Kota