Exporting data from HTML table via JS to xls, does not leave values as it is
I am exporting data using JavaScript, but it does not leave my values as decimal. For example 56.00 turns as just 56. I tried to use .toFixed(), but it does not help.
I have a lot of HTML and JS code, so i dont know what exact code part you would like to see. But maybe someone have solved the same problem?
Here is my JS export HTML - .xls code.
function Exc(elem){
var tables = document.querySelectorAll("#tableWrapper .col-lg-12");
var tab_text = "";
// Add variable with names of all tables
var fileName = "(";
if(tables.className != "hidden")
{
for(var i = 0; i < tables.length; i++)
{
tab_text += "<table border='3px'><tr bgcolor='#87AFC6'>";
// Get name, manufacturer & standard
var name = tables[i].querySelector("h2").innerHTML;
// Set table names as filename
if(i == tables.length - 1)
fileName += name + ")";
else
fileName += name + ", ";
var manufacturer = tables[i].querySelectorAll(".col-md-6")[1].querySelectorAll("li")[0].innerHTML;
var standard = tables[i].querySelectorAll(".col-md-6")[1].querySelectorAll("li")[1].innerHTML;
// Set name, manufacturer & standard
tab_text += "<div><h2>" + name + "</h2>";
tab_text += manufacturer == " " ? "" : "<li>" + manufacturer + "</li>";
tab_text += standard == "" ? "</div>" : "<li>" + standard + "</li></div>";
// Get current table
var table = tables[i].querySelector("table");
// Get all properties
var prop = table.querySelectorAll("thead .tsl-colgroup");
tab_text += "<th class="tsl-series"></th>";
for(var j = 0 ; j < prop.length ; j++)
{
tab_text += prop[j].outerHTML;
}
tab_text += "</tr>";
// Get all measures
var mes = table.querySelectorAll("tbody tr th");
tab_text += "<th class="tsl-series active" ><strong>Profile-Loremipsum</strong></th>"
for(var j = 1 ; j < mes.length ; j++)
{
tab_text += "<th class="tsl-cell" onclick="sortListByProperty(this)">";
//th[j].firstChild.textContent +(th[j].childElementCount == 2 ? th[j].children[0].outerHTML: "");
tab_text += mes[j].childElementCount == 2 ? mes[j].firstChild.textContent + mes[j].children[0].outerHTML + "<br>" : mes[j].firstChild.textContent + "<br>";
tab_text += mes[j].lastElementChild.outerHTML + "</th>";
}
tab_text += "</tr>";
// Get all demensions
var dem = table.querySelectorAll("tbody tr");
for(var j = 1 ; j < dem.length; j++)
{
if(dem[j].className != "hidden")
{
tab_text += "<tr>"
tab_text += "<td class="tsl-series active"><strong>" + dem[j].querySelectorAll("td strong")[0].innerHTML + "</strong>";
var cells = dem[j].querySelectorAll(".tsl-cell");
for(var p = 0; p < cells.length; p++)
{
var numberFloat = parseFloat(cells[p].querySelector("a").innerHTML);
tab_text += "<td></a>" + numberFloat.toFixed(2) + "</a></td>";
}
}
}
tab_text += "</table>";
}
}
var result = "data:application/vnd.ms-excel," + encodeURIComponent(tab_text);
elem.href = result;
elem.download = fileName + ".xls";
}
You can see below how values looks before export and after.
before export
after export in .xls
javascript export xls
add a comment |
I am exporting data using JavaScript, but it does not leave my values as decimal. For example 56.00 turns as just 56. I tried to use .toFixed(), but it does not help.
I have a lot of HTML and JS code, so i dont know what exact code part you would like to see. But maybe someone have solved the same problem?
Here is my JS export HTML - .xls code.
function Exc(elem){
var tables = document.querySelectorAll("#tableWrapper .col-lg-12");
var tab_text = "";
// Add variable with names of all tables
var fileName = "(";
if(tables.className != "hidden")
{
for(var i = 0; i < tables.length; i++)
{
tab_text += "<table border='3px'><tr bgcolor='#87AFC6'>";
// Get name, manufacturer & standard
var name = tables[i].querySelector("h2").innerHTML;
// Set table names as filename
if(i == tables.length - 1)
fileName += name + ")";
else
fileName += name + ", ";
var manufacturer = tables[i].querySelectorAll(".col-md-6")[1].querySelectorAll("li")[0].innerHTML;
var standard = tables[i].querySelectorAll(".col-md-6")[1].querySelectorAll("li")[1].innerHTML;
// Set name, manufacturer & standard
tab_text += "<div><h2>" + name + "</h2>";
tab_text += manufacturer == " " ? "" : "<li>" + manufacturer + "</li>";
tab_text += standard == "" ? "</div>" : "<li>" + standard + "</li></div>";
// Get current table
var table = tables[i].querySelector("table");
// Get all properties
var prop = table.querySelectorAll("thead .tsl-colgroup");
tab_text += "<th class="tsl-series"></th>";
for(var j = 0 ; j < prop.length ; j++)
{
tab_text += prop[j].outerHTML;
}
tab_text += "</tr>";
// Get all measures
var mes = table.querySelectorAll("tbody tr th");
tab_text += "<th class="tsl-series active" ><strong>Profile-Loremipsum</strong></th>"
for(var j = 1 ; j < mes.length ; j++)
{
tab_text += "<th class="tsl-cell" onclick="sortListByProperty(this)">";
//th[j].firstChild.textContent +(th[j].childElementCount == 2 ? th[j].children[0].outerHTML: "");
tab_text += mes[j].childElementCount == 2 ? mes[j].firstChild.textContent + mes[j].children[0].outerHTML + "<br>" : mes[j].firstChild.textContent + "<br>";
tab_text += mes[j].lastElementChild.outerHTML + "</th>";
}
tab_text += "</tr>";
// Get all demensions
var dem = table.querySelectorAll("tbody tr");
for(var j = 1 ; j < dem.length; j++)
{
if(dem[j].className != "hidden")
{
tab_text += "<tr>"
tab_text += "<td class="tsl-series active"><strong>" + dem[j].querySelectorAll("td strong")[0].innerHTML + "</strong>";
var cells = dem[j].querySelectorAll(".tsl-cell");
for(var p = 0; p < cells.length; p++)
{
var numberFloat = parseFloat(cells[p].querySelector("a").innerHTML);
tab_text += "<td></a>" + numberFloat.toFixed(2) + "</a></td>";
}
}
}
tab_text += "</table>";
}
}
var result = "data:application/vnd.ms-excel," + encodeURIComponent(tab_text);
elem.href = result;
elem.download = fileName + ".xls";
}
You can see below how values looks before export and after.
before export
after export in .xls
javascript export xls
Check this answer: stackoverflow.com/a/3399625/7128891
– Mike Kaskun
Dec 8 '18 at 0:20
add a comment |
I am exporting data using JavaScript, but it does not leave my values as decimal. For example 56.00 turns as just 56. I tried to use .toFixed(), but it does not help.
I have a lot of HTML and JS code, so i dont know what exact code part you would like to see. But maybe someone have solved the same problem?
Here is my JS export HTML - .xls code.
function Exc(elem){
var tables = document.querySelectorAll("#tableWrapper .col-lg-12");
var tab_text = "";
// Add variable with names of all tables
var fileName = "(";
if(tables.className != "hidden")
{
for(var i = 0; i < tables.length; i++)
{
tab_text += "<table border='3px'><tr bgcolor='#87AFC6'>";
// Get name, manufacturer & standard
var name = tables[i].querySelector("h2").innerHTML;
// Set table names as filename
if(i == tables.length - 1)
fileName += name + ")";
else
fileName += name + ", ";
var manufacturer = tables[i].querySelectorAll(".col-md-6")[1].querySelectorAll("li")[0].innerHTML;
var standard = tables[i].querySelectorAll(".col-md-6")[1].querySelectorAll("li")[1].innerHTML;
// Set name, manufacturer & standard
tab_text += "<div><h2>" + name + "</h2>";
tab_text += manufacturer == " " ? "" : "<li>" + manufacturer + "</li>";
tab_text += standard == "" ? "</div>" : "<li>" + standard + "</li></div>";
// Get current table
var table = tables[i].querySelector("table");
// Get all properties
var prop = table.querySelectorAll("thead .tsl-colgroup");
tab_text += "<th class="tsl-series"></th>";
for(var j = 0 ; j < prop.length ; j++)
{
tab_text += prop[j].outerHTML;
}
tab_text += "</tr>";
// Get all measures
var mes = table.querySelectorAll("tbody tr th");
tab_text += "<th class="tsl-series active" ><strong>Profile-Loremipsum</strong></th>"
for(var j = 1 ; j < mes.length ; j++)
{
tab_text += "<th class="tsl-cell" onclick="sortListByProperty(this)">";
//th[j].firstChild.textContent +(th[j].childElementCount == 2 ? th[j].children[0].outerHTML: "");
tab_text += mes[j].childElementCount == 2 ? mes[j].firstChild.textContent + mes[j].children[0].outerHTML + "<br>" : mes[j].firstChild.textContent + "<br>";
tab_text += mes[j].lastElementChild.outerHTML + "</th>";
}
tab_text += "</tr>";
// Get all demensions
var dem = table.querySelectorAll("tbody tr");
for(var j = 1 ; j < dem.length; j++)
{
if(dem[j].className != "hidden")
{
tab_text += "<tr>"
tab_text += "<td class="tsl-series active"><strong>" + dem[j].querySelectorAll("td strong")[0].innerHTML + "</strong>";
var cells = dem[j].querySelectorAll(".tsl-cell");
for(var p = 0; p < cells.length; p++)
{
var numberFloat = parseFloat(cells[p].querySelector("a").innerHTML);
tab_text += "<td></a>" + numberFloat.toFixed(2) + "</a></td>";
}
}
}
tab_text += "</table>";
}
}
var result = "data:application/vnd.ms-excel," + encodeURIComponent(tab_text);
elem.href = result;
elem.download = fileName + ".xls";
}
You can see below how values looks before export and after.
before export
after export in .xls
javascript export xls
I am exporting data using JavaScript, but it does not leave my values as decimal. For example 56.00 turns as just 56. I tried to use .toFixed(), but it does not help.
I have a lot of HTML and JS code, so i dont know what exact code part you would like to see. But maybe someone have solved the same problem?
Here is my JS export HTML - .xls code.
function Exc(elem){
var tables = document.querySelectorAll("#tableWrapper .col-lg-12");
var tab_text = "";
// Add variable with names of all tables
var fileName = "(";
if(tables.className != "hidden")
{
for(var i = 0; i < tables.length; i++)
{
tab_text += "<table border='3px'><tr bgcolor='#87AFC6'>";
// Get name, manufacturer & standard
var name = tables[i].querySelector("h2").innerHTML;
// Set table names as filename
if(i == tables.length - 1)
fileName += name + ")";
else
fileName += name + ", ";
var manufacturer = tables[i].querySelectorAll(".col-md-6")[1].querySelectorAll("li")[0].innerHTML;
var standard = tables[i].querySelectorAll(".col-md-6")[1].querySelectorAll("li")[1].innerHTML;
// Set name, manufacturer & standard
tab_text += "<div><h2>" + name + "</h2>";
tab_text += manufacturer == " " ? "" : "<li>" + manufacturer + "</li>";
tab_text += standard == "" ? "</div>" : "<li>" + standard + "</li></div>";
// Get current table
var table = tables[i].querySelector("table");
// Get all properties
var prop = table.querySelectorAll("thead .tsl-colgroup");
tab_text += "<th class="tsl-series"></th>";
for(var j = 0 ; j < prop.length ; j++)
{
tab_text += prop[j].outerHTML;
}
tab_text += "</tr>";
// Get all measures
var mes = table.querySelectorAll("tbody tr th");
tab_text += "<th class="tsl-series active" ><strong>Profile-Loremipsum</strong></th>"
for(var j = 1 ; j < mes.length ; j++)
{
tab_text += "<th class="tsl-cell" onclick="sortListByProperty(this)">";
//th[j].firstChild.textContent +(th[j].childElementCount == 2 ? th[j].children[0].outerHTML: "");
tab_text += mes[j].childElementCount == 2 ? mes[j].firstChild.textContent + mes[j].children[0].outerHTML + "<br>" : mes[j].firstChild.textContent + "<br>";
tab_text += mes[j].lastElementChild.outerHTML + "</th>";
}
tab_text += "</tr>";
// Get all demensions
var dem = table.querySelectorAll("tbody tr");
for(var j = 1 ; j < dem.length; j++)
{
if(dem[j].className != "hidden")
{
tab_text += "<tr>"
tab_text += "<td class="tsl-series active"><strong>" + dem[j].querySelectorAll("td strong")[0].innerHTML + "</strong>";
var cells = dem[j].querySelectorAll(".tsl-cell");
for(var p = 0; p < cells.length; p++)
{
var numberFloat = parseFloat(cells[p].querySelector("a").innerHTML);
tab_text += "<td></a>" + numberFloat.toFixed(2) + "</a></td>";
}
}
}
tab_text += "</table>";
}
}
var result = "data:application/vnd.ms-excel," + encodeURIComponent(tab_text);
elem.href = result;
elem.download = fileName + ".xls";
}
You can see below how values looks before export and after.
before export
after export in .xls
javascript export xls
javascript export xls
edited Dec 8 '18 at 0:19
Nick
30.2k121941
30.2k121941
asked Nov 26 '18 at 14:48
Mark GrindcoreMark Grindcore
62
62
Check this answer: stackoverflow.com/a/3399625/7128891
– Mike Kaskun
Dec 8 '18 at 0:20
add a comment |
Check this answer: stackoverflow.com/a/3399625/7128891
– Mike Kaskun
Dec 8 '18 at 0:20
Check this answer: stackoverflow.com/a/3399625/7128891
– Mike Kaskun
Dec 8 '18 at 0:20
Check this answer: stackoverflow.com/a/3399625/7128891
– Mike Kaskun
Dec 8 '18 at 0:20
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53483630%2fexporting-data-from-html-table-via-js-to-xls-does-not-leave-values-as-it-is%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53483630%2fexporting-data-from-html-table-via-js-to-xls-does-not-leave-values-as-it-is%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Check this answer: stackoverflow.com/a/3399625/7128891
– Mike Kaskun
Dec 8 '18 at 0:20