If you want to export level1 also to excel, there is an alternative way:
Replace rowspan / colspan with empty cells like:
<thead> <tr> <th>Level 1</th> <th>Level 1 - Item 1</th> <th></th> <th>Level 1 - Item 2</th> <th></th> </tr> <tr> <th>Level 2</th> <th>Level 2 - Item 1a</th> <th>Level 2 - Item 1b</th> <th>Level 2 - Item 2a</th> <th>Level 2 - Item 2b</th> </tr> </thead>
Then, as suggested by @misiu above, Edit TableTools.js.
Find _fnGetDataTablesData and inside it change this:
if (oConfig.bHeader) { ... }
with this like:
if (oConfig.bHeader) { //another loop for (i = 0, rowCount = dt.nTHead.rows.length; i < rowCount; i++) { aRow = []; //clear row data for (j = 0, iLen = dt.aoColumns.length; j < iLen; j++) { if (aColumnsInc[j] && dt.nTHead.rows[i].children[j] !== null) { sLoopData = dt.nTHead.rows[i].children[j].innerHTML.replace(/\n/g, " ") .replace(/<.*?>/g, "") .replace(/^\s+|\s+$/g, ""); sLoopData = this._fnHtmlDecode(sLoopData); aRow.push(this._fnBoundData(sLoopData, oConfig.sFieldBoundary, regex)); } else { aRow.push(this._fnBoundData("", oConfig.sFieldBoundary, regex)); //I'm not shure of this!! } } aData.push(aRow.join(oConfig.sFieldSeperator)); } }
This will copy / export complex headers to csv / excel. Although empty table cells remain empty in excel and will not be merged. You can manually combine them.
Although this is not an ideal solution, for me it worked great now.
source share