JavaScript - descargar una tabla html en formato excel

 
Vista:
sin imagen de perfil
Val: 2
Ha aumentado su posición en 9 puestos en JavaScript (en relación al último mes)
Gráfica de JavaScript

descargar una tabla html en formato excel

Publicado por Pablo (1 intervención) el 22/02/2021 15:13:13
Buenas, mi problema es el siguiente, necesito descargar una tabla html en formato excel, para eso utilizo la función
exportTableToExcel('t_buscar', 'Patentamiento'), debería descargarme la tabla con el id t_buscar pero no funciona

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<head>
 
	<meta name="name" content="initial-scale=1.0; maximum-scale=1.0; width=device-width;"  charset="utf-8">
	<link rel="stylesheet" href="estilos.css">
 <script src="procesos.js"></script>
 
 
</head>
<body>
<!-------------------------------TABLA 3-------------------------------------->
<div id="main-container">
	     <h3>CALCULAR TOTAL DE PATENTES POR MES EN UN AÑO</h3>
		<form action="calculos.php" method="post" class="form">
			<label for="year">INGRESAR AÑO</label class="form-label">
			<input type="number" max="3000" min="1995" name="year" id="year" required>
			  <label for="tipo" class="form-label">TIPO</label>
		<!--<input type="text" name="provincia">-->
		<select name="tipo" class="form-control" required>
			<option></option>
			<option>AUTO</option>
			<option>MOTO</option>
		</select>
		<br><br>
			<input class="boton" type="submit" name="total" value="Calcular Total">
			<br><br>
		</form>
 
		<table id="t_buscar">
			<thead>
			<tr>
				<th>AÑO</th>
				<th>MES</th>
				<th>TIPO</th>
				<th>CANTIDAD TOTAL</th>
			</tr>
		</thead>
			<tr>
				<?php ConsultarTotal();
				 ?>
			</tr>
 
		</table>
 
			<br>
	<button onclick="exportTableToExcel('t_buscar', 'Patentamiento')">Descargar</button>
</div>

------------------------------------------------------------codigo javascript---------------------------------------------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" />
<script>
 
function exportTableToExcel(tableID, filename = ''){
    var downloadLink;
    var dataType = 'application/vnd.ms-excel';
    var tableSelect = document.getElementById(tableID);
    var tableHTML = tableSelect.outerHTML.replace(/ /g, '%20');
 
    // Specify file name
    filename = filename?filename+'.xls':'excel_data.xls';
 
    // Create download link element
    downloadLink = document.createElement("a");
 
    document.body.appendChild(downloadLink);
 
    if(navigator.msSaveOrOpenBlob){
        var blob = new Blob(['ufeff', tableHTML], {
            type: dataType
        });
        navigator.msSaveOrOpenBlob( blob, filename);
    }else{
        // Create a link to the file
        downloadLink.href = 'data:' + dataType + ', ' + tableHTML;
 
        // Setting the file name
        downloadLink.download = filename;
 
        //triggering the function
        downloadLink.click();
    }
}
</script>
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder