jQuery导出excel、pdf文件文件及打印页面

1.导出excel
由于没有找到jquery-table2excel插件在线引用,所以我们需要去官网下载
jquery-table2excel插件的github地址为: https://github.com/rainabba/jquery-table2excel

<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>导出excel</title>
		<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
		<script src="js/jquery.table2excel.js"></script>
		<script>
			window.onload = function() {
				$('#button1').click(function() {
					console.log(1)
					$("#dataTable").table2excel({
					//      table2excel插件的可用配置参数有:
						exclude: ".noExl",//   exclude:不被导出的表格行的CSS class类。
						name: "Excel Document Name",//   name:导出的Excel文档的名称
						filename: "myFileName",//   filename:Excel文件的名称。
						exclude_img: true,//   exclude_img:是否导出图片。
						exclude_links: true,//   exclude_links:是否导出超链接
						exclude_inputs: true//    exclude_inputs:是否导出输入框中的内容
					});
				})
			}
			
		</script>
	</head>

	<body>
		<table border="1" id="dataTable">
			<tr>
				<td>学号</td>
				<td>姓名 </td>
			</tr>
			<tr>
				<td>1001</td>
				<td>张三</td>
			</tr>
			<tr>
				<td>1002</td>
				<td>李四</td>
			</tr>
		</table>
		<br>
		<button class="btn btn-primary btn-lg" data-toggle="modal" id="button1">导出EXCEL</button>

	</body>

</html>

2.导出pdf

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>导出PDF</title>
		<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
	    <script src="https://cdn.bootcss.com/html2canvas/0.5.0-beta4/html2canvas.js"></script>
        <script src="https://cdn.bootcss.com/jspdf/1.3.4/jspdf.debug.js"></script>

<script type="text/javascript">
	window.onload = function() {
    var downPdf = document.getElementById("exportToPdf");
    downPdf.onclick = function () {
        html2canvas(
                document.getElementById("export_content"),
                {
                    dpi: 172,//导出pdf清晰度
                    onrendered: function (canvas) {
                        var contentWidth = canvas.width;
                        var contentHeight = canvas.height;

                        //一页pdf显示html页面生成的canvas高度;
                        var pageHeight = contentWidth / 592.28 * 841.89;
                        //未生成pdf的html页面高度
                        var leftHeight = contentHeight;
                        //pdf页面偏移
                        var position = 0;
                        //html页面生成的canvas在pdf中图片的宽高(a4纸的尺寸[595.28,841.89])
                        var imgWidth = 595.28;
                        var imgHeight = 592.28 / contentWidth * contentHeight;

                        var pageData = canvas.toDataURL('image/jpeg', 1.0);
                        var pdf = new jsPDF('', 'pt', 'a4');

                        //有两个高度需要区分,一个是html页面的实际高度,和生成pdf的页面高度(841.89)
                        //当内容未超过pdf一页显示的范围,无需分页
                        if (leftHeight < pageHeight) {
                            pdf.addImage(pageData, 'JPEG', 0, 0, imgWidth, imgHeight);
                        } else {
                            while (leftHeight > 0) {
                                pdf.addImage(pageData, 'JPEG', 0, position, imgWidth, imgHeight)
                                leftHeight -= pageHeight;
                                position -= 841.89;
                                //避免添加空白页
                                if (leftHeight > 0) {
                                    pdf.addPage();
                                }
                            }
                        }
                        pdf.save('content.pdf');
                    },
                    //背景设为白色(默认为黑色)
                    background: "#fff"  
                })
    }
    }
</script>

	</head>
	<body>
		<button id="exportToPdf">导出PDF</button>
<div id="export_content">
    <table id="customers" class="table table-striped table-bordered" border="1">
        <thead>
            <tr class='warning'>
                <th>学号</th>
                <th>姓名</th>
                <th>班级</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>1001</td>
                <td>张三</td>
                <td>一班</td>
            </tr>
            <tr>
                <td>1002</td>
                <td>李四</td>
                <td>二班</td>
            </tr>
            <tr>
                <td>1003</td>
                <td>王五</td>
                <td>一班</td>
            </tr>
            <tr>
                <td>1004</td>
                <td>赵六</td>
                <td>J三班</td>
            </tr>
          
        </tbody>
    </table>
</div>

	</body>
</html>

3.打印

jquery.jqprint-0.3.js官网下载地址:https://webscripts.softpedia.com/script/Modules/jQuery-Plugins/jqPrint-68448.html

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>打印</title>

<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script language="javascript" src="js/jquery.jqprint-0.3.js"></script>
<script src="http://www.jq22.com/jquery/jquery-migrate-1.2.1.min.js";></script>
<script language="javascript">
    function aa() {
         $("#ddd").jqprint();
    }
</script>
</head>
  
<body>
    <div id="ddd">
         <table>
             <tr>
                 <table border="1" id="dataTable">
			<tr>
				<td>学号</td>
				<td>姓名 </td>
			</tr>
			<tr>
				<td>1001</td>
				<td>张三</td>
			</tr>
			<tr>
				<td>1002</td>
				<td>李四</td>
			</tr>
		</table>
             </tr>
         </table>
    </div>
    <input type="button" onclick="aa()" value="打印" />
</body>
</html>

猜你喜欢

转载自blog.csdn.net/qq_38720976/article/details/84427064