Exporting Dataview WebPart data into Excel using Javascript

Use following JS to export Dataview Webpart data in a excel sheet.
// JS SCRIPT
<Script Language="Javascript"> 
function isIE() 
// Function to Determine IE or Not 
{
 return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent); 

function exportToExcel() 

var isIEBrowser = isIE(); 
if(isIEBrowser== false) 

 // This script works for IE. If browser is not IE throw alert 
 alert('Please use Internet Explorer for Excel Export Functionality.'); 
 return false; 

else 

 var strTableID = "dataTable"; // It's the Table ID of Table in Webpart 
 var detailsTable = document.getElementById(strTableID); 
 var objExcel = new ActiveXObject("Excel.Application"); 
 var objWorkBook = objExcel.Workbooks.Add; 
 var objWorkSheet = objWorkBook.Worksheets(1); 
 for (var intRowIndex=0;intRowIndex<detailsTable.rows.length;intRowIndex++) 

 for (var intColumnIndex=0;intColumnIndex<detailsTable.rows(intRowIndex).cells.length;intColumnIndex++) 

 if(intColumnIndex != 3) 
 objWorkSheet.Cells(intRowIndex+1,intColumnIndex+1) = detailsTable.rows(intRowIndex).cells(intColumnIndex).innerText; 


 objExcel.Visible = true; 
 objExcel.UserControl = true; 


</Script> 
 // CALL A JS FUNCTION ON A BUTTON CLICK
<button onclick="exportToExcel();">Export to Excel</button>

That's it.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s