Visualizzazione dei risultati da 1 a 5 su 5
  1. #1
    Utente di HTML.it L'avatar di redmak
    Registrato dal
    May 2005
    Messaggi
    83

    Esportare una tabella in excel

    Salve a tutti, ho necessità di esportare una tabella in html in un foglio excel. Cercando nel forum ho trovato una funzione che fa esattamente questa operazione.

    Ho solo un problema, la funzione recupera il contenuto di ogni cella della tabella, ma nella mia tabella ogni cella contiene un tag input. Come faccio a prendere il valore presente nel tag?

    codice:
    <html>
    
    <head>
    <script type="text/javascript">
    
    function CreateExcelSheet()
    {
    
    
    var x=myTable.rows
    
    var xls = new ActiveXObject("Excel.Application")
    xls.visible = true
    xls.Workbooks.Add
    for (i = 0; i < x.length; i++)
    {
    var y = x[i].cells
    
    for (j = 0; j < y.length; j++)
    {
    xls.Cells( i+1, j+1).Value = y[j].innerText
    }
    }
    
    
    
    
    }
    </script>
    
    
    </head>
    
    <body marginheight="0" marginwidth="0">
    
    
    
    esporta tabella in xls
    </p>
    <table id="myTable" border="0">
    <tr> <th>Name</th> <th>Age</th></tr>
    <tr> <td><input value="Shivani" /> </td> <td><input value="25" /></td> </tr>
    <tr> <td><input value="Naren" /> </td> <td><input value="28" /></td> </tr>
    <tr> <td><input value="Logs" /></td> <td><input value="57" /></td> </tr>
    <tr> <td><input value="Kas" /></td> <td><input value="54" /></td> </tr>
    <tr> <td><input value="Sent" /> </td> <td><input value="26" /></td> </tr>
    <tr> <td><input value="Bruce" /> </td> <td><input value="7" /></td> </tr>
    </table>
    
    
    </body>
    
    </html>
    
    
    
    </body>
    
    </html>
    grazie

  2. #2
    Moderatore di Annunci siti web, Offro lavoro/collaborazione, Cerco lavoro L'avatar di cavicchiandrea
    Registrato dal
    Aug 2001
    Messaggi
    26,133
    devi scorrere i nodi del documento documentati qui
    Cavicchi Andrea
    Problemi con javascript, jquery, ajax clicca qui

  3. #3
    Utente di HTML.it L'avatar di redmak
    Registrato dal
    May 2005
    Messaggi
    83
    non sono molto esperto di javascript, leggendo la guida DOM la funzione dobebbe essere più o meno così:

    codice:
    <html>
    
    <head>
    <script type="text/javascript">
    
    function CreateExcelSheet(){
    	
    	var x=myTable.rows
    	
    	var xls = new ActiveXObject("Excel.Application")
    	xls.visible = true
    	xls.Workbooks.Add
    	for (i = 0; i < x.length; i++){
    		var y = x[i].cells
    		for (j = 0; j < y.length; j++){
    			xls.Cells( i+1, j+1).Value = y[j].getElementsByTagName("input").getAttribute("value")
    			
    		}
    	}
    }
    </script>
    
    
    </head>
    
    <body marginheight="0" marginwidth="0">
    
    
    
    esporta tabella in xls
    </p>
    <table id="myTable" border="0">
    <tr> <th>Name</th> <th>Age</th></tr>
    <tr> <td><input value="Shivani" /> </td> <td><input value="25" /></td> </tr>
    <tr> <td><input value="Naren" /> </td> <td><input value="28" /></td> </tr>
    <tr> <td><input value="Logs" /></td> <td><input value="57" /></td> </tr>
    <tr> <td><input value="Kas" /></td> <td><input value="54" /></td> </tr>
    <tr> <td><input value="Sent" /> </td> <td><input value="26" /></td> </tr>
    <tr> <td><input value="Bruce" /> </td> <td><input value="7" /></td> </tr>
    </table>
    
    
    </body>
    
    </html>
    
    
    
    </body>
    
    </html>

    solo che non funziona... dov'è l'errore?

  4. #4
    Moderatore di Annunci siti web, Offro lavoro/collaborazione, Cerco lavoro L'avatar di cavicchiandrea
    Registrato dal
    Aug 2001
    Messaggi
    26,133
    Cosi recuperi i valori di ogni input presente nella pagina:
    codice:
    <html>
    
    <head>
    <script type="text/javascript">
    
    function CreateExcelSheet(){
    	
    	var x= document.getElementById('myTable').getElementsByTagName("input");
    		for (var i = 0; i < x.length; i++){
    			alert(i);
    		var y = x[i].getAttribute("value")
    			document.getElementById('ciclo').innerHTML=y;
    	}
    }
    </script>
    
    
    </head>
    
    <body marginheight="0" marginwidth="0">
    
    
    
    esporta tabella in xls
    </p>
    <table id="myTable" border="0">
    <tr> <th>Name</th> <th>Age</th></tr>
    <tr> <td><input value="Shivani" /> </td> <td><input value="25" /></td> </tr>
    <tr> <td><input value="Naren" /> </td> <td><input value="28" /></td> </tr>
    <tr> <td><input value="Logs" /></td> <td><input value="57" /></td> </tr>
    <tr> <td><input value="Kas" /></td> <td><input value="54" /></td> </tr>
    <tr> <td><input value="Sent" /> </td> <td><input value="26" /></td> </tr>
    <tr> <td><input value="Bruce" /> </td> <td><input value="7" /></td> </tr>
    </table>
    <div id="ciclo"></div>
    
    </body>
    
    </html>
    
    
    
    </body>
    
    </html>
    Come integrare con new ActiveXObject("Excel.Application") excel provvedi tu, non ho window e ie.

    P.S. Sei consapevole che funzionerà solo ie e chiederà conferma di tale procedura? (con un avviso di sistema)
    Cavicchi Andrea
    Problemi con javascript, jquery, ajax clicca qui

  5. #5
    Utente di HTML.it L'avatar di redmak
    Registrato dal
    May 2005
    Messaggi
    83
    grazie 1000.

    lo so che funzionerà solo con ie, ma è all'interno di un sistema più grande che funziona solo con ie.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.