Introduction
This article demonstrates how to export SharePoint Filtered list items to Excel. SharePoint gives an option out of the box to export SharePoint views directly, however, if we apply any filter on the SharePoint view and then export the SharePoint list then instead of the filtered view we get all the results in the view in Excel. Hence, to just get those filtered items in Excel we add a content editor web part and link it with our code to export filtered items.
To export only filtered list items in a SharePoint view to Excel
Firstly create a .txt file and paste the following code in the txt file. You can name the file as ExportToExcel.txt.
- <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js"></script>
- <script>
- function fnExcelReport() {
- $('.ms-listviewtable').css({
- 'border-collapse': 'collapse',
- 'border': '1px solid #ddd'
- });
- $('.ms-listviewtable tr td').css({
- 'border': '1px solid #ddd'
- });
- var tab_text = $('.ms-listviewtable')[0].outerHTML;
- debugger;
- $(tab_text).find("tr td:first-child").remove();
- tab_text = tab_text.replace(/<A[^>]*>|<\/A>/g, ""); //remove if u want links in your table
- tab_text = tab_text.replace(/<img[^>]*>/gi, ""); // remove if u want images in your table
- tab_text = tab_text.replace(/<input[^>]*>|<\/input>/gi, ""); // reomves input params
- var ua = window.navigator.userAgent;
- var msie = ua.indexOf("MSIE ");
- if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) // If Internet Explorer
- {
- txtArea1.document.open("txt/html", "replace");
- txtArea1.document.write(tab_text);
- txtArea1.document.close();
- txtArea1.focus();
- sa = txtArea1.document.execCommand("SaveAs", true, "ActionPoints.xls");
- } else //other browser not tested on IE 11
- tab_text = excelExportHtml(tab_text, true)
- sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));
- return (sa);
- }
- function excelExportHtml(table, includeCss) {
- var html = "<html><head>";
- html += "</head><body>" + table + "</body></html>";
- return html;
- }
- </script>
- <body> <input type="button" value="Export To Excel" onclick="fnExcelReport();" /> <iframe id="txtArea1"></iframe> </body>
Now, upload this file to Site Assets library in SharePoint.
Go to the list view in SharePoint where you would like to filter the list view and then click on Edit Page as shown in the screenshot below.
Then, click on Add a Web part >> Media And Content >> Content Editor and then click on Add button as shown in the image below.
After adding the web part click on the Down Arrow and then click on Edit Web Part as shown in the screenshot below.
Scroll to the right of the page and in the Content Link property paste the link of the txt file that was uploaded to the Site Assets Gallery, namely ExporttoExcel.txt and then click OK.
Now, you will see Export to Excel button on the list. Now if you filter the list and then click on “Export to Excel” button Excel will be downloaded with only filtered view records that are visible on the screen.
Summary
In this article, we discussed how we can export SharePoint Filtered Items to Excel. This is of great help as we can directly export filtered view items which are currently not provided by SharePoint out of the box features.
Avian DecostaPosted May 1, 2023, 3:17 PM
CIO Piyush, Above code work correct. But I am facing problem n Modern Page, becoz Content Editor is not supporting modern Page in Allitems.aspx page. Can you please let me know to convert this beutiful code in SPFx, so this can be used with any new button in allitems.aspx command bar pane.
Waqar KhanPosted Feb 14, 2022, 1:15 PM
Its works fine on the main site list. The problem I'm facing is that is not working when I create a subsite and in that subsite I have a list to export its only working there in an edit mode as soon as I save the edit page its no longer exporting
Scott HumphreysPosted Feb 4, 2021, 5:15 PM
Hi Piyush, I am getting a 'about:blank#blocked' url when I press the new 'export to excel' button. Any suggestions?
Dave MeltonPosted Dec 13, 2019, 2:19 PM
Does "Debugger" in line 12 do anything other than activate the debugger?
Tejas KPosted Jul 10, 2019, 1:39 AM
Hi Piyush, this script works as expected on IE. But just doesn't function on Chrome.Can anyone please advise ?
althaf shaikPosted Feb 7, 2019, 4:18 AM
Hi Piyush, the above script is not working when the list view has more data with Pagination. The code exports only the data from the current page in the list view. Can you please suggest how to export entire data from the list view?
Elliot DPosted Sep 17, 2018, 5:26 PM
Hi Piyush - thanks for this, its very helpful. I am just wondering where in the code I can remove the blank white space between the top of the page and the button? Thanks :)