admin管理员组

文章数量:1317898

I can export excel but the problem is I can't merge the excel header column. I have attached an image you can check. this is what I want to export like this.

i want to export this table in excel.

                    <table id="" class="uk-report-table table table-striped">
                        <thead>
                            <tr>
                                <th colspan="1">Date Range</th>
                                <th colspan="5">
                                    <h2>Last 30 Days</h2>
                                </th>
                                <th colspan="5">
                                    <h2>Previous 30 Days</h2>
                                </th>
                            </tr>
                            <tr>
                                <th>A</th>
                                <th>B</th>
                                <th>C</th>
                                <th>D</th>
                                <th>E</th>
                                <th>A2</th>
                                <th>B2</th>
                                <th>C2</th>
                                <th>D2</th>
                                <th>E2</th>
                            </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>

I used this code to export it works fine. I need to merge the first column cells. you can avoid this solution if is there any solution for that.

here is the reference link: How to export Javascript array data to excel on the client side

var lineArray = [];
result_table.forEach(function(infoArray, index) {
  var line = infoArray.join(" \t");
  lineArray.push(index == 0 ? line : line);
});
var csvContent = lineArray.join("\r\n");
var excel_file = document.createElement('a');
excel_file.setAttribute('href', 'data:application/vnd.ms-excel;charset=utf-8,' + encodeURIComponent(csvContent));
excel_file.setAttribute('download', 'Visitor_History.xls');
document.body.appendChild(excel_file);
excel_file.click();
document.body.removeChild(excel_file);

I can export excel but the problem is I can't merge the excel header column. I have attached an image you can check. this is what I want to export like this.

i want to export this table in excel.

                    <table id="" class="uk-report-table table table-striped">
                        <thead>
                            <tr>
                                <th colspan="1">Date Range</th>
                                <th colspan="5">
                                    <h2>Last 30 Days</h2>
                                </th>
                                <th colspan="5">
                                    <h2>Previous 30 Days</h2>
                                </th>
                            </tr>
                            <tr>
                                <th>A</th>
                                <th>B</th>
                                <th>C</th>
                                <th>D</th>
                                <th>E</th>
                                <th>A2</th>
                                <th>B2</th>
                                <th>C2</th>
                                <th>D2</th>
                                <th>E2</th>
                            </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>

I used this code to export it works fine. I need to merge the first column cells. you can avoid this solution if is there any solution for that.

here is the reference link: How to export Javascript array data to excel on the client side

var lineArray = [];
result_table.forEach(function(infoArray, index) {
  var line = infoArray.join(" \t");
  lineArray.push(index == 0 ? line : line);
});
var csvContent = lineArray.join("\r\n");
var excel_file = document.createElement('a');
excel_file.setAttribute('href', 'data:application/vnd.ms-excel;charset=utf-8,' + encodeURIComponent(csvContent));
excel_file.setAttribute('download', 'Visitor_History.xls');
document.body.appendChild(excel_file);
excel_file.click();
document.body.removeChild(excel_file);
Share Improve this question edited May 16, 2020 at 19:45 Mustak_Talukder asked May 16, 2020 at 19:11 Mustak_TalukderMustak_Talukder 1431 silver badge9 bronze badges 3
  • I didn't find any solution so that asked . would you suggest any of the existing solutions? – Mustak_Talukder Commented May 16, 2020 at 19:18
  • Would you share your detailed code description ? – Aman Kumayu Commented May 16, 2020 at 19:19
  • I sheared my table formate. I want this table export to excel. I have attached an example photo that I want to export. you can suggest me any solution for that using javascript. – Mustak_Talukder Commented May 16, 2020 at 19:29
Add a ment  | 

1 Answer 1

Reset to default 7

I'd suggest using a javascript based excel library (e.g. SheetJS) to produce a true excel file. You'll have a lot more options with it than faking it via a csv or html file with a bogus mime-type which is what your sample code is doing.

Here's a fiddle.

<html>
<head>
<script src="//unpkg./xlsx/dist/xlsx.full.min.js" type="text/javascript"></script>
<script>
function exportFile(){
  var wb = XLSX.utils.table_to_book(document.getElementById('sampletable'));
  XLSX.writeFile(wb, 'sample.xlsx');
  return false;
}
</script>
</head>
<body>
<table id="sampletable" class="uk-report-table table table-striped">
                        <thead>
                            <tr>
                                <th colspan="1">Date Range</th>
                                <th colspan="5">
                                    <h2>Last 30 Days</h2>
                                </th>
                                <th colspan="5">
                                    <h2>Previous 30 Days</h2>
                                </th>
                            </tr>
                            <tr>
                                <th>A</th>
                                <th>B</th>
                                <th>C</th>
                                <th>D</th>
                                <th>E</th>
                                <th>A2</th>
                                <th>B2</th>
                                <th>C2</th>
                                <th>D2</th>
                                <th>E2</th>
                            </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
<button onclick="return exportFile()">Export</button>
</body>
</html>

本文标签: MERGE column when export excel using javascriptStack Overflow