Click here to Skip to main content
14,868,583 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have two html table and im exporting that data into the excel sheet i have tried following code it works fine in chrome but not working in Internet explorer showing error as Result for "data" you search for data had no result
please suggest solution as user is using IE only
Thanks in advance

What I have tried:

<script type="text/javascript">

       var tablesToExcel = (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>'
   , templateend = '</x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head>'
   , body = '<body>'
   , tablevar = '<table>{table'
   , tablevarend = '}</table>'
   , bodyend = '</body></html>'
   , worksheet = '<x:ExcelWorksheet><x:Name>'
   , worksheetend = '</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet>'
   , worksheetvar = '{worksheet'
   , worksheetvarend = '}'
   , base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))) }
   , format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }) }
   , wstemplate = ''
   , tabletemplate = '';

             return function (table, name, filename) {
                 var tables = table;

                 for (var i = 0; i < tables.length; ++i) {
                     wstemplate += worksheet + worksheetvar + i + worksheetvarend + worksheetend;
                     tabletemplate += tablevar + i + tablevarend;
                 }

                 var allTemplate = template + wstemplate + templateend;
                 var allWorksheet = body + tabletemplate + bodyend;
                 var allOfIt = allTemplate + allWorksheet;

                 var ctx = {};
                 for (var j = 0; j < tables.length; ++j) {
                     ctx['worksheet' + j] = name[j];
                 }

                 for (var k = 0; k < tables.length; ++k) {
                     var exceltable;
                     if (!tables[k].nodeType) exceltable = document.getElementById(tables[k]);
                     ctx['table' + k] = exceltable.innerHTML;
                 }

<input type="button" onclick="tablesToExcel(['1', '2'], ['first', 'second'], 'myfile.xls')" value="Export to Excel" class="btn btn-primary btn-md">
Posted
Updated 8-Feb-17 19:24pm
v2
Comments
Karthik_Mahalingam 9-Feb-17 1:08am
   
try this
http://sheetjs.com/demos/table.html
Karthik_Mahalingam 9-Feb-17 1:14am
   
ok

1 solution

HTML
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  
    <script type="text/javascript" src="http://oss.sheetjs.com/js-xlsx/xlsx.core.min.js" ></script>
    <script type="text/javascript" src="http://oss.sheetjs.com/js-xlsx/ods.min.js"></script>
    <script type="text/javascript" src="http://sheetjs.com/demos/Blob.js"></script>
    <script type="text/javascript" src="http://sheetjs.com/demos/FileSaver.js"></script>
    <script type="text/javascript" src="http://sheetjs.com/demos/Export2Excel.js"></script>
    <script>
        function download() { export_table_to_excel('myTable', 'xlsx' || 'xlsx'); }
    </script> 
    </head>
<body>
    <button onclick="download();">Export to Excel</button>
  
    <table id="myTable" border="1">
        <tr style="background-color:gray">
            <td align="center">Company</td>
            <td align="center">Contact</td>
            <td align="center">Country</td>
        </tr>
        <tr>
            <td>Alfreds Futterkiste</td>
            <td>Maria Anders</td>
            <td>Germany</td>
        </tr>
        <tr>
            <td>Centro comercial Moctezuma</td>
            <td>Francisco Chang</td>
            <td>Mexico</td>
        </tr>
        <tr>
            <td>Ernst Handel</td>
            <td>Roland Mendel</td>
            <td>Austria</td>
        </tr>
        <tr>
            <td>Island Trading</td>
            <td>Helen Bennett</td>
            <td>UK</td>
        </tr>
        
    </table>
     
  

</body>
</html>


table data from w3schools[^]
   
v2
Comments
Member 12966735 9-Feb-17 7:36am
   
It is working fine but i have 2 tables how can i do thiswith two tables
Karthik_Mahalingam 9-Feb-17 11:00am
   
call the function twice with the table id

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900