2016-08-06 2 views
0

rainabba/jquery-table2excel을 사용하여 html 표를 내보내고 Excel에서 파일을 다운로드합니다. 버튼을 클릭하면 파일이 다운로드되지만 파일이나 형식이 손상되었음을 알리는 데 탁월합니다. 제발 제안하십시오. 이 code..for 생성파일 형식 또는 파일 확장명이 유효하지 않습니다.

<html> 
<head> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> 
    <script type="text/javascript" src="jquery-table2excel-master/dist/jquery.table2excel.min.js"></script> 

    <script type="text/javascript"> 
$(document).ready(function(e){ 
    $("#myButton").click(function (e){ 
     $(".mytable").table2excel({ 
      name:"new File", 
      filename:"work", 
      //fileext:".xlsx" 

     }); 

    }); 

    }); 


</script> 
</head> 
<body> 
    <button id="myButton">Click To Download</button> 
    <table class="mytable" border="1"> 
     <tr> 
      <th>new1</th> 
      <th>new2</th> 
      <th>new3</th> 
      <th>new4</th> 

     </tr> 
     <tr> 
      <td>Data 1</td> 
      <td>Data 2</td> 
      <td>Data 3</td> 
      <td>Data 4</td> 
     </tr> 
    </table> 
</body> 
</html> 
+0

http://stackoverflow.com/questions/38377796/export-html-table-to-excel-doesnt-open- .. 파일을 엑셀 in-office-2010/38379929 # 38379929 –

+0

여전히 같은 문제가 발생했습니다 @AxelRichter – art

답변

2
<button class="btn btn-default" onclick="tableToExcel(\'#divId\', \'Excel Report\')">Generate Excel</Button> 
<div id="divId"> 
    <table class="mytable" border="1"> 
     <tr> 
      <th>new1</th> 
      <th>new2</th> 
      <th>new3</th> 
      <th>new4</th> 

     </tr> 
     <tr> 
      <td>Data 1</td> 
      <td>Data 2</td> 
      <td>Data 3</td> 
      <td>Data 4</td> 
     </tr> 
    </table> 
</div> 

    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) { 
        if (!table.nodeType) table = document.getElementById(table) 
        var ctx = { worksheet: name || 'Worksheet', table: table.innerHTML } 
        window.location.href = uri + base64(format(template, ctx)) 
       } 
      })() 

사용

+0

잘못된 문자 오류 표시 – SunUser

+0

@SunUser 코드에 대한 자세한 내용을 알려주십시오. – Goku

관련 문제