javascript - 使用javascript导出excel时合并列?

标签 javascript export-to-excel

我可以导出 excel,但问题是我无法合并 excel 标题列。我附上了一张你可以检查的图片。这就是我想要这样导出的内容。

this is what I want to export my excel using javascript

我想在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>


我使用此代码导出它工作正常。我需要合并第一列单元格。
如果有任何解决方案,您可以避免此解决方案。

这是引用链接: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);

最佳答案

我建议使用基于 javascript 的 excel 库(例如 SheetJS )来生成真正的 excel 文件。与通过具有虚假 mime 类型的 csv 或 html 文件伪造它相比,您将有更多的选择,这就是您的示例代码正在做的事情。

这是 fiddle .

<html>
<head>
<script src="//unpkg.com/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>

关于javascript - 使用javascript导出excel时合并列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61842227/

相关文章:

javascript - 将带分页的 PHP 表导出为 EXCEL、PDF、PRINTABLE

javascript - 替换整个元素而不是 innerHTML?

javascript - 如何将普通数组转换为 Float32Array?

php - 如果我托管 JS 文件,我可以识别嵌入它的 URL 吗?

c# - 如何将 datagridview 单元格格式值导出到 Excel?

c# - 在 C# 中将 Excel 列(或单元格)格式化为文本?

excel - 我可以从数据源导入 INTO excel 而无需迭代吗?

JavaScript 井字棋计时器

javascript - jQuery 禁用启用按钮样式

php - 将 html 表格内容导出为 xlsx 格式