lukassliacky
lukassliacky

Reputation: 408

Datatables.net: Set column width when exporting to XLSX

i have datatables table and i want export content of this table. Because some headers are so long, i must set specific text of headers. All works fine, but columns with customized text are still so long.

customize: function( xlsx ) {
                var sheet = xlsx.xl.worksheets['sheet1.xml'];

                // Exported excel custom properties
                $('row:first c', sheet).attr( 's', '7' );

                $('c[r=B1] t', sheet).text( 'Date' ).;
                $('c[r=C1] t', sheet).text( 'Type' );
                $('c[r=E1] t', sheet).text( 'Subject' );
            }
        }]

Pls, is possibe set specific width for columns with "new" - shorter texts? Thanks for any answers.

Upvotes: 1

Views: 2806

Answers (2)

pxeba
pxeba

Reputation: 1776

Use the customize tag inside the excel button to configure the width options

{
  title: "",
  exportOptions: { ... }
  customize: function (xlsx: any) {
      var sheet = xlsx.xl.worksheets["sheet1.xml"];
      var col = $("col", sheet);
      

      var col = $('col', sheet);
      col.each(function () { //update all columns with width 30
          $(this).attr('width', 30); 
      });
      $(col[0]).attr("width", 7); // update specific columns
      $(col[4]).attr("width", 7); // update specific columns
   }
}

Upvotes: 1

Vidya Tikone
Vidya Tikone

Reputation: 11

    "customize": function (xlsx) {
                        var sheet = xlsx.xl.worksheets['sheet1.xml'];
                        $('row:first c', sheet).attr( 's', '2' );
                        $('row:first c is t', sheet).each(function () {
                            if (this.innerHTML == 'creationdate') { this.innerHTML = 'Date' }
                            if (this.innerHTML == 'ty') { this.innerHTML = 'Type' }
                            if (this.innerHTML == 'sub') { this.innerHTML = 'Subject' }

                        });
                    }

Upvotes: 1

Related Questions