如何将用户输入的值写入html页面中excel文件的csv文件中 [英] how to write the user inputted values into the csv file of excel file in html page

查看:191
本文介绍了如何将用户输入的值写入html页面中excel文件的csv文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将html中文本框的值导出到csv或excel

。我创建了一个HTML表单,它将从本地计算机上使用并希望表单数据到保存在CSV文件中。



每次提交表单时,都应在CSV文件中添加一行。



这需要在本地运行,所以不能使用PHP或JSP。

解决方案

希望这可以帮助你 -



 <   html  >  
< head >
< script < span class =code-attribute> language = JavaScript的 < span class =code-keyword>>
function WriteToFile(passForm){
var fso = new ActiveXObject( Scripting.FileSystemObject的);
var fileLoc = D:\ \sample.csv;
var file = fso.CreateTextFile(fileLoc, true );
file.writeline(passForm.FirstName.value + ' ,' +
passForm.LastName.value);
file.Close();
alert(' 在位置成功创建文件:' + fileLoc);
}
< / 脚本 >
< / head >
< body >
< p > 创建一个csv文件,其中包含以下详细信息 - < < span class =code-leadattribute> / p
>
< 表格 >
输入您的名字:< 输入 type
= text 名称 = FirstName size = 20 > < br >
输入您的姓氏: < 输入 type = text name = LastName size = 20 > < br >
< input type = 按钮 value = 提交 onclick = WriteToFile(this.form) >
< / m >
< / body >
< / html >
< / br > < / br > < / html >





此外,请参阅此链接使用Javascript在客户端进行文件处理 [ ^ ]供进一步参考。



问候,

Niral Soni


< blockquote>只是因为你不喜欢WoodenLegNamedSmith给出的答案并没有阻止它是正确的。

将问题倾向于尝试更多地回答你的喜好并不会改变计算机的工作方式!



所以要重新回答答案:Javascript无法访问客户端PC上的硬盘驱动器,也无法在服务器上运行代码。安全注意事项禁止它。



您不能直接编写Excel文件或CSV文件(或文本文件,PDF文件或任何其他类型的文件)到PC - 您所能做的就是将其写入服务器并提供下载并保存到客户端 - 即使这样您也无法控制客户端存储数据的位置,甚至无法控制下载数据。



正如WoodenLegNamedSmith所说,它可以通过ActiveX控件完成,但是现在在任何PC上安装其中一个的可能性很小。

您还可以创建一个具有无限期到期的cookie,并将其用作伪数据库以生成CSV格式的文本块,然后可以使用该文本生成文本文件。如果可以保证持久性,则可以每次重新加载cookie,或者在本地打开网页时解析保存的CSV。这应该有助于您入门:



代码取自: Javascript:创建并保存文件 - Stack Overflow [ ^ ];积分:Awesomeness01



<前lang =Javascript> 功能下载(文字,名称,类型) ){
var a = document .getElementById( a);
var file = new Blob([text],{type:type});
a.href = URL.createObjectURL(file);
a.download = name;
}
< a href = id = a>点击此处下载文件< / a >
< button onclick = download('file text','myfilename.txt','text / plain')>创建文件< / 按钮 >


How can i export the values of the text box in html to a csv or a excel
.I have created an HTML Form which will be used from a local computer and want the form data to be saved in CSV file.

Each time the form is submitted, it should add a line in the CSV file.

This needs to be run locally so cannot use PHP or JSP.

解决方案

Hope this may help you out -

<html>
  <head>
  <script language="javascript">
    function WriteToFile(passForm) {
      var fso = new ActiveXObject("Scripting.FileSystemObject");
      var fileLoc = "D:\\sample.csv";
      var file = fso.CreateTextFile(fileLoc, true);
      file.writeline(passForm.FirstName.value + ',' +
                     passForm.LastName.value);
      file.Close();
      alert('File created successfully at location: ' + fileLoc);
     }
  </script>
  </head>
  <body>
  <p>create a csv file with following details -</p>
  <form>
    Type your first name: <input type="text" name="FirstName" size="20"><br>
    Type your last name: <input type="text" name="LastName" size="20"><br>
    <input type="button" value="submit" onclick="WriteToFile(this.form)">
  </form>
  </body>
</html>
</br></br></html>



Also, refer to this link File Handling at Client Side Using Javascript[^] for further reference.

Regards,
Niral Soni


Just because you don't like the answer WoodenLegNamedSmith gave doesn't stop it being right.
Bumping the question to try and get a answer more to you liking won't change how computers work!

So to re-iterate the answer: Javascript does not have access to the hard-drive on the client PC, nor does the code behind which runs on the server. Security considerations forbid it.

You cannot write a Excel file or a CSV file (or a text file, or a PDF file, or any other type of file) directly to the PC - all you can do is write it to the server and offer it for download and saving to the client - even then you have no control whatsoever over where the client stores the data, or even that he downloads it.

As WoodenLegNamedSmith says, it could be done with an ActiveX control, but the chances of getting one of those installed on any PC these days is remote.


You could also create a cookie with an indefinite expiry, and use that as a pseudo-database to generate a block of text in CSV format that could then be used to generate a text file. You could then either reload the cookie every time if you can guarantee persistence, or parse the saved CSV when you open the web page locally. This should help get you started:

Code taken from: Javascript: Create and save file - Stack Overflow[^]; credit to: Awesomeness01

function download(text, name, type) {
  var a = document.getElementById("a");
  var file = new Blob([text], {type: type});
  a.href = URL.createObjectURL(file);
  a.download = name;
}
<a href="" id="a">click here to download your file</a>
<button onclick="download('file text', 'myfilename.txt', 'text/plain')">Create file</button>


这篇关于如何将用户输入的值写入html页面中excel文件的csv文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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