首页 > 解决方案 > table2excel:Excel 文件已下载但未在 msoffice 中打开

问题描述

我编写了以下代码以在 excel 中下载表格。它已下载但在 ms-office 中该文件未打开。请检查所附屏幕截图中的错误。请指导我解决问题。在此处输入图像描述

<!DOCTYPE html>
<html>
	<head>
		<title>jQuery Boilerplate</title>
		<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
		<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.js" type="text/javascript"></script>  
		<script src="https://rawgit.com/unconditional/jquery-table2excel/master/src/jquery.table2excel.js"></script>
	</head>
	<body>
		<table class="table2excel" data-tableName="Test Table 2">
			<thead>
				<tr class="noExl"><td>This shouldn't get exported</td><td>This shouldn't get exported either</td></tr>
				<tr><td>This Should get exported as a header</td><td>This should too</td></tr>
			</thead>
			<tbody>
				<tr><td>data1a</td><td>data1b</td></tr>
				<tr><td>data2a</td><td>data2b</td></tr>
			</tbody>
			<tfoot>
				<tr><td colspan="2">This footer spans 2 cells</td></tr>
			</tfoot>
		</table>
		<button class="exportToExcel">Export to XLS</button>

		<script>
			$.noConflict();
            jQuery(document).ready(function($){  
				$(".exportToExcel").click(function(e){
					var table = $(this).prev('.table2excel');
					if(table && table.length){
						var preserveColors = (table.hasClass('table2excel_with_colors') ? true : false);
						$(table).table2excel({
							exclude: ".noExl",
							name: "Excel Document Name",
							filename: "myFileName",
							fileext: ".xls"
						});
					}
				});
			});
		</script>
		<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
	</body>
</html>
注意:我在项目中使用了 admin 主题,因此我上传了 jquery 版本 3.3.1 和 2.1.4。并且由于 table2excel 无法正常工作,因此我使用了 noConflict() 函数。

标签: jquery

解决方案


更新后查了一下发现

jquery.table2excel.js

文件它工作正常。完成后,您可以从此处下载表格到 excel , 只需将您的 jquery.table2excel.js 替换为下载的文件。希望它的工作。


推荐阅读