Servicenow
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-02-2024 10:20 AM
Here,Not able to open the file with .xlsx format.Its giving error and i need in this format only.Can anyone help me with this query.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-02-2024 10:39 AM
Hi @HARSHA GOWDA R ,
It looks like you're generating and downloading an Excel file in a ServiceNow widget, but the content of the file might not be correctly formatted as an Excel file. The code you provided seems to be trying to convert a string (c.data.excelContent) into a binary array buffer and then create a Blob object from it. However, if c.data.excelContent isn't properly formatted as an Excel file, the downloaded file will be corrupt.
Here's a revised approach using the SheetJS library (xlsx) to create the Excel content properly. You can include the xlsx library in your widget and use it to generate the Excel file.
First, ensure you have the xlsx library available. You can include it in your widget by adding the script source in your HTML template:
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.17.0/xlsx.full.min.js"></script>
Next, update your controller code to use SheetJS to generate the Excel file:
api.controller = function() {
/* widget controller */
var c = this;
function s2ab(s) {
var buf = new ArrayBuffer(s.length);
var view = new Uint8Array(buf);
for (var i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
return buf;
}
c.doExcel1 = function() {
// Prepare data for the Excel file
var ws_data = [
["Column 1", "Column 2", "Column 3"], // headers
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
var ws = XLSX.utils.aoa_to_sheet(ws_data);
var wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, "Sheet1");
var wbout = XLSX.write(wb, {bookType: 'xlsx', type: 'binary'});
var myBlob = new Blob([s2ab(wbout)], {
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
});
var url = window.URL.createObjectURL(myBlob);
var a = document.createElement("a");
document.body.appendChild(a);
a.href = url;
a.download = c.data.fileName + ".xlsx";
a.click();
window.URL.revokeObjectURL(url);
};
};
Thanks,
Hope this helps.
If my response turns useful please mark it helpful and accept it as solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-09-2024 03:41 AM - edited 07-09-2024 03:42 AM
Hi @HrishabhKumar
this is my server side code