How to create ui page to export data in excel sheet
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi All,
I am looking for some help to know how to create ui page which will export data in excel sheet.
what I am basically looking. I am working on one catalog where user select opened by filed and configuration item filed on the basis of that we provide download link in ui page where all the incidents get download loaded in excel sheet by gliding in these filters this excel sheet is unique sheet where headers in row 3 and data start updating from row 4 how I can achieve this.
All suggestion are highly appreciated \
@Ankur Bawiskar your suggestion how to do this
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @keshav77
Below Code is tested and Its working .. but its my use case you can use it for your own
1. Create a UI Page
-
Navigate: System UI → UI Pages → New.
-
Give it a name (e.g., export_incidents_excel).
-
This UI page will run a Script Include (server-side logic) to generate the Excel file.
2. Script Include to Generate Excel
Create a Script Include that builds an Excel file. ServiceNow doesn’t generate native .xlsx, but you can generate an Excel-readable XML or a CSV (which Excel opens directly).
Here’s an Excel XML sample that matches your formatting requirement:
var ExportIncidents = Class.create();
ExportIncidents.prototype = {
initialize: function() {},
generateExcel: function(openedBy, ci) {
var gr = new GlideRecord('incident');
if (openedBy)
gr.addQuery('opened_by', openedBy);
if (ci)
gr.addQuery('cmdb_ci', ci);
gr.query();
// Start Excel XML content
var xml = '<?xml version="1.0"?>';
xml += '<?mso-application progid="Excel.Sheet"?>';
xml += '<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" ';
xml += 'xmlns:o="urn:schemas-microsoft-com:office:office" ';
xml += 'xmlns:x="urn:schemas-microsoft-com:office:excel" ';
xml += 'xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">';
xml += '<Worksheet ss:Name="Incidents"><Table>';
// Empty first 2 rows (so headers appear in row 3)
xml += '<Row></Row>';
xml += '<Row></Row>';
// Row 3 = Headers
xml += '<Row>';
xml += '<Cell><Data ss:Type="String">Number</Data></Cell>';
xml += '<Cell><Data ss:Type="String">Short Description</Data></Cell>';
xml += '<Cell><Data ss:Type="String">State</Data></Cell>';
xml += '<Cell><Data ss:Type="String">Opened By</Data></Cell>';
xml += '<Cell><Data ss:Type="String">Configuration Item</Data></Cell>';
xml += '</Row>';
// Data rows (start from row 4)
while (gr.next()) {
xml += '<Row>';
xml += '<Cell><Data ss:Type="String">' + gr.getValue('number') + '</Data></Cell>';
xml += '<Cell><Data ss:Type="String">' + gr.getValue('short_description') + '</Data></Cell>';
xml += '<Cell><Data ss:Type="String">' + gr.getDisplayValue('state') + '</Data></Cell>';
xml += '<Cell><Data ss:Type="String">' + gr.getDisplayValue('opened_by') + '</Data></Cell>';
xml += '<Cell><Data ss:Type="String">' + gr.getDisplayValue('cmdb_ci') + '</Data></Cell>';
xml += '</Row>';
}
xml += '</Table></Worksheet></Workbook>';
return xml;
},
type: 'ExportIncidents'
};
3. UI Page Processing Script
In your UI Page, use a server-side processing script:
4.Add a Catalog Link
-
In your catalog item or widget, generate a link like:
/export_incidents_excel.do?opened_by=<sys_id>&ci=<sys_id> -
Clicking the link downloads the Excel file with your format.
5. Result
-
Row 1–2 → blank.
-
Row 3 → headers.
-
Row 4 onward → data from the query.
-
File downloads as .xls and opens in Excel.
If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!
Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI
YouTube: https://www.youtube.com/@learnservicenowwithravi
LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/