diff --git a/dist/ExcelPlugin/utils/DataUtil.js b/dist/ExcelPlugin/utils/DataUtil.js index 0c9211fd..5fcbd066 100644 --- a/dist/ExcelPlugin/utils/DataUtil.js +++ b/dist/ExcelPlugin/utils/DataUtil.js @@ -64,16 +64,26 @@ var excelSheetFromDataSet = function excelSheetFromDataSet(dataSet) { rowCount += ySteps; + var columnsWidth = []; if (columns.length >= 0) { columns.forEach(function (col, index) { var cellRef = _xlsx2.default.utils.encode_cell({ c: xSteps + index, r: rowCount }); fixRange(range, 0, 0, rowCount, xSteps, ySteps); - getHeaderCell(col, cellRef, ws); + var colTitle = col; + if ((typeof col === 'undefined' ? 'undefined' : _typeof(col)) === 'object') { + colTitle = col.title; + columnsWidth.push(col.width || { wpx: 80 }); /* wch (chars), wpx (pixels) - e.g. [{wch:6},{wpx:50}] */ + } + getHeaderCell(colTitle, cellRef, ws); }); rowCount += 1; } + if (columnsWidth.length > 0) { + ws['!cols'] = columnsWidth; + } + for (var R = 0; R != data.length; ++R, rowCount++) { for (var C = 0; C != data[R].length; ++C) { var cellRef = _xlsx2.default.utils.encode_cell({ c: C + xSteps, r: rowCount }); diff --git a/examples/styled_excel_sheet.md b/examples/styled_excel_sheet.md index 37bce9dc..20d3cb97 100644 --- a/examples/styled_excel_sheet.md +++ b/examples/styled_excel_sheet.md @@ -8,7 +8,11 @@ const ExcelSheet = ReactExport.ExcelFile.ExcelSheet; const multiDataSet = [ { - columns: ["Headings", "Text Style", "Colors"], + columns: [ + {title: "Headings", width: {wpx: 80}},//pixels width + {title: "Text Style", width: {wch: 40}},//char width + {title: "Colors", width: {wpx: 90}}, + ], data: [ [ {value: "H1", style: {font: {sz: "24", bold: true}}}, diff --git a/src/ExcelPlugin/utils/DataUtil.js b/src/ExcelPlugin/utils/DataUtil.js index 90e3020b..b6badeef 100644 --- a/src/ExcelPlugin/utils/DataUtil.js +++ b/src/ExcelPlugin/utils/DataUtil.js @@ -51,16 +51,26 @@ const excelSheetFromDataSet = (dataSet) => { rowCount += ySteps; + var columnsWidth = [] if (columns.length >= 0) { columns.forEach((col, index) => { var cellRef = XLSX.utils.encode_cell({c: xSteps + index, r: rowCount}); fixRange(range, 0, 0, rowCount, xSteps, ySteps); - getHeaderCell(col, cellRef, ws); + var colTitle = col; + if (typeof col === 'object'){ + colTitle = col.title; + columnsWidth.push(col.width || {wpx:80}); /* wch (chars), wpx (pixels) - e.g. [{wch:6},{wpx:50}] */ + } + getHeaderCell(colTitle, cellRef, ws); }); rowCount += 1; } + if (columnsWidth.length > 0){ + ws['!cols'] = columnsWidth; + } + for (var R = 0; R != data.length; ++R, rowCount++) { for (var C = 0; C != data[R].length; ++C) { var cellRef = XLSX.utils.encode_cell({c: C + xSteps, r: rowCount});