如何将数据从HTML保存为excel,点击“提交”形式按钮? [英] How to save data from HTML to excel, on click on "submit" button of form?

查看:443
本文介绍了如何将数据从HTML保存为excel,点击“提交”形式按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <形式> 
名字:< input type =textname =fname>< br>
姓氏:< input type =textname =lname>< br>
< input type =submitvalue =提交>
< / form>

我想点击提交它应该在Excel工作表中保存名字和姓氏。我不想使用数据库,我没有任何服务器。我在某处读到我们可以在CSV中做同样的事情。帮助我。



在此先感谢您。

解决方案

方法是创建一个html表格。您可以将表单值设置为不可见表格,并通过链接模拟html表格的下载。例如:


$ b

  function exportF(){//用表单数据格式化你的表document.getElementById(input)。innerHTML = document.getElementById 文本)值。 var table = document.getElementById(table); var html = table.outerHTML; var url ='data:application / vnd.ms-excel,'+ escape(html); //将你的html表设置为url var link = document.getElementById(downloadLink); link.setAttribute(href,url); link.setAttribute(download,export.xls); //选择文件名link.click(); //下载你的excel文件返回false;}  

< form onsubmit =return exportF()> < input id =texttype =text/> < input type =submit/>< / form>< table id =tablestyle =display:none> < TR> < td id =input> < / TD> < / tr>< / table>< a style =display:noneid =downloadLink>< / a>


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

I want onclick "submit" it should save first name and last name in Excel sheet. I don't want to use database and I don't have any server. I read somewhere that we can do the same in CSV. Help me out.

Thanks in advance.

解决方案

A possible way is to create an html table. You can set the form values into an invisible table and simulate the download of html table with a link. Excel reads the html and display the data.


Example :

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>

这篇关于如何将数据从HTML保存为excel,点击“提交”形式按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆