jQuery导出Excel格式

1、问题背景

     将一个表格的数据导出到Excel中,保证不出现中文乱码


2、实现源码

  1. <!DOCTYPE html>  
  2. <html>  
  3.     <head>  
  4.         <meta charset="utf-8" />  
  5.         <title>导出Excel格式</title>  
  6.         <script type="text/javascript" src="js/jquery-1.12.3.js" ></script>  
  7.         <script type="text/javascript" src="js/jquery.base64.js" ></script>  
  8.         <script type="text/javascript" src="js/tableExport.js" ></script>  
  9.         <script type="text/javascript" src="js/jspdf/libs/sprintf.js" ></script>  
  10.         <script type="text/javascript" src="js/jspdf/jspdf.js" ></script>  
  11.         <script type="text/javascript" src="js/jspdf/libs/base64.js" ></script>  
  12.         <style>  
  13.             html,body{  
  14.                 width: 99%;  
  15.                 height: 99%;  
  16.                 font-family: "微软雅黑";  
  17.                 font-size: 12px;  
  18.             }  
  19.             #tables{  
  20.                 width: 100%;  
  21.                 text-align: center;  
  22.                 border: 1px #000 solid;  
  23.                 border-collapse: collapse;  
  24.             }  
  25.             #tables th,#tables td{  
  26.                 border: 1px solid #000000;  
  27.             }  
  28.             #tables th{  
  29.                 font-size: 14px;  
  30.                 font-weight: bolder;  
  31.             }  
  32.         </style>  
  33.         <script>  
  34.             $(document).ready(function(){  
  35.                 $("#exportData").click(function(){  
  36.                     //导出  
  37.                     $("#tables").tableExport({type:"excel",escape:"false"});  
  38.                 });  
  39.             });  
  40.         </script>  
  41.     </head>  
  42.     <body>  
  43.         <table id="tables">  
  44.             <thead>  
  45.                 <tr>  
  46.                     <th>姓名</th>  
  47.                     <th>性别</th>  
  48.                     <th>年龄</th>  
  49.                     <th>地址</th>  
  50.             </thead>  
  51.             <tbody>  
  52.                 <tr>  
  53.                     <td>张三</td>  
  54.                     <td></td>  
  55.                     <td>34</td>  
  56.                     <td>湖北省武汉市</td>  
  57.                 </tr>  
  58.                 <tr>  
  59.                     <td>张三</td>  
  60.                     <td></td>  
  61.                     <td>34</td>  
  62.                     <td>湖北省武汉市</td>  
  63.                 </tr>  
  64.                 <tr>  
  65.                     <td>张三</td>  
  66.                     <td></td>  
  67.                     <td>34</td>  
  68.                     <td>湖北省武汉市</td>  
  69.                 </tr>  
  70.                 <tr>  
  71.                     <td>张三</td>  
  72.                     <td></td>  
  73.                     <td>34</td>  
  74.                     <td>湖北省武汉市</td>  
  75.                 </tr>  
  76.                 <tr>  
  77.                     <td>张三</td>  
  78.                     <td></td>  
  79.                     <td>34</td>  
  80.                     <td>湖北省武汉市</td>  
  81.                 </tr>  
  82.                 <tr>  
  83.                     <td>张三</td>  
  84.                     <td></td>  
  85.                     <td>34</td>  
  86.                     <td>湖北省武汉市</td>  
  87.                 </tr>  
  88.                 <tr>  
  89.                     <td>张三</td>  
  90.                     <td></td>  
  91.                     <td>34</td>  
  92.                     <td>湖北省武汉市</td>  
  93.                 </tr>  
  94.                 <tr>  
  95.                     <td>张三</td>  
  96.                     <td></td>  
  97.                     <td>34</td>  
  98.                     <td>湖北省武汉市</td>  
  99.                 </tr>  
  100.                 <tr>  
  101.                     <td>张三</td>  
  102.                     <td></td>  
  103.                     <td>34</td>  
  104.                     <td>湖北省武汉市</td>  
  105.                 </tr>  
  106.                 <tr>  
  107.                     <td>张三</td>  
  108.                     <td></td>  
  109.                     <td>34</td>  
  110.                     <td>湖北省武汉市</td>  
  111.                 </tr>  
  112.             </tbody>  
  113.         </table>  
  114.         <input type="button" id="exportData" value="导出"/>  
  115.     </body>  
  116. </html>  
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>导出Excel格式</title>
		<script type="text/javascript" src="js/jquery-1.12.3.js" ></script>
		<script type="text/javascript" src="js/jquery.base64.js" ></script>
		<script type="text/javascript" src="js/tableExport.js" ></script>
		<script type="text/javascript" src="js/jspdf/libs/sprintf.js" ></script>
		<script type="text/javascript" src="js/jspdf/jspdf.js" ></script>
		<script type="text/javascript" src="js/jspdf/libs/base64.js" ></script>
		<style>
			html,body{
				width: 99%;
				height: 99%;
				font-family: "微软雅黑";
				font-size: 12px;
			}
			#tables{
				width: 100%;
				text-align: center;
				border: 1px #000 solid;
				border-collapse: collapse;
			}
			#tables th,#tables td{
				border: 1px solid #000000;
			}
			#tables th{
				font-size: 14px;
				font-weight: bolder;
			}
		</style>
		<script>
			$(document).ready(function(){
				$("#exportData").click(function(){
					//导出
					$("#tables").tableExport({type:"excel",escape:"false"});
				});
			});
		</script>
	</head>
	<body>
		<table id="tables">
			<thead>
				<tr>
					<th>姓名</th>
					<th>性别</th>
					<th>年龄</th>
					<th>地址</th>
			</thead>
			<tbody>
				<tr>
					<td>张三</td>
					<td>男</td>
					<td>34</td>
					<td>湖北省武汉市</td>
				</tr>
				<tr>
					<td>张三</td>
					<td>男</td>
					<td>34</td>
					<td>湖北省武汉市</td>
				</tr>
				<tr>
					<td>张三</td>
					<td>男</td>
					<td>34</td>
					<td>湖北省武汉市</td>
				</tr>
				<tr>
					<td>张三</td>
					<td>男</td>
					<td>34</td>
					<td>湖北省武汉市</td>
				</tr>
				<tr>
					<td>张三</td>
					<td>男</td>
					<td>34</td>
					<td>湖北省武汉市</td>
				</tr>
				<tr>
					<td>张三</td>
					<td>男</td>
					<td>34</td>
					<td>湖北省武汉市</td>
				</tr>
				<tr>
					<td>张三</td>
					<td>男</td>
					<td>34</td>
					<td>湖北省武汉市</td>
				</tr>
				<tr>
					<td>张三</td>
					<td>男</td>
					<td>34</td>
					<td>湖北省武汉市</td>
				</tr>
				<tr>
					<td>张三</td>
					<td>男</td>
					<td>34</td>
					<td>湖北省武汉市</td>
				</tr>
				<tr>
					<td>张三</td>
					<td>男</td>
					<td>34</td>
					<td>湖北省武汉市</td>
				</tr>
			</tbody>
		</table>
		<input type="button" id="exportData" value="导出"/>
	</body>
</html>

3、实现结果

(1)初始化


(2)点击导出


(3)打开Excel

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值