Click here to Skip to main content
13,054,615 members (62,716 online)
Click here to Skip to main content
Add your own
alternative version

Stats

82.9K views
47 bookmarked
Posted 12 Dec 2004

Link ASP.NET Data Grid to Microsoft Excel in the client side

, 12 Dec 2004
Rate this:
Please Sign up or sign in to vote.
How to use JavaScript to export HTML table into Excel work sheet?

Sample Image - 6.jpg

Introduction

I was playing with internet explorer context menu, suddenly I noted that I have menu item called "export to Microsoft Excel".

:)

When I clicked on this command it is transferred the contents of the selected table to Microsoft excel.

How did I steal the code?

To guess what was happing on when I clicked on the context menu item, I used the IE feature, break at next statement (from menu item choose View-> Script Debugger->Break at Next Statement ), and made a small table and as I clicked on the menu and I got the VBScript file in Visual Studio .NET.

How did I link the datagrid with the script?

I have used something called HTC "HTML Components" which is a good thing to learn if you are developing an ASP.NET Server Control. My HTC script is divided in to two parts :

  • First one is how to display a small icon near the datagrid, and that was by
    1. creating HTML image object.
      ImgaElement=this.document.createElement("img")
    2. putting the image near the datagrid table.
      ImgaElement.style.top=this.element.offsetTop-3 
      ImgaElement.style.left=this.element.offsetLeft+offsetWidth-3
  • And the second part is how to plug it to the Microsoft Excel. In this task I did not do anything new but converting the VBS file to JavaScript one and add additional lines for checking the security settings.
    try 
    {
           xlApp= new ActiveXObject("Excel.Application"); 
    } 
    catch(e) 
    {
        alert("Please check the Internet security setting");
            return -1;
    }

How to use it?

  • An important note before using the component is, to enable the "Initialize and script ActiveX controls not marked as safe"

    :)

    in the security tab in the internet security settings.

  • Copy the images and the HTC folders to your web project.
  • Add a the DLL file (DataGridProject.dll) to your references and to the Toolbox controls.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

mohammed barqawi
Web Developer
Jordan Jordan
web developer
email : mohammed.barqawi AT gmail.com

You may also be interested in...

Comments and Discussions

 
QuestionHow does it work? Pin
alexmoraes22-Feb-06 2:30
memberalexmoraes22-Feb-06 2:30 
Generallogo Pin
uswebpro226-Jan-06 17:22
memberuswebpro226-Jan-06 17:22 
GeneralNeedhelp Pin
Mikiyuki Sakura7-Mar-05 22:41
memberMikiyuki Sakura7-Mar-05 22:41 
GeneralROFL! Pin
Ashaman13-Dec-04 2:54
memberAshaman13-Dec-04 2:54 
GeneralRe: ROFL! Pin
Mark Brandon20-Dec-04 4:33
memberMark Brandon20-Dec-04 4:33 
GeneralRe: ROFL! Pin
Ashaman20-Dec-04 7:04
memberAshaman20-Dec-04 7:04 
GeneralRe: ROFL! Pin
DanielHac8-Feb-05 13:05
memberDanielHac8-Feb-05 13:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170713.1 | Last Updated 12 Dec 2004
Article Copyright 2004 by mohammed barqawi
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid