Monday, 19 November 2012

Export list or library into Excel using JavaScript

Using JavaScript, List or Libaray Items export to Excel

Add one Content Editor in ur pages:

<script language="javascript" src="/sites/web/SiteAssets/JS/Export.js"></script><input class="ms-rteThemeBackColor-5-4" onclick="exportToExcel();" type="button" value="Export"/>

function exportToExcel()
{
var strTableID = "onetidDoclibViewTbl0";
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<columns.length;i++){
       oSheet.cells(1,i+1).value= columns.innerText; //XlSheetHeader;
       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.rows.length;y++)
{
    for (var x=0;x<detailsTable.rows(y).cells.length;x++)
    {
        oSheet.Cells(y+1,x+1) = detailsTable.rows(y).cells(x).innerText;
    }
}
oSheet.columns.autofit;
oExcel.Visible = true;
oExcel.UserControl = true;
}

1 comment:

  1. Hi,
    I am facing issues in exporting data from a list to excel in SharePoint 2010 so was trying to implement your approach. But had a couple of questions:
    1)Where can I download this export.js file?
    2)What is onetidDoclibViewTbl0. How can I get this?

    ReplyDelete