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, "");
- tab_text = tab_text.replace(/<img[^>]*>/gi, "");
- tab_text = tab_text.replace(/<input[^>]*>|<\/input>/gi, "");
- var ua = window.navigator.userAgent;
- var msie = ua.indexOf("MSIE ");
- if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./))
- {
- 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
- 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.