javascript - Format hyperlinks when exporting HTML table to Excel -
i'm downloading html table contents excel using javascript mentioned in export html table excel downloading table contents excel.
in table there links in 1 column. when download, column content shows hyperlinks in excel sheet. want export table excel without links (just plain text).
is there way that?
you have replace hyperlinks text.
clone table exported
table = $('#'+tablename).clone();
replace anchor tag corresponding span
var sp1 = document.createelement("span"); var sp1_content = document.createtextnode($(hyperlinks[i]).text()); sp1.appendchild(sp1_content); var sp2 = hyperlinks[i]; var parentdiv = sp2.parentnode; parentdiv.replacechild(sp1, sp2);
you can find here details replacing node.
exported using code stackoverflow answer
here snippet or pen
var tmp; function strip(html) { tmp = document.createelement("div"); tmp.innerhtml = html; console.log(tmp.innertext); console.log(tmp.textcontent); return tmp.textcontent || tmp.innertext || ""; } var tabletoexcel = (function() { var uri = 'data:application/vnd.ms-excel;base64,', template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/tr/rec-html40"><head><!--[if gte mso 9]><xml><x:excelworkbook><x:excelworksheets><x:excelworksheet><x:name>{worksheet}</x:name><x:worksheetoptions><x:displaygridlines/></x:worksheetoptions></x:excelworksheet></x:excelworksheets></x:excelworkbook></xml><![endif]--></head><body><table>{table}</table></body></html>', base64 = function(s) { return window.btoa(unescape(encodeuricomponent(s))) }, format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) } return function(table, name, filename) { if (!table.nodetype) /*clone table exported*/ table = $('#'+table).clone(); var hyperlinks = table.find('a'); (i = 0; < hyperlinks.length; i++) { //hyperlinks[i] = $(hyperlinks[i]).text(); var sp1 = document.createelement("span"); // give id attribute called 'newspan' sp1.setattribute("id", "newspan"); // create content new element. var sp1_content = document.createtextnode($(hyperlinks[i]).text()); console.log(sp1_content); // apply content new element sp1.appendchild(sp1_content); // build reference existing node replaced var sp2 = hyperlinks[i]; var parentdiv = sp2.parentnode; // replace existing node sp2 new span element sp1 parentdiv.replacechild(sp1, sp2); } var ctx = { worksheet: name || 'worksheet', table: table[0].innerhtml } document.getelementbyid("dlink").href = uri + base64(format(template, ctx)); document.getelementbyid("dlink").download = filename; document.getelementbyid("dlink").click(); } })()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a id="dlink" style="display:none;"></a> <table name="tablename" id="tablename" border="1"> <tr> <td>1</td> <td><a href="http://google.com">google</a></td> </tr><tr> <td>2</td> <td><a href="http://microsoft.com">microsoft</a></td> </tr><tr> <td>3</td> <td><a href="http://amazon.com">amazon</a></td> </tr> </table> <input type="button" onclick="tabletoexcel('tablename', 'name', 'myfile.xls')" value="export excel">
Comments
Post a Comment