簡體   English   中英

單擊表單的“提交”按鈕,如何將數據從HTML保存到Excel?

[英]How to save data from HTML to excel, on click on “submit” button of form?

<form>
  First name: <input type="text" name="fname"><br>
  Last name: <input type="text" name="lname"><br>
  <input type="submit" value="Submit">
</form>

我想onclick“提交”它應該在Excel工作表中保存名字和姓氏。 我不想使用數據庫,也沒有任何服務器。 我讀過某個地方,我們可以用CSV做同樣的事情。 幫幫我。

提前致謝。

一種可能的方法是創建一個html表。 您可以將表單值設置為不可見的表,並通過鏈接模擬html表的下載。 Excel讀取html並顯示數據。


范例:

 function exportF() { //Format your table with form data document.getElementById("input").innerHTML = document.getElementById("text").value; var table = document.getElementById("table"); var html = table.outerHTML; var url = 'data:application/vnd.ms-excel,' + escape(html); // Set your html table into url var link = document.getElementById("downloadLink"); link.setAttribute("href", url); link.setAttribute("download", "export.xls"); // Choose the file name link.click(); // Download your excel file return false; } 
 <form onsubmit="return exportF()"> <input id="text" type="text" /> <input type="submit" /> </form> <table id="table" style="display: none"> <tr> <td id="input"> </td> </tr> </table> <a style="display: none" id="downloadLink"></a> 

有多種庫可以生成.xlsx文件,您需要選擇其中一個(Google xlsx.js ),在提交時讀出表單中的值,然后使用選擇的庫創建所需的電子表格。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM