Client Side Script to
Export to Excel 2003
Export to Excel is one of the most important features that business users request. This requirement is quite frequently observed in reporting world. The requirement could be that we click on any button/link and after processing the request on server open up an Excel with desired data on the client machine. On the other hand, the requirement could be that we first display the data in the desired format to the users on the client tool like IE for the web application, and on clicking any link/button, we export that data to Excel. And this is where we can use Script
Let us first have a look at the script serves our purpose and then we will get into its details. So here is the VBScript for the same.
Dim sHTML, oExcel, oBook
sHTML = document.all.item(objToExport).outerhtml
Set oExcel = CreateObject("Excel.Application")
Set oBook = oExcel.Workbooks.Add
oBook.HTMLProject.HTMLProjectItems("Sheet1").Text = sHTML
oExcel.Visible = true
oExcel.UserControl = true
The script above is pretty straightforward and surely most of us must have used it in one or the other application. In the script, we just need to provide the HTML Object which has been rendered on the screen to the function and the same will be exported to the Excel using the Office which is already present on client machines. Simply put, we first create an ActiveX object of Excel.Application then add a Workbook to it. Then to the first WorkSheet, i.e. Sheet1, of the workbook, we place the HTML contents which the Excel application can recognize using HTMLProject and HTML ProjectItems. Excel thus parses that HTML and renders on "Sheet1" . An important thing to note here would be that it is not actually the Excel Binary data file which users will be seeing in the Excel, it is the HTML data which is parsed and rendered by Excel Application.