前端jquery导入excle表格笔记

该博客记录了前端使用jQuery导入Excel表格的相关笔记,提供了在js文件中复制粘贴的操作内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前端jquery导入excle表格笔记

<!DOCTYPE html>
<html>
	<head lang="en">
		<meta charset="UTF-8">
		<title>html 表格导出道</title>
		<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
		<script src="js/jquery_table.js"></script>//创建js文件 路径填写自己的存放路径
		<script language="JavaScript" type="text/javascript">
			$(document).ready(function() {
				$("#btnExport").click(function() {
					$("#tableExcel tr").each(function(i, _tr) { //循环表格的行
						var tds = $(_tr).find("td"); //得到每行的所有列
						tds.each(function(j, _td) { //循环每列
							var _ipt = $(_td).find("input"); //查找每列当中的INPUT控件
							var hasInput = (_ipt.length > 0) ? 1 : 0;
							if (hasInput) {
								//console.log('第'+(i+1)+'行'+(j+1)+'列含有INPUT控件');
								_ipt.attr("value", _ipt.val()); //如果有INPUT控件就给它设置value值
							}
						});
					});

					$("#tableExcel").table2excel({
						exclude: ".noExl", //过滤位置的 css 类名
						filename: "你想说啥", //文件名称
						name: "Excel Document Name",
						exclude_img: true,
						exclude_links: true,
						exclude_inputs: true

					});
				});
			});
		</script>
	</head>
	<body>

		<div>

			<button type="button" id="btnExport" onclick="method5('tableExcel')">导出Excel</button>
		</div>
		<div id="myDiv">
			<table border="0" cellspacing="0" cellpadding="0" id="tableExcel" class="xd_table_sj">

				<tbody>
					<tr>
						<td colspan="8">
							<div align="center" id="titlelable">起始时间</div>
						</td>
						<!-- <td><div align="center" id="titlelable">通信地点</div></td>  
                            <td><div align="center" id="titlelable">上网方式</div></td>  
                            <td><div align="center" id="titlelable">总时长</div></td>  
                            <td><div align="center" id="titlelable">总流量</div></td>  
                            <td><div align="center" id="titlelable">套餐优惠</div></td>  
                            <td><div align="center" id="titlelable">优惠或减免</div></td>  
                            <td><div align="center" id="titlelable">通信费</div></td>  
                            <td><div align="center" id="titlelable">终端类型</div></td>  -->
					</tr>


					<tr bgcolor="#EFFEDD" onmouseover="this.style.background='#D2FCA0'" onmouseout="this.style.background='#EFFEDD'"
					 style="background: rgb(239, 254, 221);">
						<td>10-01 01:57:05</td>
						<td></td>
						<td>CMNET</td>
						<td>0秒</td>
						<td>0.001</td>
						<td>校园4G套餐-400M国内流量</td>
						<td></td>
						<td>0.00</td>
						<td></td>
					</tr>

					<tr bgcolor="#ffffff" onmouseover="this.style.background='#D2FCA0'" onmouseout="this.style.background='#ffffff'"
					 style="background: rgb(255, 255, 255);">
						<td>10-01 01:58:55</td>
						<td></td>
						<td>CMNET</td>
						<td>0秒</td>
						<td>0.007</td>
						<td>校园4G套餐-400M国内流量</td>
						<td></td>
						<td>0.00</td>
						<td></td>
					</tr>

					<tr bgcolor="#EFFEDD" onmouseover="this.style.background='#D2FCA0'" onmouseout="this.style.background='#EFFEDD'"
					 style="background: rgb(239, 254, 221);">
						<td>10-01 07:00:00</td>
						<td></td>
						<td>CMNET</td>
						<td>0秒</td>
						<td>0.001</td>
						<td>校园4G套餐-400M国内流量</td>
						<td></td>
						<td>0.00</td>
						<td></td>
					</tr>

					<tr bgcolor="#ffffff" onmouseover="this.style.background='#D2FCA0'" onmouseout="this.style.background='#ffffff'"
					 style="background: rgb(255, 255, 255);">
						<td>10-01 07:23:19</td>
						<td></td>
						<td>CMNET</td>
						<td>0秒</td>
						<td>0.084</td>
						<td>校园4G套餐-400M国内流量</td>
						<td></td>
						<td>0.00</td>
						<td></td>
					</tr>

					<tr bgcolor="#EFFEDD" onmouseover="this.style.background='#D2FCA0'" onmouseout="this.style.background='#EFFEDD'"
					 style="background: rgb(239, 254, 221);">
						<td>10-01 08:16:31</td>
						<td></td>
						<td>CMNET</td>
						<td>0秒</td>
						<td>0.001</td>
						<td>校园4G套餐-400M国内流量</td>
						<td></td>
						<td>0.00</td>
						<td></td>
					</tr>

					<tr bgcolor="#ffffff" onmouseover="this.style.background='#D2FCA0'" onmouseout="this.style.background='#ffffff'"
					 style="background: rgb(255, 255, 255);">
						<td>10-01 08:16:39</td>
						<td></td>
						<td>CMNET</td>
						<td>0秒</td>
						<td>0.06</td>
						<td>校园4G套餐-400M国内流量</td>
						<td></td>
						<td>0.00</td>
						<td></td>
					</tr>

					<tr bgcolor="#EFFEDD" onmouseover="this.style.background='#D2FCA0'" onmouseout="this.style.background='#EFFEDD'"
					 style="background: rgb(239, 254, 221);">
						<td>10-01 08:28:44</td>
						<td></td>
						<td>CMNET</td>
						<td>0秒</td>
						<td>0.002</td>
						<td>校园4G套餐-400M国内流量</td>
						<td></td>
						<td>0.00</td>
						<td></td>
					</tr>

					<tr bgcolor="#ffffff" onmouseover="this.style.background='#D2FCA0'" onmouseout="this.style.background='#ffffff'"
					 style="background: rgb(255, 255, 255);">
						<td>10-01 08:34:50</td>
						<td></td>
						<td>CMNET</td>
						<td>0秒</td>
						<td>0.259</td>
						<td>校园4G套餐-400M国内流量</td>
						<td></td>
						<td>0.00</td>
						<td></td>
					</tr>

					<tr bgcolor="#EFFEDD" onmouseover="this.style.background='#D2FCA0'" onmouseout="this.style.background='#EFFEDD'"
					 style="background: rgb(239, 254, 221);">
						<td>10-01 08:34:50</td>
						<td></td>
						<td>CMNET</td>
						<td>0秒</td>
						<td>1.26</td>
						<td>校园4G套餐-400M国内流量</td>
						<td></td>
						<td>0.00</td>
						<td></td>
					</tr>

					<tr bgcolor="#ffffff" onmouseover="this.style.background='#D2FCA0'" onmouseout="this.style.background='#ffffff'"
					 style="background: rgb(255, 255, 255);">
						<td>10-01 09:54:53</td>
						<td></td>
						<td>CMNET</td>
						<td>0秒</td>
						<td>1.357</td>
						<td>校园4G套餐-400M国内流量</td>
						<td></td>
						<td>0.00</td>
						<td></td>
					</tr>

					<tr bgcolor="#EFFEDD" onmouseover="this.style.background='#D2FCA0'" onmouseout="this.style.background='#EFFEDD'"
					 style="background: rgb(239, 254, 221);">
						<td>10-01 09:56:29</td>
						<td></td>
						<td>CMNET</td>
						<td>0秒</td>
						<td>0.003</td>
						<td>校园4G套餐-400M国内流量</td>
						<td></td>
						<td>0.00</td>
						<td></td>
					</tr>

					<tr bgcolor="#ffffff" onmouseover="this.style.background='#D2FCA0'" onmouseout="this.style.background='#ffffff'"
					 style="background: rgb(255, 255, 255);">
						<td>10-01 09:56:31</td>
						<td></td>
						<td>CMNET</td>
						<td>0秒</td>
						<td>0.009</td>
						<td>校园4G套餐-400M国内流量</td>
						<td></td>
						<td>0.00</td>
						<td></td>
					</tr>

					<tr bgcolor="#EFFEDD" onmouseover="this.style.background='#D2FCA0'" onmouseout="this.style.background='#EFFEDD'"
					 style="background: rgb(239, 254, 221);">
						<td>10-01 09:56:33</td>
						<td></td>
						<td>CMNET</td>
						<td>0秒</td>
						<td>0.583</td>
						<td>校园4G套餐-400M国内流量</td>
						<td></td>
						<td>0.00</td>
						<td></td>
					</tr>

					<tr bgcolor="#ffffff" onmouseover="this.style.background='#D2FCA0'" onmouseout="this.style.background='#ffffff'"
					 style="background: rgb(255, 255, 255);">
						<td>10-01 10:27:16</td>
						<td></td>
						<td>CMNET</td>
						<td>0秒</td>
						<td>0.001</td>
						<td>校园4G套餐-400M国内流量</td>
						<td></td>
						<td>0.00</td>
						<td></td>
					</tr>

					<tr bgcolor="#EFFEDD" onmouseover="this.style.background='#D2FCA0'" onmouseout="this.style.background='#EFFEDD'"
					 style="background: rgb(239, 254, 221);">
						<td>10-01 10:28:58</td>
						<td></td>
						<td>CMNET</td>
						<td>0秒</td>
						<td>0.004</td>
						<td>校园4G套餐-400M国内流量</td>
						<td></td>
						<td>0.00</td>
						<td></td>
					</tr>

					<tr bgcolor="#ffffff" onmouseover="this.style.background='#D2FCA0'" onmouseout="this.style.background='#ffffff'"
					 style="background: rgb(255, 255, 255);">
						<td>10-01 12:44:12</td>
						<td></td>
						<td>CMNET</td>
						<td>0秒</td>
						<td>0.001</td>
						<td>校园4G套餐-400M国内流量</td>
						<td></td>
						<td>0.00</td>
						<td></td>
					</tr>

					<tr bgcolor="#EFFEDD" onmouseover="this.style.background='#D2FCA0'" onmouseout="this.style.background='#EFFEDD'"
					 style="background: rgb(239, 254, 221);">
						<td>10-01 12:45:41</td>
						<td></td>
						<td>CMNET</td>
						<td>0秒</td>
						<td>0.411</td>
						<td>校园4G套餐-400M国内流量</td>
						<td></td>
						<td>0.00</td>
						<td></td>
					</tr>

					<tr bgcolor="#ffffff" onmouseover="this.style.background='#D2FCA0'" onmouseout="this.style.background='#ffffff'"
					 style="background: rgb(255, 255, 255);">
						<td>10-01 12:57:42</td>
						<td></td>
						<td>CMNET</td>
						<td>0秒</td>
						<td>0.024</td>
						<td>校园4G套餐-400M国内流量</td>
						<td></td>
						<td>0.00</td>
						<td></td>
					</tr>

					<tr bgcolor="#EFFEDD" onmouseover="this.style.background='#D2FCA0'" onmouseout="this.style.background='#EFFEDD'"
					 style="background: rgb(239, 254, 221);">
						<td>10-01 12:57:50</td>
						<td></td>
						<td>CMNET</td>
						<td>0秒</td>
						<td>0.009</td>
						<td>校园4G套餐-400M国内流量</td>
						<td></td>
						<td>0.00</td>
						<td></td>
					</tr>

					<tr bgcolor="#ffffff" onmouseover="this.style.background='#D2FCA0'" onmouseout="this.style.background='#ffffff'"
					 style="background: rgb(255, 255, 255);">
						<td>10-01 12:57:52</td>
						<td></td>
						<td>CMNET</td>
						<td>0秒</td>
						<td>0.007</td>
						<td>校园4G套餐-400M国内流量</td>
						<td></td>
						<td>0.00</td>
						<td></td>
					</tr>
					<tr>
						<td colspan="2">
							<div>填表人:<input type="text"></div>
						</td>
						<td colspan="3">
							<div>审核人:<input type="text"></div>
						</td>
						<td colspan="3">
							<div>填表时间:<input type="date"></div>
						</td>
					</tr>
				</tbody>
			</table>
		</div>
	</body>
</html>

下面内容建立js文件复制粘贴进去

;(function ( $, window, document, undefined ) {
    var pluginName = "table2excel",

    defaults = {
        exclude: ".noExl",
        name: "Table2Excel",
        filename: "table2excel",
        fileext: ".xls",
        exclude_img: true,
        exclude_links: true,
        exclude_inputs: true
    };

    // The actual plugin constructor
    function Plugin ( element, options ) {
            this.element = element;
            // jQuery has an extend method which merges the contents of two or
            // more objects, storing the result in the first object. The first object
            // is generally empty as we don't want to alter the default options for
            // future instances of the plugin
            //
            this.settings = $.extend( {}, defaults, options );
            this._defaults = defaults;
            this._name = pluginName;
            this.init();
    }

    Plugin.prototype = {
        init: function () {
            var e = this;

            var utf8Heading = "<meta http-equiv=\"content-type\" content=\"application/vnd.ms-excel; charset=UTF-8\">";
            e.template = {
                head: "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\">" + utf8Heading + "<head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets>",
                sheet: {
                    head: "<x:ExcelWorksheet><x:Name>",
                    tail: "</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet>"
                },
                mid: "</x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body>",
                table: {
                    head: "<table>",
                    tail: "</table>"
                },
                foot: "</body></html>"
            };

            e.tableRows = [];

            // get contents of table except for exclude
            $(e.element).each( function(i,o) {
                var tempRows = "";
                $(o).find("tr").not(e.settings.exclude).each(function (i,p) {
                    
                    tempRows += "<tr>";
                    $(p).find("td,th").not(e.settings.exclude).each(function (i,q) { // p did not exist, I corrected
                        
                        var rc = {
                            rows: $(this).attr("rowspan"),
                            cols: $(this).attr("colspan"),
                            flag: $(q).find(e.settings.exclude)
                        };
                        
                        if( rc.flag.length > 0 ) {
                            tempRows += "<td> </td>"; // exclude it!!
                        } else {
                            if( rc.rows  & rc.cols ) {
                                tempRows += "<td>" + $(q).html() + "</td>";
                            } else {
                                tempRows += "<td";
                                if( rc.rows > 0) {
                                    tempRows += " rowspan=\'" + rc.rows + "\' ";
                                }
                                if( rc.cols > 0) {
                                    tempRows += " colspan=\'" + rc.cols + "\' ";
                                }
                                tempRows += "/>" + $(q).html() + "</td>";
                            }
                        }
                    });

                    tempRows += "</tr>";
                    
                });
                // exclude img tags
                if(e.settings.exclude_img) {
                    tempRows = exclude_img(tempRows);
                }

                // exclude link tags
                if(e.settings.exclude_links) {
                    tempRows = exclude_links(tempRows);
                }

                // exclude input tags
                if(e.settings.exclude_inputs) {
                    tempRows = exclude_inputs(tempRows);
                }
                e.tableRows.push(tempRows);
            });

            e.tableToExcel(e.tableRows, e.settings.name, e.settings.sheetName);
        },

        tableToExcel: function (table, name, sheetName) {
            var e = this, fullTemplate="", i, link, a;

            e.format = function (s, c) {
                return s.replace(/{(\w+)}/g, function (m, p) {
                    return c[p];
                });
            };

            sheetName = typeof sheetName === "undefined" ? "Sheet" : sheetName;

            e.ctx = {
                worksheet: name || "Worksheet",
                table: table,
                sheetName: sheetName
            };

            fullTemplate= e.template.head;

            if ( $.isArray(table) ) {
                for (i in table) {
                      //fullTemplate += e.template.sheet.head + "{worksheet" + i + "}" + e.template.sheet.tail;
                      fullTemplate += e.template.sheet.head + sheetName + i + e.template.sheet.tail;
                }
            }

            fullTemplate += e.template.mid;

            if ( $.isArray(table) ) {
                for (i in table) {
                    fullTemplate += e.template.table.head + "{table" + i + "}" + e.template.table.tail;
                }
            }

            fullTemplate += e.template.foot;

            for (i in table) {
                e.ctx["table" + i] = table[i];
            }
            delete e.ctx.table;

            var isIE = /*@cc_on!@*/false || !!document.documentMode; // this works with IE10 and IE11 both :)            
            //if (typeof msie !== "undefined" && msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./))      // this works ONLY with IE 11!!!
            if (isIE) {
                if (typeof Blob !== "undefined") {
                    //use blobs if we can
                    fullTemplate = e.format(fullTemplate, e.ctx); // with this, works with IE
                    fullTemplate = [fullTemplate];
                    //convert to array
                    var blob1 = new Blob(fullTemplate, { type: "text/html" });
                    window.navigator.msSaveBlob(blob1, getFileName(e.settings)+".xls" );
                } else {
                    //otherwise use the iframe and save
                    //requires a blank iframe on page called txtArea1
                    txtArea1.document.open("text/html", "replace");
                    txtArea1.document.write(e.format(fullTemplate, e.ctx));
                    txtArea1.document.close();
                    txtArea1.focus();
                    sa = txtArea1.document.execCommand("SaveAs", true, getFileName(e.settings) + ".xls" );
                }

            } else {
                var blob = new Blob([e.format(fullTemplate, e.ctx)], {type: "application/vnd.ms-excel"});
                window.URL = window.URL || window.webkitURL;
                link = window.URL.createObjectURL(blob);
                a = document.createElement("a");
                a.download = getFileName(e.settings) + ".xls";
                a.href = link;

                document.body.appendChild(a);

                a.click();

                document.body.removeChild(a);
            }

            return true;
        }
    };

    function getFileName(settings) {
        return ( settings.filename ? settings.filename : "table2excel" );
    }

    // Removes all img tags
    function exclude_img(string) {
        var _patt = /(\s+alt\s*=\s*"([^"]*)"|\s+alt\s*=\s*'([^']*)')/i;
        return string.replace(/<img[^>]*>/gi, function myFunction(x){
            var res = _patt.exec(x);
            if (res !== null && res.length >=2) {
                return res[2];
            } else {
                return "";
            }
        });
    }

    // Removes all link tags
    function exclude_links(string) {
        return string.replace(/<a[^>]*>|<\/a>/gi, "");
    }

    // Removes input params
    function exclude_inputs(string) {
        var _patt = /(\s+value\s*=\s*"([^"]*)"|\s+value\s*=\s*'([^']*)')/i;
        return string.replace(/<input[^>]*>|<\/input>/gi, function myFunction(x){
            var res = _patt.exec(x);
            if (res !== null && res.length >=2) {
                return res[2];
            } else {
                return "";
            }
        });
    }

    $.fn[ pluginName ] = function ( options ) {
        var e = this;
            e.each(function() {
                if ( !$.data( e, "plugin_" + pluginName ) ) {
                    $.data( e, "plugin_" + pluginName, new Plugin( this, options ) );
                }
            });

        // chain jQuery functions
        return e;
    };

})( jQuery, window, document );
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值