我正在将一个HTML表格导出到Excel,但它给我的错误如下:
“您尝试打开的文件格式与文件扩展名指定的格式不同......”
我尝试了不同的扩展名,例如.xlsx也仍然给出相同的错误。下面是我的jQuery代码:
$(document).ready(function() {
$("#btnExport").click(function(e) {
e.preventDefault();
//getting data from our table
var data_type = 'data:application/vnd.ms-excel';
var table_div = document.getElementById('table_wrapper');
var table_html = table_div.outerHTML.replace(/ /g, '%20');
var a = document.createElement('a');
a.href = data_type + ', ' + table_html;
a.download = 'DSR_Report_' + Math.floor((Math.random() * 9999999) + 1000000) + '.xls';
a.click();
});
});
我导出到Excel的HTML表格:
<div id="table_wrapper">
<table border="1" id="list" class="table-style-two">
<tbody>
<tr>
<th>NAME</th>
<th>PROJECT_NAME</th>
<th>DESCRIPTION</th>
<th>HOURS</th>
<th>START_DATE</th>
<th>CURRENT_PROJECT_STATUS</th>
<th>WORK_FOR_TOMORROW</th>
<th>TOMORROW_WORK_STATUS</th>
<th>COMMENTS</th>
<th>VIEW_POINT_TICKET</th>
<th>DSR_CURRENT_DATE</th>
</tr>
<tr id="417">
<td>Anupam Bhattacharjee</td>
<td>Cybage</td>
<td>Daily standup meeting</td>
<td>0.5</td>
<td>2015-11-20</td>
<td>Complete</td>
<td>NA</td>
<td>NA</td>
<td>NA</td>
<td>NA</td>
<td>2015-11-20</td>
</tr>
<tr id="418">
<td>Anupam Bhattacharjee</td>
<td>Tomford</td>
<td>TF3799-28303 - Assisted Pavan in resolving the issue via skype.Fixed issue in Tomford mule where the OrderShipment was erring out.</td>
<td>1.5</td>
<td>2015-11-19</td>
<td>Complete</td>
<td>NA</td>
<td>NA</td>
<td>NA</td>
<td>WO3799</td>
<td>2015-11-20</td>
</tr>
</tbody>
</table>
</div>
编辑:这不是this问题的重复,因为该问题用户无法导出,但我能够导出并导出我的导出的Excel文件。
答案 0 :(得分:1)
Check here might this will help you out
$(document).ready(function () {
function exportTableToCSV($table, filename) {
var $rows = $table.find('tr:has(td)'),
// Temporary delimiter characters unlikely to be typed by keyboard
// This is to avoid accidentally splitting the actual contents
tmpColDelim = String.fromCharCode(11), // vertical tab character
tmpRowDelim = String.fromCharCode(0), // null character
// actual delimiter characters for CSV format
colDelim = '","',
rowDelim = '"\r\n"',
// Grab text from table into CSV formatted string
csv = '"' + $rows.map(function (i, row) {
var $row = $(row),
$cols = $row.find('td');
return $cols.map(function (j, col) {
var $col = $(col),
text = $col.text();
return text.replace(/"/g, '""'); // escape double quotes
}).get().join(tmpColDelim);
}).get().join(tmpRowDelim)
.split(tmpRowDelim).join(rowDelim)
.split(tmpColDelim).join(colDelim) + '"',
// Data URI
csvData = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csv);
$(this)
.attr({
'download': filename,
'href': csvData,
'target': '_blank'
});
}
// This must be a hyperlink
$(".export").on('click', function (event) {
// CSV
exportTableToCSV.apply(this, [$('#dvData>table'), 'export.csv']);
// IF CSV, don't do event.preventDefault() or return false
// We actually need this to be a typical hyperlink
});
});
答案 1 :(得分:1)
您可以尝试使用此插件 - tableExport.js
<强> HTML:强>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="src/jquery.table2excel.js"></script>
<body>
<tr class="noExl">
<th>#</th>
<th>Column heading</th>
<th>Column heading</th>
<th>Column heading</th>
</tr>
</body>
<强> jQuery的:强>
$("button").click(function(){
$("#table2excel").table2excel({
// exclude CSS class
exclude: ".noExl",
name: "Excel Document Name"
});
});
答案 2 :(得分:0)
Javascript
abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+
HTML
$("#btn").click(function (e) {
window.open('data:application/vnd.ms-excel,' + $('#tableData').html());
return false;
});
答案 3 :(得分:0)
该文件基本上只是一个带有XLS扩展名的CSV文件,可以在Excel中打开它。如果您遇到问题,只需将文件名重命名为&#39; .csv&#39;。 这只是我的一个好答案。Here