<form>
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br>
<input type="submit" value="Submit">
</form>
我希望单击“提交”时它应该将名字和姓氏保存在 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>
关于javascript - 如何通过单击表单的 "submit"按钮将数据从 HTML 保存到 Excel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30346942/