Kwizcom Forms

Export list items to Excel using JavaScript object model (jsom) in SharePoint 2013

Export list items to Excel using JavaScript object model in SharePoint 2013

This jsom SharePoint tutorial, we will discuss how to export SharePoint list items to excel using the javascript object model (jsom) in SharePoint Online or SharePoint 2013/2016.

Export list items to Excel using JavaScript object model in SharePoint 2013/2016/Online

I have a list name as MyTestList, we are retrieving and displaying the list items in a table. And in another button, we are exporting list items to an excel in SharePoint.

SharePoint deveopment training course

The limitation of this method is export to excel will work in IE because it requires ActiveX.

So to do this Put the below code in a single Script editor web part in a web part page in SharePoint Online or SharePoint 2013/2016.

Below is the code to display items into the table in SharePoint:

<script language="javascript" type="text/javascript">
function retrieveListItemsInclude() {
var table = document.getElementById('cntryTable');
while (table.rows.length > 1) {
table.deleteRow(1);
}
var clientContext = new SP.ClientContext.get_current();
var oList = clientContext.get_web().get_lists().getByTitle('MyTestList');
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<View><RowLimit>100</RowLimit></View>');
this.collListItem = oList.getItems(camlQuery);
clientContext.load(collListItem, 'Include(Title)');
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed)); }
function onQuerySucceeded(sender, args) {
var listItemInfo = ";
var listItemEnumerator = collListItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
var table = document.getElementById('cntryTable');
var row = table.insertRow(1);
var cell1 = row.insertCell(0);
cell1.innerHTML = oListItem.get_item('Title');
} }
function onQueryFailed(sender, args) { alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace()); } </script>
<table id="cntryTable" border="1″>
<tbody>
<tr>
<th>
Title
</th>
</tr>
</tbody>
</table>

<input type='button' value='Display List Items' onclick="retrieveListItemsInclude();"/>

Below is the code to export the data to excel in SharePoint.

<script language="javascript" type="text/javascript">
function generateexcel() {
var x = document.getElementById('cntryTable').rows;
var xls = new ActiveXObject(“Excel.Application");
xls.visible = true
xls.Workbooks.Add
for (i = 0; i < x.length; i++) {
var y = x[i].cells;
for (j = 0; j < y.length; j++) {
xls.cells(i + 1, j + 1).value = y[j].innerText;
}
}
}
</script>
<input type='button' value='Export To Excel' onclick="generateexcel();"/>

It will display like this in the page:

sharepoint export to excel javascript
sharepoint export to excel javascript

You may like following jsom SharePoint tutorials:

Hope this SharePoint tutorial explains, how to Export list items to Excel using JavaScript object model in SharePoint 2013/2016 or SharePoint Online.

Check out Best Alternative to InfoPath -> Try Now

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →