3

I'm trying to export HTML table content to excel. I saw this solution which worked but not as I expected it (because I can't choose which columns to copy, and it doesn't works with big tables).

And another solution to copy by js and manually paste to excel file, which didn't work as well, and I don't really fancy this method.

Shortly what I want is, export customized view of the table, not all columns. to show you an example of what I mean:

Here is the normal table view:

enter image description here

and Here is how what I want to show in excel:

enter image description here

But because I have hidden fields, the first method didn't work:

enter image description here

I would like a client side, cross-browser, workaround/solution, considering that I have around 2,500 lines in the table.

9
  • 1
    I think it would be better if you build the excel sheet in server side , so you could customize it properly. Commented Mar 4, 2014 at 11:39
  • will you use any framework like struts,spring etc Commented Mar 4, 2014 at 11:39
  • @KanhuBhol Actually, I'm stuck with Classical ASP!! Commented Mar 4, 2014 at 11:42
  • @MohamedFarrag, that would be my last resort! Commented Mar 4, 2014 at 11:44
  • 1
    If you changed your mind, so I recommend to use Office Open excelpackage.codeplex.com it is a great tool to build excel sheet from scratch. Commented Mar 4, 2014 at 11:51

2 Answers 2

1

I made something very similar and use it every day in my office following a tutorial found on the web. You can use this template for PHP:

<?
   $filename="sheet.xls";
   header ("Content-Type: application/vnd.ms-excel");
   header ("Content-Disposition: inline; filename=$filename");
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang=it><head>
<title>Titolo</title></head>
<body>
<table border="1">
<?
for ($i=1;$i < 11; $i++)
{
   echo "<tr>";
   for ($j=1; $j<11;$j++)
   {
      $a = $i * $j;
      echo "<td>$a</td>";
   }
   echo "</tr>";
}
?>
</table>
</body></html>

You can write something similar with JS, just generate a simple table with HTML tags and be sure to write the correct code in the header.

Sign up to request clarification or add additional context in comments.

Comments

1
Excel Export Script works on IE7+ , Firefox and Chrome
===========================================================



function fnExcelReport()
    {
             var tab_text="<table border='2px'><tr bgcolor='#87AFC6'>";
             var textRange; var j=0;
          tab = document.getElementById('headerTable'); // id of table


          for(j = 0 ; j < tab.rows.length ; j++) 
          {     
                tab_text=tab_text+tab.rows[j].innerHTML+"</tr>";
                //tab_text=tab_text+"</tr>";
          }

          tab_text=tab_text+"</table>";
          tab_text= tab_text.replace(/<A[^>]*>|<\/A>/g, "");//remove if u want links in your table
          tab_text= tab_text.replace(/<img[^>]*>/gi,""); // remove if u want images in your table
                      tab_text= tab_text.replace(/<input[^>]*>|<\/input>/gi, ""); // reomves input params

               var ua = window.navigator.userAgent;
              var msie = ua.indexOf("MSIE "); 

                 if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./))      // If Internet Explorer
                    {
                           txtArea1.document.open("txt/html","replace");
                           txtArea1.document.write(tab_text);
                           txtArea1.document.close();
                           txtArea1.focus(); 
                            sa=txtArea1.document.execCommand("SaveAs",true,"Say Thanks to Sumit.xls");
                          }  
                  else                 //other browser not tested on IE 11
                      sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));  


                      return (sa);
                            }

    Just Create a blank iframe

        <iframe id="txtArea1" style="display:none"></iframe>

    Call this function on

        <button id="btnExport" onclick="fnExcelReport();"> EXPORT 
        </button>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.