Download as Zip which contains the Excel - excelHtml5

Download as Zip which contains the Excel - excelHtml5

samirgjsamirgj Posts: 7Questions: 2Answers: 0

I use excelHtml5 to download the table but when is too big i need to download as a zip, how could do this?

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 61,723Questions: 1Answers: 10,108 Site admin

    It is already a zip :). That's how the xlsx file format works - its a bunch of xml files zipped together. Rename an xlsx file from .xlsx to .zip and unzip it to see the source.

    Allan

  • samirgjsamirgj Posts: 7Questions: 2Answers: 0

    Hi Allan:

    Thanks for the answer, I already changed the extension, but what i need is that the object, which is exported, will a zip and inside of this zip have the xlsx object.

    How could i do this?

    Thanks again for the help.

  • allanallan Posts: 61,723Questions: 1Answers: 10,108 Site admin
    Answer ✓

    I don't see why you would do that? The xlsx is a zip. You would just be zipping another zip file.

    If you did want to do that you would need to modify this section of the code to wrap the current zip in another zip. But as I say, I don't see any benefit in that.

    Allan

  • samirgjsamirgj Posts: 7Questions: 2Answers: 0

    Hi allan:

    I understand what you say, but the file which will be export, sometimes be very large and when i try to zip a xlsx, it will reduce.

    For that reason i will try to do this:

    //Here init Table
    function InicializarTabla() {
    
        var table = jQuery('#tabla').DataTable({
    
            scrollY: "300px",
            scrollX: true,
            scrollCollapse: true,
            paging: false,
            fixedColumns: {
                leftColumns: 2,
                rightColumns: 0
            },
            "ordering": false,
            "info": false,
            "searching": true,
            dom: 'frtip',
            buttons: [
                'excelHtml5',
                {
                // Here i will create a extra button with the zip
                }
            ],
            initComplete: function () {
    
                var api = this.api();
    
                api.columns().every(function () {
    
                    var that = this;
                    jQuery('input', this.footer()).on('change', function () {
                        if (that.search() !== this.value) {
                            that
                              .search(this.value)
                              .draw();
                        }
                    });
                });
    
                jQuery('tfoot').each(function () {
                    this.style.setProperty('display', 'table-header-group', 'important');
                });
            }
        });
    }
    
    //Call the action from here
    
    function ExportarExcel() {
        var table = jQuery('#tabla').DataTable();
    
        if (table.rows().count() > NumMaxRows) {
            table.button('1').trigger();
        }
        else {
            table.button('0').trigger();
        }
    }
    

    Finally, when i put the filter of each columns at header using:

    // Here i move
    jQuery('#tabla tfoot tr').appendTo('#tabla thead');
    
    // here change where to search
    jQuery('input', this.header()).on('change', function () {
    ...
    }
    

    Almost every columns work fine but don't do it for the fixed columns.

    Thanks a lot again, i will try the first and tell you if everything work fine.

  • samirgjsamirgj Posts: 7Questions: 2Answers: 0
    edited May 2017

    I'm trying to do this

                    var objConfigZip = config;
                    objConfigZip.extension = '.zip';
    
                    zip
                        .generateAsync(zipConfig)
                        .then(function (blob) {
                            //config.extension = '.xlsx';
                            _saveAs(blob, _filename(config));
                            that.processing(false);
                        })
                        .generateAsync({ type: "blob" })
                        .then(function (blob) {
                            //config.extension = '.zip';
                            _saveAs(blob, _filename(objConfigZip));
                            that.processing(false);
                        });
    

    But, still downloading a xlsx and not a zip as i want.

  • allanallan Posts: 61,723Questions: 1Answers: 10,108 Site admin
    Answer ✓

    _saveAs is downloading the file in your first then. You need to have JSZip return a blob which you can then zip again.

    Allan

  • samirgjsamirgj Posts: 7Questions: 2Answers: 0

    Thanks allan, it works fine.

    Finally, could you help me with the filters at headers that i mentioned in this question or maybe it is better to do a new question?

    Thanks again.

  • allanallan Posts: 61,723Questions: 1Answers: 10,108 Site admin

    Try using fixedColumns().update() immediately after you add your event listeners in the header.

    Allan

This discussion has been closed.