2015-12-11 8 views
10

Questa è la funzione che uso per generare i miei file .xls di ExcelC'è un modo per impostare un contenuto sicuro su un file Excel generato da js?

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)) 
    } 
})() 

tableToExcel('table', 'Table Title') 

E questo è l'errore che ottengo

Error

+0

hai controllato il problema che l'errore implica? estensione .xls e il tipo di contenuto corrisponde o hai impostato in modo errato? – Thorarins

+2

è io o il codice creerà il file XLSX non XLS – Poof

+0

@Proof: Il codice crea una miscela di 'HTML' e incorporato' XML' se '' La versione di '' 'è maggiore o uguale a 9' '. 'Excel' accetterà questa miscela come file' * .xls' e analizzerà la miscela per creare una cartella di lavoro da essa. Ma lancerà quell'avvertimento. Ci sono soluzioni 'JavaScript' in natura che possono realmente creare' XLS' e/o 'XLSX'. Cerca parole chiave: 'javascript create excel xls xlsx'. –

risposta

1

Il seguente codice prende il tavolo e lo converte alla excel file.

Nota: - La funzione ha 3o parametro come nome file .

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, filename) { 
 
    if (!table.nodeType) table = document.getElementById(table) 
 
    var ctx = { 
 
     worksheet: name || 'Worksheet', 
 
     table: table.innerHTML 
 
    } 
 

 
    document.getElementById("dlink").href = uri + base64(format(template, ctx)); 
 
    document.getElementById("dlink").download = filename; 
 
    document.getElementById("dlink").click(); 
 

 
    } 
 
})()
<table id="newTab"> 
 
    <th>Col 1</th> 
 
    <th>Col 2</th> 
 
    <th>Col 3</th> 
 
    <tr> 
 
    <td>1</td> 
 
    <td>2</td> 
 
    <td>3</td> 
 
    </tr> 
 
    <tr> 
 
    <td>4</td> 
 
    <td>5</td> 
 
    <td>6</td> 
 
    </tr> 
 
    <tr> 
 
    <td>7</td> 
 
    <td>8</td> 
 
    <td>9</td> 
 
    </tr> 
 
</table> 
 
<a id="dlink" style="display:none;"></a> 
 

 
<input type="button" onclick="tableToExcel('newTab', 'name', 'newSheet.xls')" value="Table to Excel">

Il seguente errore potrebbe popup durante l'apertura del file scaricato.

Errore durante l'apertura del file. Ma si apre facendo clic su 'Sì' comunque.

Error on opening the file. But it opens by clicking 'Yes' anyways.

e il file viene aperto correttamente.

File opens successfully.

Problemi correlati