Javascript 如何使用javascript将HTML用户输入数据存储到excel中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31564756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:49:57  来源:igfitidea点击:

How to store HTML user input data into excel using javascript

javascripthtmlexcel

提问by Vitthal Gaytonde

I need to create HTML page with JavaScript which can store user information into excel directly. Information can contain user input fields like:

我需要使用 JavaScript 创建 HTML 页面,该页面可以将用户信息直接存储到 excel 中。信息可以包含用户输入字段,例如:

  1. First Name (Datatype: Text)
  2. Last Name (Data Type: Text)
  1. 名字(数据类型:文本)
  2. 姓氏(数据类型:文本)

I have tried below code for storing values into CSV but I don't know how to store value in excel sheet.

我已经尝试使用下面的代码将值存储到 CSV 中,但我不知道如何在 Excel 表中存储值。

<hrml>
  <head>
     <script language="javascript">
        function WriteToFile(passForm) {
        var fso = new ActiveXObject("Scripting.FileSystemObject");
        var fileLoc = "D:\sample.csv";
        var file  = fso.OpenTextFile(fileLoc, 8, true,0);
        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">
    Type your last name: <input type="text" name="LastName" size="20">
    <input type="button" value="submit" onclick="WriteToFile(this.form)">
 </form>
</body>
</html>

Kindly help me for this "How to write HTML input data into Excel using JavaScript"

请帮我解决这个“如何使用 JavaScript 将 HTML 输入数据写入 Excel”

Thank you so much in advance.

非常感谢你。

回答by Ernesto Mario

you can create an automation object (in windows) using ActiveXObject() in your javascript code. example:

您可以在 JavaScript 代码中使用 ActiveXObject() 创建自动化对象(在 Windows 中)。例子:

var ExcelApp = new ActiveXObject("Excel.Application");
var ExcelSheet = new ActiveXObject("Excel.Sheet");
// a text is stored in the first cell  
ExcelSheet.ActiveSheet.Cells(1,1).Value = "Texto1";
// the sheet is saved
ExcelSheet.SaveAs("D:\TEST.XLS");
// close Excel with the Quit() method of the Application object 
ExcelSheet.Application.Quit();

回答by Ricardo Pontual

You can generate a semicolon separated var with your data and use the window.open function, here is an example:

你可以用你的数据生成一个分号分隔的 var 并使用 window.open 函数,这是一个例子:

var data = '';
data += $('#Name').val() + ';'
data += $('#EmployeeID').val();
data += '\r\n';
window.open( "data:application/vnd.ms-excel;charset=utf-8," + encodeURIComponent(data));

You can also generate a formatted html instead a semicolon separated text.

您还可以生成格式化的 html 而不是分号分隔的文本。

回答by Yavor Ivanov

This one uses ActiveX object to write to files on your PC. It will work only on IE. This is forbidden in browsers by default.

这个使用 ActiveX 对象来写入您 PC 上的文件。它仅适用于 IE。默认情况下,这在浏览器中是禁止的。

Here's the "normal" use case and how you would be able to complete this with all browsers:

这是“正常”用例以及如何使用所有浏览器完成此操作:

  1. Get form's data (JavaScript)
  2. Make an AJAX request to the server with the data (JavaScript)
  3. The server should have some logic to get the data and write it to a file (Excel file) (JavaScript, PHP, Java, C#, 1000s more....)
  1. 获取表单数据 (JavaScript)
  2. 使用数据 (JavaScript) 向服务器发出 AJAX 请求
  3. 服务器应该有一些逻辑来获取数据并将其写入文件(Excel 文件)(JavaScript、PHP、Java、C#、1000 多个......)

If you want it to work everywhere, you should put some efforts to create some server too. You may start the server on your local machine so you'll have files saved locally

如果你想让它在任何地方都能工作,你也应该努力创建一些服务器。您可以在本地计算机上启动服务器,以便将文件保存在本地