Servicenow

HARSHA GOWDA R
Tera Contributor
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() {
        var myBlob = new Blob([s2ab(c.data.excelContent)], {
            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);
   
 };

};

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.
HARSHAGOWDAR_0-1719940830845.png

 




2 REPLIES 2

HrishabhKumar
Kilo Sage

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.

Hi @HrishabhKumar 
this is my server side code

(function() {
    /* populate the 'data' object */
    /* e.g., data.table = $sp.getValue('table'); */
    data.restrictExportSummary = 'role_composition';
    data.excelContent = '';
    data.fileName = '';
    data.certType = '';
    data.showExport = true;

    if (input) {
        data.excelContent = input.excelTable;
        data.fileName = input.fileName;
        data.certType = input.certType;

        if (data.restrictExportSummary.indexOf(input.certType) > -1)
            data.showExport = false;
    }

})();
I have changed as per you mentioned but its not working and i have added dependencies also,can u help me with that?