clarketm / TableExport

The simple, easy-to-implement library to export HTML tables to xlsx, xls, csv, and txt files.
https://tableexport.travismclarke.com/
Apache License 2.0
885 stars 291 forks source link

set excel column with #162

Open parsibox opened 5 years ago

parsibox commented 5 years ago

hi how can set width for column in xlsx ?

amthe22 commented 5 years ago
        var exportTable = $("table")
        var export_tables = new TableExport(exportTable, {
            headers: true,
            formats: ['xlsx'],
            bootstrap: true,
            exportButtons: false,
            htmlContent: true,

        });
        var tables_data = export_tables.getExportData()
        var export_data = []
        var xlsx_info = {}
        for (table_id in tables_data) {
            xlsx_info = tables_data[table_id]["xlsx"]
            if (xlsx_info.filename != "tblInvoiceDetails") {
                export_data.push(tables_data[table_id]["xlsx"].data);
            }
        }
        var fileExtension = xlsx_info.fileExtension
        var mimeType = xlsx_info.mimeType
        export_tables.exportmultisheet(export_data, mimeType,"name for the Excel file", ["sheetname"], fileExtension, {}, {"sheetname":[10,10,10]})//here the 10 is the width of the column, u can add the more .

//add this function to your exporttable.js

exportmultisheet: function (data, mime, filename, sheetnames, extension, merges={}, cols_width={}) { var sheet_data = null; var key = extension.substring(1); if (_isEnhanced(key)){ var wb = new this.Workbook(); for (var i=0; i<data.length; i++){ wb.SheetNames.push(sheetnames[i]); var sheet_data = this.createSheet(data[i], merges[sheetnames[i]] || [], cols_width[sheetnames[i]] || []); wb.Sheets[sheetnames[i]] = sheet_data; } var bookType = this.getBookType(key); var wopts = { bookType: bookType, bookSST: false, type: 'binary' }, wbout = XLSX.write(wb, wopts);

                sheet_data = this.string2ArrayBuffer(wbout);
            }
            if (sheet_data){
                if (_isMobile) {
                    var dataURI = 'data:' + mime + ';' + this.charset + ',' + sheet_data;
                    this.downloadDataURI(dataURI, filename, extension);
                } else {
                    saveAs(new Blob([sheet_data],
                        {type: mime + ';' + this.charset}),
                        filename + extension, true);
                }
            }
        },