0

i am trying to save a <table>...</table> html markup as an excel file (so it is basically a table html in a file with .xls extension), i have a code i got from the internet(credits to owner and thanks!) which works for IE.

My problem now is, clients use google chrome since it does the export faster (and lower file size), after months since deployment the file downloaded is now not being read by ms excel. The file is downloaded but when opened with ms-excel nothing appears not even a warning that the file is corrupt or file format is unknown try to open? message. Funny thing is when opened with notepad++ the markup can be viewed, delete a space then re-enter it then save>open in excel and viola it now can be viewed..

What i have:

if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./))      // If Internet Explorer
{
    txtArea1.document.open("txt/html","replace");
    txtArea1.document.write($(tbl).html());
    txtArea1.document.close();
    txtArea1.focus(); 
    sa=txtArea1.document.execCommand("SaveAs",true,"toExcel.xls");
    return (sa);
}
else                 
{   
    try{
        var blob = new Blob([$(tbl).html()]);
        var blobURL = window.URL.createObjectURL(blob);
        var a = document.createElement('a');           
        a.href = blobURL;
        a.download = 'toExcel.xls';
        a.click();
    } catch(err){
        //console.log(err.message);
        alert(err.message);            
    }        
}

What i tried (still cannot be read by ms-excel):

var blob = new Blob([$(tbl).html()],{type:'application/vnd.ms-excel'});
/*-----------------------------------------------------------------------*/
var blob = new Blob([$(tbl).html()],{type:"application/vnd.ms-excel"});
        var reader = new window.FileReader();
        reader.readAsDataURL(blob); 
        reader.onloadend = function() {                
            window.open(reader.result);  
        } //this code works but if users export 1000++ rows of data it fails //URL max limit?
/*---------------------------------------------------------------------*/
var blob = new Blob(['\ufeff', $(tbl).html()]);
/*---------------------------------------------------------------------*/
var blob = new Blob(['<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" \n\
                            xmlns="http://www.w3.org/TR/REC-html40"><head>\n\
                            <!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions>\n\
                            <x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]-->\n\
                            </head><body><table>'+$(tbl).html()+'</table></body></html>'],{type:"application/vnd.ms-excel"}); 
/*-----------------------------------------------------------*/

i also tried window.open('data:application/vnd.ms-excel'...) but again if user export 1000++ rows data this will not work...

Even heard and tried FileSaver.js but still not resolved the issue file is downloaded but cannot be opened by excel unless open>edit>save on np++...

Also i am using Google Chrome Version 52.0.2743.116 m ...

Last Resort(will need/have to overhaul the code...): Joel Coehoorn's Answer

Any thoughts/suggestions thanks!

10
  • "but again if user export 1000++ rows data" What is Blob.size of "1000++ rows data"? Is file rendered correctly when less than one thousand rows of data are saved? Commented Aug 17, 2016 at 5:57
  • @guest271314 File is rendered correctly when less than one thousand rows of data are saved Commented Aug 17, 2016 at 5:59
  • You can store each file which contains less than one thousand rows of data and provide .zip of both files as download. See stackoverflow.com/questions/37176397/… Commented Aug 17, 2016 at 6:06
  • @guest271314 blob size of 1104 rows is 1499260 Commented Aug 17, 2016 at 6:06
  • Blob.size of 1104 should not cause issue. What is $(tbl).html()? Commented Aug 17, 2016 at 6:07

1 Answer 1

1

https://support.microsoft.com/en-us/kb/3181507 explains it all... can now close this thread... installing the patch allowed me to view the downloaded html.xls file...

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

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.