5,693,062 members and growing! (21,063 online)
Email Password   helpLost your password?
Web Development » ASP.NET » Howto     Intermediate

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

By mohammed barqawi

How to use JavaScript to export HTML table into Excel work sheet?
C#, Javascript, HTMLWindows, .NET, .NET 1.1, WinXP, ASP.NET, VS.NET2003, IE 6.0, Visual Studio, IE, Dev

Posted: 12 Dec 2004
Updated: 12 Dec 2004
Views: 56,646
Bookmarked: 37 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
11 votes for this Article.
Popularity: 2.99 Rating: 2.87 out of 5
3 votes, 27.3%
1
2 votes, 18.2%
2
4 votes, 36.4%
3
0 votes, 0.0%
4
2 votes, 18.2%
5

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

About the Author

mohammed barqawi


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

Other popular ASP.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 7 of 7 (Total in Forum: 7) (Refresh)FirstPrevNext
GeneralHow does it work?memberalexmoraes3:30 22 Feb '06  
Generallogomemberuswebpro218:22 26 Jan '06  
GeneralNeedhelpmemberMikiyuki Sakura23:41 7 Mar '05  
GeneralROFL!memberAshaman3:54 13 Dec '04  
GeneralRe: ROFL!memberMark Brandon5:33 20 Dec '04  
GeneralRe: ROFL!memberAshaman8:04 20 Dec '04  
GeneralRe: ROFL!memberDanielHac14:05 8 Feb '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 12 Dec 2004
Editor: Sumalatha K.R.
Copyright 2004 by mohammed barqawi
Everything else Copyright © CodeProject, 1999-2008
Web12 | Advertise on the Code Project