javascript - 如何通过单击表单的 "submit"按钮将数据从 HTML 保存到 Excel?

标签 javascript html excel

<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/

相关文章:

javascript - 获取 tr 元素的正确顺序作为 jQuery 对象

javascript - 列出 js 多重过滤器全部

html - Img 源是一个变量,URL 被编码

arrays - 为什么 VBA 类不能通过 ByRef Array()?

vba - Excel 中的 MsgBox 焦点

javascript - 同时将事件绑定(bind)到多个 jquery 元素

javascript - 合并两个深度数组

javascript - HTML5 拖放在目标中创建原始副本?

html - 对超出窗口右侧的元素应用填充

excel - 使用 "ADODB.Stream"将ANSI转为UTF-8,第一行漏掉1-2个字符