Reputation: 567
I am developing a page that exports a HTML table to excel using a JQuery plugin. (https://github.com/rainabba/jquery-table2excel).
For some reason when the button is clicked absolutely nothing happens. I have identified my table, and called my function so I have no idea why this code is not working.
View~ (condensed)
<div class="row">
<div class="col-md-2">
<a onclick="return ResultsToTable()" class="btn btn-default">Export</a>
</div>
<div class=col-md-10></div>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"><script>
<script src="~/js/jquery.table2excel.js"></script>
<script type="text/javascript">
function ResultsToTable(){
$("#resultsTable").table2excel({
exclude: ".noExl",
name: "Results"
});
};
</script>
<table id="resultsTable">
//table stuff
</table>
Upvotes: 2
Views: 12434
Reputation: 1
You can use this code snippet to export Excel file.
checkout this https://jsfiddle.net/santoshgawande/2fcsq7o6/1/
It is working fine.
var Results = [
["Col1", "Col2", "Col3", "Col4"],
["Data", 50, 100, 500],
["Data", -100, 20, 100],
];
exportToCsv = function() {
var CsvString = "";
Results.forEach(function(RowItem, RowIndex) {
RowItem.forEach(function(ColItem, ColIndex) {
CsvString += ColItem + ',';
});
CsvString += "\r\n";
});
CsvString = "data:application/vnd.ms-excel," + encodeURIComponent(CsvString);
var x = document.createElement("A");
x.setAttribute("href", CsvString );
x.setAttribute("download","Data.xlsx");
document.body.appendChild(x);
x.click();
}
<button onclick="exportToCsv()">export to CSV</button>
Upvotes: 0
Reputation: 7666
If I'm not mistaken jquery.table2excel.js is not working properly ATM. I tried running the demo from the repo and it didn't work for me.
I experimented a bit and found a solution:
https://github.com/rainabba/jquery-table2excel/pull/13
Check this JSFiddle
(note that used is the fixed version, see the script includes at the top of HTML)
Just for compliance, JS code:
jQuery(document).ready(function() {
$('#export-btn').on('click', function(e){
e.preventDefault();
ResultsToTable();
});
function ResultsToTable(){
$("#resultsTable").table2excel({
exclude: ".noExl",
name: "Results"
});
}
});
Upvotes: 0
Reputation: 6684
Correct this:
<script src="~/js/jquery.table2excel.js"></script
>
It looks like you do not load the library properly. I guess it should be:
<script src="/js/jquery.table2excel.js"></script>
But adapt it to the right structure of your folders
Upvotes: 0