Forums

Forums / Bugs & Issues / Exported .XLS file

Exported .XLS file

Thread is closed for posting
3 posts, 1 answered
  1. geoff.white
    geoff.white avatar
    31 posts
    Registered:
    27 Jan 2015
    18 Aug 2016
    Link to this post
    Can the format of the data in the exported .XLS file be changed to a CSV format?

    Currently the data in the file is HTML with a table inside a DIV.

    Some of our clients have issues with opening this on their local machine, whether it isn't setup to open it correctly or not I'm unsure.

    Is this possible?
  2. Missing user
    Missing user avatar
    Answered
    22 Aug 2016 in reply to geoff.white
    Link to this post
    Hi Geoff,

    Export to CSV format from grid is currently not supported.

    Thanks
    Syed
  3. Josh
    Josh avatar
    31 posts
    Registered:
    01 Jun 2018
    25 Oct 2019 in reply to Missing user
    Link to this post
    If anybody needs this functionality, we have created a custom script that can export a grid to a CSV file. It will add a button to the grid toolbar, and simply requires the identifier of the grid model. Simply replace the "TpcGridModel" at the very bottom of the following code in the enableCsvExport invocation with the name of your own grid.

    var enableCsvExport = function (gridId) {    
        var grid = tpc.find(gridId).get_grid();
        var exportCsv = function (fileName) {
            var originalPageSize = grid.dataSource.pageSize();
            fileName = fileName || 'download.csv';
    
            // Increase page size to cover all the data and get a reference to that data
            var total = grid.dataSource.total();
            if(total > 5000) total = 5000;
            grid.dataSource.pageSize(total);
            grid.dataSource.fetch(function() {
                var csv = '';
                var data = grid.dataSource.data();
    
                //add the header row
                for (var i = 0; i < grid.columns.length; i++) {
                    var field = grid.columns[i].field;
                    var title = grid.columns[i].title || field;
        
                    //NO DATA
                    if (!field) continue;
        
                    title = title.replace(/"/g, '""');
                    csv += '"' + title + '"';
                    if (i < grid.columns.length - 1) {
                        csv += ',';
                    }
                }
                csv += '\n';
                //add each row of data
                for (var row = 0; row < data.length; row++) {
                    for (var i = 0; i < grid.columns.length; i++) {
                        var fieldName = grid.columns[i].field;
                        var template = grid.columns[i].template;
                        var exportFormat = grid.columns[i].exportFormat;
        
                        //VALIDATE COLUMN
                        if (!fieldName) continue;
                        var value = '';
                        if (fieldName.indexOf('.') >= 0) {
                            var properties = fieldName.split('.');
                            var value = data[row] || '';
                            for (var j = 0; j < properties.length; j++) {
                                var prop = properties[j];
                                value = value[prop] || '';
                            }
                        }
                        else {    
                            value = data[row][fieldName] || '';
                        }
                        if (value && template && exportFormat !== false) {
                            value = _.isFunction(template)
                                ? template(data[row])
                                : kendo.template(template)(data[row]);
                        }
        
                        value = value.toString().replace(/"/g, '""');
                        csv += '"' + value + '"';
                        if (i < grid.columns.length - 1) {
                            csv += ',';
                        }
                    }
                    csv += '\n';
                }
        
                var blob = new Blob([csv]);
                if (window.navigator.msSaveOrOpenBlob)  // IE hack; see http://msdn.microsoft.com/en-us/library/ie/hh779016.aspx
                    window.navigator.msSaveBlob(blob, fileName);
                else
                {
                    var a = window.document.createElement("a");
                    a.href = window.URL.createObjectURL(blob, {type: "text/plain"});
                    a.download = fileName;
                    document.body.appendChild(a);
                    a.click();  // IE: "Access is denied"; see: https://connect.microsoft.com/IE/feedback/details/797361/ie-10-treats-blob-url-as-cross-origin-and-denies-access
                    document.body.removeChild(a);
                }
            }).always(function(){
                grid.dataSource.pageSize(originalPageSize);
            })
        }
        var exportToCsvButton = {
            name: "tpcexportcsv",
            text: "Export to CSV"
        }
        var options = grid.getOptions();
        options.toolbar.push(exportToCsvButton);
        grid.setOptions(options);
        
        $(".k-grid-tpcexportcsv", grid.element).click(function(){
            exportCsv("export.csv");
        });
    };
    
    $(document).on("tpc:ready", function(){
        enableCsvExport("TpcGridModel")
    });
    
    Last modified on 25 Oct 2019 14:10 by Josh
3 posts, 1 answered