Question: Using JavaScript and taking the values from a form, how can I populate those values into the cells of a Microsoft Excel Spreadsheet? Do I need a add-in into my editor? Do I write to e specific file extension? Posting something on here to get some feedback or some point of direction. #javascript #webdevelopment #bootstrap
-
1"Using JavaScript" is pretty vague. In what environment are you using js? Server side or client side and in what host application? – Tim Williams Jan 07 '20 at 23:32
2 Answers
What you could do is convert your data to CSV which XLS can open .
Even though there are a few libraries that can do that. You can research that via google but Here is how you do it direct from JavaScript
First thing you would need to do is extract the data into an Array using
var formElement = document.querySelector("form");
var formData = new FormData(formElement);
formData.get("fieldName");
then use this code to create your csv
var data = [
['Foo', 'programmer'],
['Bar', 'bus driver'],
['Moo', 'Reindeer Hunter']
];
function download_csv() {
var csv = 'Name,Title\n';
data.forEach(function(row) {
csv += row.join(',');
csv += "\n";
});
console.log(csv);
var hiddenElement = document.createElement('a');
hiddenElement.href = 'data:text/csv;charset=utf-8,' + encodeURI(csv);
hiddenElement.target = '_blank';
hiddenElement.download = 'people.csv';
hiddenElement.click();
}
<button onclick="download_csv()">Download CSV</button>

- 1,117
- 8
- 22
-
@JonoJones any videos or sites you can point me to please? Or just some key terms I can search up. Thank you. – alexbrito1993 Jan 07 '20 at 23:42
-
Ok what you looking for is CSV from JS and look at mozillas link on form Data .. https://developer.mozilla.org/en-US/docs/Web/API/FormData/Using_FormData_Objects – JonoJames Jan 07 '20 at 23:48
I am submitting this answer under the assumption that by 'JavaScript' you are referring to a server-side implementation such as Node JS.
To achieve what you desire requires two steps -
- Submit and process the form data;
- Populate and export an xls file.
Create an HTML form as you would usually, submit it to a Node endpoint, fetch the POST data from the form using the Express middleware as described here and then create, populate and save a spreadsheet using Node-xlsx module.
You can download the Node-xlsx package using NPM from here.
If, perhaps, you were referring to using Javascript interpreted by the browser (client-side), then please refer to an existing discussion on Stack Overflow.

- 23
- 6