Download in Null

2

On my%% co_button I have a "Descargar excel" that leads to the following function in onclick :

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'm sending a file called JS same that was used to generate a PDF, the problem I have is that if I download the reporte.php but when open it is in cell excel is in the first only says A1 does not say more. What could be the problem for which he does not even show me the names of the columns?

null :

<!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>
    
asked by VirusDetected 14.12.2018 в 22:44
source

2 answers

0

I recommend you use PHPExcel link

Your call should be like this:

function downloadExcel(status)
{
    url = "http://localhost/WareHouse/reporte.php?status="+status;
    window.open(url,"_blank"); 
}

And your PHP file something like this:

<?php
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('America/Mexico_City');//Change to your location

if (PHP_SAPI == 'cli')
die('This example can be run only in web browsers');

/** Include PHPExcel */
require_once dirname(__FILE__) . '/Classes/PHPExcel.php';

// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

// Set document properties
$objPHPExcel->getProperties()->setCreator("StackOverFlow")
                         ->setLastModifiedBy("StackOverFlow")
                         ->setTitle("Office 2007 XLSX Document")
                         ->setSubject("Office 2007 XLSX Document")
                         ->setDescription("Document for Office 2007 XLSX, generated using PHP classes.")
                         ->setKeywords("office 2007 openxml php")
                         ->setCategory("Result file");
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)
  {
      $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', 'Description');
      $objPHPExcel->setActiveSheetIndex(0)->setCellValue('B1', 'Quantity');
      $objPHPExcel->setActiveSheetIndex(0)->setCellValue('C1', 'Usage');
      $objPHPExcel->setActiveSheetIndex(0)->setCellValue('D1', 'Name Unit');
      $objPHPExcel->setActiveSheetIndex(0)->setCellValue('E1', 'Reorder Level');
      $objPHPExcel->setActiveSheetIndex(0)->setCellValue('F1', 'Target Stock');
      $objPHPExcel->setActiveSheetIndex(0)->setCellValue('G1', 'Area');
      $objPHPExcel->setActiveSheetIndex(0)->setCellValue('H1', 'Comment');


      $arraycount=count($result);
      $i=0;
      $total=0;
      while ($i < $arraycount)
      {
        $j=$i+2;
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A'.$j, $result[$i]['description_item']);
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('B'.$j, $result[$i]['quantity']);
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('C'.$j, $result[$i]['quantity_s'];);
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('D'.$j, $result[$i]['name_unit'];);
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('E'.$j, $result[$i]['reorder_Level'];);
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('F'.$j, $result[$i]['target_Stock'];);
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('G'.$j, $result[$i]['name_location'];);
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('H'.$j, $result[$i]['commentt'];);
$i++;
        }
    }
}

// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('Result');


// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);


// Redirect output to a client’s web browser (Excel5)
$nombre=date("d-m-y H:i");
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="Reporte_'.$nombre.'.xls"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');

// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;

I have not tried it, please tell me if it worked.

    
answered by 19.12.2018 / 20:14
source
1

I usually use ajax to call things this way, and I know that it has ever failed me because I was missing the cache , contentType , processData ... Although in your case it's get, I'm not sure if it changes anything.

$.ajax({ url: url,
        type: 'post',
        success: function(output) {
            console.log(output);
        },
        error: function(salida) {
            console.log(salida);
        },
        data: formData,
        cache: false,
        contentType: false,
        processData: false,

    });

I also usually make calls in this other way:

var request = new XMLHttpRequest();
var formData = new FormData();
request.onreadystatechange = function() {
    if (request.readyState == XMLHttpRequest.DONE) {
        var response = request.responseText;
        console.log(response);
    }
};
request.open('POST', url);
request.send(formData);
    
answered by 20.12.2018 в 14:22