12 Sep

Jquery Save to Excel II

Slightly more advanced version of my previous script. This script handles if the table doesn’t have an ID which was a problem previously. It also paves the way for multiple export options including Excel, HTML and PDF – whatever your server can handle.

This code is slightly different to previous code as it will add the export buttons to all tables on the page that do not have a class of ‘noExcel’. Run this script from your jQuery ready() function. A simpler version of this code is available at http://www.topsemtips.com/2008/11/save-html-table-to-excel-using-jquery/:

var RandomIDToUse = 1000;
function AddActionsToTable(  )
{
$(" table:not(.noExcel)").each( function(i){
var TableID = this.id ;

if ( $(this).data('ToolBarAdded') === undefined) {
$(this).data('ToolBarAdded',1);
var TableName = this.id ;
if ( this.id == '' || this.id === undefined ){
TableName = "MyRandomID" + RandomIDToUse ;
$(this).attr('id',TableName);
RandomIDToUse++ ;
}
var FormID = 'form' + TableName ;
var SavetoID = 'saveto' + TableName ;
var DataToDisplayName = "datatodisplay" + TableName ;
var SaveToDisk =
"<div class='TableToolBar'>" +
"<form action='/reports/SaveData/SaveToExcel.php' method='post' target='_blank' id='" + FormID + "'" +
"onsubmit='$(\".DataToDisplay\", this ).val( $(\"<div>\").append( $(\"#" + TableName + "\").eq(0).clone() ).html() )'>" +
"<input type='hidden' id='" + DataToDisplayName + "' name='DataToDisplay' class='DataToDisplay' />" +
"<input type='hidden' id='" + SavetoID + "' name='SaveTo' val='' />" +
"</form>" +
"<input  type='image' src='/images/icons/page_excel.png' width='16' height='16' alt='Save to Excel' title='Save to Excel'" +
" onclick='$(\"input:checked\").attr(\"checked\",true); $(\"#" + SavetoID + "\").val(\"Excel\"); $(\"#" + FormID + "\").submit();' />" +
"  " +
"<input  type='image' src='/images/icons/doc_table.png' width='16' height='16' alt='Save to HTML' title='Save to HTML'" +
" onclick='$(\"input:checked\").attr(\"checked\",true); $(\"#" + SavetoID + "\").val(\"HTML\"); $(\"#" + FormID + "\").submit();' />" +
"  " +
"<input  type='image' src='/images/icons/doc_pdf.png' width='16' height='16' alt='Save to PDF' title='Save to PDF'" +
" onclick='$(\"input:checked\").attr(\"checked\",true); $(\"#" + SavetoID + "\").val(\"PDF\"); $(\"#" + FormID + "\").submit();' />" +
"</div>" ;
$(this).after( SaveToDisk ) ;
}
} ) ;

}

9 thoughts on “Jquery Save to Excel II

  1. Thanks for the fast reply but the html still prints. I do have that file from previous script on the website, however after each table, the html code writes as if it doesnt get interpreted by the browser.

    **********

    <form action='reports/SaveToExcel.php' method='post' target='_blank' id='formMyRandomID1000'onsubmit='$(".DataToDisplay", this ).val( $("”).append( $(“#MyRandomID1000”).eq(0).clone() ).html() )’>    

    *********

    • I found out that if you replace “<” with “”, the code gets injected, now I struggling with that SaveToExcel.php file,

      ****

      ***

      Excel says the file has not a valid extension or is corrupted and the proper extensions for html and pdf are not good.

      • That’s one of the problems… you just have to ignore the Excel warning. Alternatively you could use the CSV code and save the file as CSV and Excel shouldn’t complain then.

    • Ensure you include jquery library (use the Google CDN at http://code.google.com/apis/libraries/devguide.html to make life easy).

      Put the code above into a

Leave a Reply

Your email address will not be published.