Products
Database Search Solution (New Version) Search Control SEO Pager Highlighter Shortcut Controls Crypt Package Free ASP.NET Controls
Geotargeting Component ASP.NET Media Player Control Flash Video Player Control Services
ASP.NET Telecommute Jobs Free IP Location Lookup Test .Net Regular Expressions CSS/Table/DIV Page Layouts Custom Programming Article Sites Master List |
Generating Excel Report in ASP.NET 2.0IntroductionExcel is such a powerful tool that almost all companies make extensive use of it for analyzing data. Moreover, the internet is not as it was 10 years ago. Almost all pages that are out there on the internet are dynamic ones, that is, interacts with a database in backend to produce results. Sometimes, data that are displayed, if made available on in an Excel file, proper analysis of the data often helps in making more accurate decisions by using Excel features. In this tutorial, we will learn how to interact with Excel files, both reading and writing. You can download Excel Reports Example ASP.NET Project associated with this tutorial. Excel - ASP.NET ScenariosFor the purpose of this tutorial, an Excel sheet had been prepared that holds record of students and their respective marks in subjects. Please note that these records are fictitious and had been input randomly.
Fig 1. Table holding information for students
Fig 2. Table holding student marks in Mathematics subject
Fig 3. Table holding student marks in Geography subject Interacting with ExcelFortunately, there are COM objects that are available to interact with the Excel file. Also, we can make use of the OleDB class to treat the Excel file as a simple table. In this way, we can fire SQL queries to the Excel file so that we can retrieve data from the sheets. An analogy can be made to a database. The Excel Workbook is considered as the Database while the Sheets are considered as tables. Microsoft Excel 11.0 Object Library should be added as Reference to get the features for interacting with Excel.
The Excel file is then added to the DataSource folder in ASAP.NET to be interacted with. To connect to a certain database, a connection string is needed. The same thing applies for Excel. The following is a key added in the Web.Config file for future reference. <add name="ExcelConnection" connectionString="Provider=Microsoft.Jet.Oledb.4.0;Data Source=|DataDirectory|\SchoolMgt.xls;Extended Properties=Excel 8.0"/> Note that our Excel file which contains the necessary data will be SchoolMgt.xls, as written in the connection string.
Reading data from Excel sheetsLet us build an interface for getting data from the Excel file.
It is important to import the following:
Imports
System.Data.OleDb
The following code creates the connection and builds the query based on the selected table.
Protected
Sub btnSearch_Click(ByVal
sender As Object,
ByVal e As
System.EventArgs) Handles btnSearch.Click Please note that the table name in the SELECT query should be in the format [TableName$], which the square brackets and the dollar sign at the end. Playing with SELECT statementsAs you have guessed, the Excel file has turned out to be a normal database. The transparency that the OleDB connection had made is exceptional. Also, note that the first row is taken to contain field names for the columns. Now, let us get the marks of the students in Mathematics, Geography and Total, sorted in Descending order.
Protected
Sub btnGenerateReport_Click(ByVal
sender As Object,
ByVal e As
System.EventArgs) Handles
btnGenerateReport.Click
Generating Excel ReportsTwo ways for generating Excel report will be discussed in this tutorial. The first one is using the "Response" class and the second one by manipulating the Excel objects that "Microsoft Excel 11.0" Objects provide. The following piece of code illustrates how this is done
Protected Sub
btnToExcelByResponse_Click(ByVal sender
As Object,
ByVal e As
System.EventArgs) Handles
btnToExcelByResponse.Click
The second way for generating Excel reports from ASP.NET is to use the Microsoft Excel 11.0 Objects. This method offers more fallibility in terms of manipulating the features that Excel provides and formatting the report. The following code snippet makes use of the various Excel objects that Excel provides to generate the report.
Imports
System.Reflection An Excel workbook is first created. Then the active sheet is accessed by xlWorkSheet = xlWorkBook.ActiveSheet. After that, we get the data into the dataset, ready to be processed. Using the Font property, the title is displayed in Pink bold colour. Note how the Formula that Excel provides is properly used to calculate the SUM of the marks for each student xlWorkSheet.Cells(i, 5).Formula = "=SUM($C{0}:$D{0})".Replace("{0}", i.ToString()).
A chart is also generated using the objects. The x-axis consists of the names and id of each student. The y-axis, the marks for the different students. The chart is always handy for analysis of data.
SummaryIn this tutorial, you learned how to read data from Excel, generate report using the Response objects and to play with the Microsoft Excel 11.0 objects provided. Indeed, it is impossible to cover all features in this tutorial but many important techniques were covered to help you down the line. It is up to you to be creative and adapt the techniques to your scenarios. Going professional with NativeExcel for .NETExcel is powerful tool, but in real world development you rarely have Microsoft Office installed on your web server. There is no Excel on shared hosting and obtaining Office license for dedicated server is possible, but also pretty expensive. Fortunately, there is pure .NET component named Native Excel for .NET that creates or modifies Excel documents on the fly. For just $120 for single license you can use it on unlimited number of web sites and servers (source code available too). It is easier to distribute application since you work with simple assembly written in C#, instead of COM Excel object. Best of all, you don't need to learn anything new because objects and properties are identical to Excel's, so if you understood this tutorial you are already expert for NativeExcel too :). Tutorial toolbar: Tell A Friend | Add to favorites | Feedback | |