xlsx type attachment error

Rj27
Mega Guru

Hello All,
I am trying to create an attachment of type xslx.
The attachment is getting created but when i try to upload in one drive and access it from there getting this error : "This workbook couldn't be opened because the file format may not be matching with the file extension."

It is working for csv/xls type but not with xlsx. Is this this a limitation for excel type ? Any suggestions on this ?
Sample code:

 

 

var data = 'test';
var fileName = "testing1";
fileName += '.xlsx';
grR = new GlideRecord("sys_user");
grR.addQuery("sys_id", '62826bf03710200044e0bfc8bcbe5df1');
grR.query();
if (grR.next()) {
     var grAttachment = new GlideSysAttachment();
     grAttachment.write(grR, fileName, 'application/xlsx', data);
}

 

Also tried replacing with this but getting same error message:

if (grR.next()) {
    var base64Content = GlideStringUtil.base64Encode(data);
    var attachment = new GlideSysAttachment();
    attachment.write(grR, fileName, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', base64Content);
}

 

6 REPLIES 6

Anand Kumar P
Giga Patron
Giga Patron

Hi @Rj27 ,

For xlsx content type will be different so try to replace application/xlsx with below one

'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'

 Mark it as helpful and solution proposed if it serves your purpose.
Thanks,
Anand

Hi Anand,

Tried that content type as well, but same error message

Rajdeep Ganguly
Mega Guru


The issue you're facing is likely due to the fact that you're trying to create an .xlsx file with plain text data. The .xlsx file format is a complex, zipped XML format that can't be created just by changing the file extension.

Here are some steps you can take to resolve this issue:

- Use a third-party library to generate .xlsx files. There are several JavaScript libraries available that can generate .xlsx files, such as ExcelJS or XLSX.js. You can use these libraries to generate your .xlsx file, then upload that file to ServiceNow.

- Use the ServiceNow OOB Excel export functionality. ServiceNow has built-in functionality for exporting data to Excel. You can use this functionality to export your data to an .xlsx file, then upload that file to OneDrive.

- Convert your data to CSV format. If you don't need the advanced features of .xlsx files, you can convert your data to CSV format, which can be opened in Excel. This is a simpler format that can be created just by changing the file extension.

Here's a sample code using the ExcelJS library:

javascript
var ExcelJS = require('exceljs');
var workbook = new ExcelJS.Workbook();
var worksheet = workbook.addWorksheet('My Sheet');

worksheet.columns = [
{ header: 'Id', key: 'id', width: 10 },
{ header: 'Name', key: 'name', width: 32 },
{ header: 'D.O.B.', key: 'dob', width: 15, }
];

worksheet.addRow({id: 1, name: 'John Doe', dob: new Date(1970,1,1)});
worksheet.addRow({id: 2, name: 'Jane Doe', dob: new Date(1965,1,7)});

workbook.xlsx.writeFile('My Excel File.xlsx')
.then(function() {
// done
});


Please note that you would need to upload this file to ServiceNow after it's created.


nowKB.com

For asking ServiceNow-related questions try this :
For a better and more optimistic result, please visit this website. It uses a Chat Generative Pre-Trained Transformer ( GPT ) technology for solving ServiceNow-related issues.
Link - https://nowgpt.ai/

For the ServiceNow Certified System Administrator exams try this :
https://www.udemy.com/course/servicenow-csa-admin-certification-exam-2023/?couponCode=NOW-DEVELOPER

Aniket Chavan
Tera Sage
Tera Sage

Hello @Rj27 ,

Please give a try to the script below and let me know how it works for you.

var data = 'test';
var fileName = "testing1.xlsx";
var mimeType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';

var grR = new GlideRecord("sys_user");
grR.addQuery("sys_id", '62826bf03710200044e0bfc8bcbe5df1');
grR.query();

if (grR.next()) {
    var base64Content = GlideStringUtil.base64Encode(data);
    var attachment = new GlideSysAttachment();
    attachment.write(grR, fileName, mimeType, base64Content);
}

 

Let me know your views on this and Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks,

Aniket