Tuesday 26 January 2016

Export girdview to Excel using JavaScript object model in SharePoint 2010

In this post we will discuss how to export gridview  to excel in SharePoint 2010 using JavaScript client object model.

function fnExcelReport()
         {
 
 
             var strTableID = "{0120ED5F-14B2-45FB-A817-2C5F8B6AAF23}-{G_851CE056_F02A_4524_AD2D_CC9185FF96F3}";  // It's the Table ID of Table in Webpart
var detailsTable = document.getElementById(strTableID);
var columns = detailsTable.getElementsByTagName("th");
var oExcel = new ActiveXObject("Excel.Application");
var oBook = oExcel.Workbooks.Add;
var oSheet = oBook.Worksheets(1);
for(i=0;i                                oSheet.cells(1,i+1).value= columns[i].innerText; //XlSheetHeader[i];
                                oSheet.cells(1,i+1).font.color="6";
                                oSheet.cells(1,i+1).font.bold="true";
                      oSheet.cells(1,i+1).interior.colorindex="15";
                      oSheet.cells(1,i+1).columnwidth =20;
                        }
for (var y=0;y// detailsTable is the table where the content to be exported is
{
    for (var x=0;x    {
        oSheet.Cells(y+1,x+1) = detailsTable.rows(y).cells(x).innerText;
    }
}

oSheet.columns.autofit;
oExcel.Visible = true;
oExcel.UserControl = true;              
         }