{"id":318,"date":"2022-12-03T00:22:55","date_gmt":"2022-12-02T16:22:55","guid":{"rendered":"http:\/\/192.168.1.126\/tom_wp\/?p=318"},"modified":"2022-12-03T21:35:23","modified_gmt":"2022-12-03T13:35:23","slug":"table-to-csv","status":"publish","type":"post","link":"https:\/\/tomstudio.my.to\/?p=318","title":{"rendered":"table to csv"},"content":{"rendered":"\n<!--more-->\n\n\n\n<button onclick=\"exportTableToCSV('toble_to_csv.csv')\"> \u4e0b\u8f09CSV <\/button> \n\n<table class=\"table\" id=\"list_tb\" border=\"1\" style=\"border:1px solid #ddd;\">\n\t<thead>\n\t\t<tr>\n\t\t<th style=\"width: 50px;text-align: center;\">#<\/th>\n\t\t<th colspan=\"2\" style=\"text-align: center;\">\u72c0\u6cc1<\/th>\n\t\t<th style=\"text-align: center;\">\u9ede\u6578<\/th>\n\t\t<\/tr>\n\t<\/thead><thead>\n\t<\/thead><tbody>\n\t\t<tr>\n\t\t<td style=\"text-align: center;\">1<\/td>\n\t\t<td style=\"vertical-align: middle;\">\u4e0d\u5206\u79d1\u76ee\uff1a\u6708\u8003\/\u6bb5\u8003<\/td>\n\t\t<td>100\u5206<\/td>\t\n\t\t<td style=\"text-align: center;\">20<\/td>\t\t\t\t\n\t\t<\/tr>\n\t\t<tr>\n\t\t<td style=\"text-align: center;\">2<\/td>\n\t\t<td style=\"vertical-align: middle;\">\u4e0d\u5206\u79d1\u76ee\uff1a\u6708\u8003\/\u6bb5\u8003<\/td>\n\t\t<td>95-99\u5206<\/td>\t\n\t\t<td style=\"text-align: center;\">10<\/td>\t\t\t\t\n\t\t<\/tr>\n\t\t<tr>\n\t\t<td style=\"text-align: center;\">3<\/td>\n\t\t<td style=\"vertical-align: middle;\">\u4e0d\u5206\u79d1\u76ee\uff1a\u6708\u8003\/\u6bb5\u8003<\/td>\n\t\t<td>90-94\u5206<\/td>\t\n\t\t<td style=\"text-align: center;\">5<\/td>\n\t\t<\/tr>\n\t\t<tr>\n\t\t<td style=\"text-align: center;\">4<\/td>\n\t\t<td rowspan=\"3\" style=\"vertical-align: middle;\">\u96a8\u5802\u8003<\/td>\n\t\t<td>100\u5206<\/td>\t\n\t\t<td style=\"text-align: center;\">20<\/td>\t\t\t\t\n\t\t<\/tr>\n\t\t<tr>\n\t\t<td style=\"text-align: center;\">5<\/td>\n\t\t<td>90-99\u5206<\/td>\t\n\t\t<td style=\"text-align: center;\">10<\/td>\t\t\t\t\n\t\t<\/tr>\n\t\t<tr>\n\t\t<td style=\"text-align: center;\">6<\/td>\n\t\t<td>80-89\u5206<\/td>\t\n\t\t<td style=\"text-align: center;\">5<\/td>\n\t\t<\/tr>\n\t\t<tr>\n\t\t<td style=\"text-align: center;\">7<\/td>\n\t\t<td rowspan=\"3\" style=\"vertical-align: middle;\">\u80fd\u529b\u6e2c\u9a57<\/td>\n\t\t<td>PR\u503c95-100<\/td>\t\n\t\t<td style=\"text-align: center;\">30<\/td>\t\t\t\t\n\t\t<\/tr>\n\t\t<tr>\n\t\t<td style=\"text-align: center;\">8<\/td>\n\t\t<td>PR\u503c80-94<\/td>\t\n\t\t<td style=\"text-align: center;\">20<\/td>\t\t\t\t\n\t\t<\/tr>\n\t\t<tr>\n\t\t<td style=\"text-align: center;\">9<\/td>\n\t\t<td>PR\u503c60-79<\/td>\t\n\t\t<td style=\"text-align: center;\">10<\/td>\n\t\t<\/tr>\n\t\t<!--?\n\t\tif($u_type==\"\u4e3b\u4efb\" || $u_type==\"\u8001\u5e2b\" ){\n\t\t?-->\n\t\t<tr>\n\t\t<td style=\"text-align: center;\">10<\/td>\n\t\t<td colspan=\"2\">\u4e0a\u8ab2\u8209\u624b\u767c\u8a00\/\u56de\u7b54\u6b63\u78ba<\/td>\t\n\t\t<td style=\"text-align: center;\">5-10<\/td>\t\t\t\t\n\t\t<\/tr>\n\t\t<tr>\n\t\t<td style=\"text-align: center;\">11<\/td>\n\t\t<td colspan=\"2\">\u4f9d\u5b78\u751f\u4e0a\u8ab2\u72c0\u6cc1\uff0c\u4e00\u6b21\u4e0d\u5f97\u8d85\u904e20\u9ede\u3002<\/td>\t\n\t\t<td style=\"text-align: center;\">0-20<\/td>\n\t\t<\/tr>\n\t\t<!--?\n\t\t}\n\t\t?-->\n\t<\/tbody>\n<\/table>\n\n<script>  \n\/\/user-defined function to download CSV file  \nfunction downloadCSV(csv, filename) {  \n    var csvFile;  \n    var downloadLink;  \n     \n    \/\/define the file type to text\/csv  \n    csvFile = new Blob([csv], {type: 'text\/csv'});  \n    downloadLink = document.createElement(\"a\");  \n\t\n\tNumber.prototype.padLeft = function(base,chr){\n\t\tvar  len = (String(base || 10).length - String(this).length)+1;\n\t\treturn len > 0? new Array(len).join(chr || '0')+this : this;\n\t}\n\tvar d = new Date,\n    dformat = [d.getFullYear(),\n\t\t\t\t\t(d.getMonth()+1).padLeft(),\n\t\t\t\t\td.getDate().padLeft()].join('-')+' ' +\n\t\t\t\t\t[d.getHours().padLeft(),\n\t\t\t\t\td.getMinutes().padLeft(),\n\t\t\t\t\td.getSeconds().padLeft()].join(':');\n\tconsole.log(dformat);\n\t\n    downloadLink.download =dformat+\":\"+filename;  \n    downloadLink.href = window.URL.createObjectURL(csvFile);  \n    downloadLink.style.display = \"none\";  \n  \n    document.body.appendChild(downloadLink);  \n    downloadLink.click();  \n}  \n  \n\/\/user-defined function to export the data to CSV file format  \nfunction exportTableToCSV(filename) {  \n   \/\/declare a JavaScript variable of array type  \n   var csv = [];  \n   var rows = document.querySelectorAll(\"#list_tb tr\");\n      \n   \/\/merge the whole data in tabular form   \n   for(var i=0; i<rows.length; i++) {  \n    var row = [], cols = rows[i].querySelectorAll(\"td, th\");  \n    for( var j=0; j<cols.length; j++)  \n       row.push(cols[j].innerText);  \n    csv.push(row.join(\",\"));  \n   }   \n   \/\/call the function to download the CSV file  \n   downloadCSV(csv.join(\"\\n\"), filename);  \n}  \n<\/script>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u5099\u8a3b\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8981\u8a2d\u5b9aid<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;table class=\"table\" id='list_tb' border=\"1\" style=\"border:1px solid #ddd;\"&gt;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>var rows = document.querySelectorAll(\"#list_tb tr\");<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5408\u4f753\u5217 rowspan=\"3\"<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5408\u4f752\u6b04 colspan=\"2\"<\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-global-header-display":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","footnotes":""},"categories":[11],"tags":[],"class_list":["post-318","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/tomstudio.my.to\/index.php?rest_route=\/wp\/v2\/posts\/318","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tomstudio.my.to\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tomstudio.my.to\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tomstudio.my.to\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tomstudio.my.to\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=318"}],"version-history":[{"count":9,"href":"https:\/\/tomstudio.my.to\/index.php?rest_route=\/wp\/v2\/posts\/318\/revisions"}],"predecessor-version":[{"id":345,"href":"https:\/\/tomstudio.my.to\/index.php?rest_route=\/wp\/v2\/posts\/318\/revisions\/345"}],"wp:attachment":[{"href":"https:\/\/tomstudio.my.to\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tomstudio.my.to\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tomstudio.my.to\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}