In my button "Descargar excel"
I have a onclick
that leads to the following function in JS
:
function downloadExcel(status)
{
$.ajax({
url: "http://localhost/WareHouse/reporte.php?status="+status,
type: "GET",
success:function(status)
{
console.log("Entre a download excel");
var tmpElemento = document.createElement('a');
var data_type = 'data:application/vnd.ms-excel';
var tabla_div = document.getElementById('actives');
tmpElemento.href = data_type + ', ' + tabla_div;
//Asignamos el nombre a nuestro EXCEL
tmpElemento.download = 'InventoryToReorder.xls';
tmpElemento.click();
}
});
}
I am calling a file called the reporte.php
same that was used to generate a PDF, the problem I have is that if I download excel
it but when I open it it is in the cell A1
, that is, in the first one it only says null
no more. What could be the problem why it doesn't even show me the column names?
reporte.php
:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<?php echo'<title>Inventory To Reorder</title>';?>
</head>
<body onload="window.print()">
<?php echo '<img src="http://localhost/WareHouse/assets/img/Esterline.png" style="position: absolute; top: 0; left: 0; height:6%; width:23%; margin-top:40px; margin-left:40px;"/>';?>
<?php echo '<center style="margin-top:120px; font-size:14pt;"><strong>Inventory To Reorder</strong></center>';?>
<?php echo '<label><center>____________________________________________________________________________</center></label><br>';?>
<?php
require_once('apis/connection.php');
if(isset($_GET['status']))
{
$status = $_GET["status"];
//Trae todos los item que esten por debajo de su minimo en stock.
$connection = new MySqlServerConnection();
$query = "SELECT i.description_item,i.quantity,u.name_unit,i.reorder_Level,i.target_Stock,l.name_location,i.commentt,io.quantity_s,i.status
FROM inventory_list AS i
INNER JOIN unit_mesurement AS u ON id_unit = fkUnit
INNER JOIN location AS l on id_location = fkLocation
INNER JOIN inventory_output as io on id_output = fkInventory
WHERE i.quantity <= i.reorder_Level OR i.status = 1";
$result = $connection->executeQuery($query,array($status));
if ($result > 0) {
var_dump($result);
//echo $query;
?>
<center>
<table class="table table-striped xd" border="1px;" id="actives">
<thead>
<tr>
<th style="width: 3%;">Description</th>
<th style="width: 3%;">Quantity</th>
<th style="width: 3%;">Usage</th>
<th style="width: 3%;">Name Unit</th>
<th style="width: 3%;">Reorder Level</th>
<th style="width: 3%;">Target Stock</th>
<th style="width: 3%;">Area</th>
<th style="width: 3%;">Comment</th>
</tr>
</thead>
<?php
$arraycount=count($result);
$i=0;
$total=0;
while ($i < $arraycount)
{
?>
<tr>
<td><center><?php echo $result[$i]['description_item']; ?></td>
<td><p style="color:red;" ><?php echo $result[$i]['quantity']; ?></p></td>
<td>
<?php
$qs = $result[$i]['quantity_s'];
switch (true) {
case ($qs >= 1000 && $qs <= 2000) :
echo " 1000 a 2000 SEMANAL";
break;
case ($qs >= 100 && $qs <= 200) :
echo " 100 a 200 SEMANAL Y/O QUINCENAL";
break;
}
?>
</td>
<td><?php echo $result[$i]['name_unit']; ?></td>
<td><?php echo $result[$i]['reorder_Level']; ?></td>
<td><?php echo $result[$i]['target_Stock']; ?></td>
<td><?php echo $result[$i]['name_location']; ?></td>
<td><?php echo $result[$i]['commentt']; ?></center></td>
</tr>
<?php $i++;
}
?>
</table><br><br>
</center>
<?php
}
//}
}
?>
</body>
I usually use ajax to call things like this, and I know it has failed me sometimes because I was missing the
cache
,contentType
,processData
... Although in your case it's get, I'm not sure if it changes anything.I also usually make the calls in this other way:
I recommend you to use PHPExcel https://github.com/PHPOffice/PHPExcel
Your call should be like this:
And your PHP file something like this:
I haven't tried it, please tell me if it worked.