吐血推薦:網上搜了好久關於webgis地圖屬性表輸出到Excel表,並沒能找到相關有價值的信息,在小白麪前,這就是一臉懵x啊!網上要麼是關於前端如何在頁面上直接導出excel,和webgis半毛錢關係沒有,要麼查看arcgis api for javascript 的幫助文檔,裏面有的實例是顯示屬性表信息,如何同時顯示並能輸出excel表的文章介紹,沒找到。。那隻能經過看代碼,進行改進來實現上面的兩種功能,終於終於。。。。找到了他們之間的聯繫,實現出來了!!javascript
一、首先我已經發布過地圖並找到URL,瞭解直接要發佈地圖的屬性表信息css
二、我實現的代碼功能是經過查詢一個區域的名稱,找到該區域的經緯值,並輸出到excel中:html
三、將相關信息查詢出來,顯示頁面中,實現的結果以下:前端
四、最關鍵一步,將這些信息輸出到excel中去!點擊按鍵,如圖java
五、代碼部分:web
<!DOCTYPE html> <html> <head> <title>圖層屬性查詢</title> <meta http-equiv="content-type" content="text/html;charset=utf-8"> <meta http-equiv="Access-Control-Allow-Origin" content="*"> <link rel="stylesheet" href="https://js.arcgis.com/3.23/esri/css/esri.css"> <script src="https://js.arcgis.com/3.23/"></script> <style> #map{ position:relative; height:500px; width:100%; } </style> </head> <body> <div> 輸入省份或省會名稱: <input type="text" id="searchText" size="40" value="烏魯木齊" /> <input type="button" value="查詢" id="find"/> <div style="width:200px;margin:auto;text-align:center;"> <button onclick="ok()">導出查詢地區經緯度excel文件</button> </div> <div id="tbl"> </div> </div> <div id='map'> </div> <script> require([ "esri/map", "esri/tasks/FindTask", "esri/tasks/FindParameters", "dojo/_base/array", "esri/layers/ArcGISDynamicMapServiceLayer"], function ( Map, FindTask, FindParameters, Array, ArcGISDynamicMapServiceLayer) { var map = new Map("map", { center: [116.403119,39.915599], zoom:3, basemap: "topo" }); //調用地圖服務 DyLayer=new ArcGISDynamicMapServiceLayer('http://localhost:6080/arcgis/rest/services/dtchina/MapServer'); map.addLayer(DyLayer); var find = new FindTask("http://localhost:6080/arcgis/rest/services/dtchina/MapServer"); var params = new FindParameters(); // layerIds、seachFields、searchText params.layerIds = [1]; //對layerid=0和1的圖層查詢 params.searchFields = ["FID", "name","id","lon","lat"];//查該圖層的哪些字段 document.getElementById("find").onclick = doFind; function doFind() { params.searchText = document.getElementById("searchText").value; find.execute(params, showResults); //執行查詢 } function showResults(results) { console.log("results", results); let result, attribs; let s = ["<table border=\"2\"> " + "<thead>" + "<tr style=\" background-color:#98a9cc;\">" + "<td>Name</td> <td>FID</td> <td>ID</td> <td>lon</td> <td>lat</td> </tr> " + "</thead> " + "<tbody id=\"state\">"]; Array.forEach(results, function (result) { attribs = result.feature.attributes; s.push("<tr> <td>" + attribs.name + "</td> <td>" + attribs.FID + "</td> <td>" +attribs.id+ "</td> <td>" + attribs.lon + "</td> <td>" + attribs.lat + "</td> </tr>" ); }); s.push("</tbody> </table>"); console.log('arr',s); document.getElementById("tbl").innerHTML = s.join("");//string.split(''); } }); var ok = (function () { var uri = 'data:application/vnd.ms-excel;base64,', template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" ' + 'xmlns="https://www.w3.org/TR/2018/SPSD-html401-20180327/">' + '<head><!--[if gte mso 9]><xml><x:ExcelWorkbook>' + '<x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name>' + '<x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet>' + '</x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>', base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))) }, format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }) } return function (table, name) { var tables = document.getElementById('state'); var ctx = { worksheet: name || 'Worksheet', table: tables.innerHTML } window.location.href = uri + base64(format(template, ctx)); } })(); function ExportSupplierMonthlyData() { try { tableToExcel(); } catch (err) { bootbox.alert('沒有數據,導出失敗'); } } </script> </body> </html>