admin管理员组文章数量:1394051
I need to create HTML page with JavaScript which can store user information into excel directly. Information can contain user input fields like:
- First Name (Datatype: Text)
- Last Name (Data Type: Text)
I have tried below code for storing values into CSV but I don't know how to store value in excel sheet.
<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"
Thank you so much in advance.
I need to create HTML page with JavaScript which can store user information into excel directly. Information can contain user input fields like:
- First Name (Datatype: Text)
- Last Name (Data Type: Text)
I have tried below code for storing values into CSV but I don't know how to store value in excel sheet.
<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"
Thank you so much in advance.
Share Improve this question edited Jul 24, 2015 at 13:54 michelem 14.6k5 gold badges55 silver badges67 bronze badges asked Jul 22, 2015 at 13:35 Vitthal GaytondeVitthal Gaytonde 111 gold badge2 silver badges6 bronze badges 2- Hi, I have updated my post actually I have tried store HTML value into CSV but I could not able to understand how to store value into Excel sheet. Please help me for the same. Thank you so much in advance. – Vitthal Gaytonde Commented Jul 22, 2015 at 13:45
- This is going to be your best bet: datatables/extensions/tabletools – Bryan Mudge Commented Jul 22, 2015 at 13:46
4 Answers
Reset to default 1You can generate a semicolon separated var with your data and use the window.open function, here is an example:
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.
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.
Here's the "normal" use case and how you would be able to plete this with all browsers:
- Get form's data (JavaScript)
- Make an AJAX request to the server with the data (JavaScript)
- The server should have some logic to get the data and write it to a file (Excel file) (JavaScript, PHP, Java, C#, 1000s more....)
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
you can create an automation object (in windows) using ActiveXObject() in your javascript code. example:
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();
//This code appends the names of each field along with the values of
//each form element
// you can create a form with an export button at the end and
//call this function onclick
function exportToExcel() {
// Get the form data
var formData = document.forms["myForm"].elements;
var csvData = [];
// Get the field names
var fieldNames = [];
for (var i = 0; i < formData.length; i++) {
fieldNames.push(formData[i].name);
}
// Add the field names to the CSV data
csvData.push('"' + fieldNames.join('","') + '"');
// Loop through the form fields and build the CSV data
var row = [];
for (var i = 0; i < formData.length; i++) {
var fieldValue = formData[i].value;
fieldValue = fieldValue.replace(/"/g, '""');
row.push('"' + fieldValue + '"');
}
// Add the row to the CSV data
csvData.push(row.join(","));
// Convert the CSV data to a string and encode it for download
var csvString = csvData.join("\n");
var encodedUri = encodeURI("data:text/csv;charset=utf-8," + csvString);
// Create a temporary link element and click it to trigger the download
var link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", "form-data.csv");
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
本文标签: How to store HTML user input data into excel using javascriptStack Overflow
版权声明:本文标题:How to store HTML user input data into excel using javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744085894a2588512.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论