How to create a report in excel in php without the use of libraries

1

How do I export a report in excel with its rows and columns from php without the use of external libraries can it be done?

    
asked by Jhon Dember Murillo Mendez 13.04.2018 в 21:58
source

1 answer

4

if you can effectively create the manual excel as follows:

<?PHP
  namespace Chirp;

  // Original PHP code by Chirp Internet: www.chirp.com.au
  // Please acknowledge use of this code by including this header.

  $data = array(
    array("firstname" => "Mary", "lastname" => "Johnson", "age" => 25),
    array("firstname" => "Amanda", "lastname" => "Miller", "age" => 18),
    array("firstname" => "James", "lastname" => "Brown", "age" => 31),
    array("firstname" => "Patricia", "lastname" => "Williams", "age" => 7),
    array("firstname" => "Michael", "lastname" => "Davis", "age" => 43),
    array("firstname" => "Sarah", "lastname" => "Miller", "age" => 24),
    array("firstname" => "Patrick", "lastname" => "Miller", "age" => 27)
  );

  function cleanData(&$str)
  {
    $str = preg_replace("/\t/", "\t", $str);
    $str = preg_replace("/\r?\n/", "\n", $str);
    if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
  }

  // file name for download
  $filename = "website_data_" . date('Ymd') . ".xls";

  header("Content-Disposition: attachment; filename=\"$filename\"");
  header("Content-Type: application/vnd.ms-excel");

  $flag = false;
  foreach($data as $row) {
    if(!$flag) {
      // display field/column names as first row
      echo implode("\t", array_keys($row)) . "\n";
      $flag = true;
    }
    array_walk($row, __NAMESPACE__ . '\cleanData');
    echo implode("\t", array_values($row)) . "\n";
  }

  exit;
?>

Bibliography: link

It is important to bear in mind that manual use implies that any change of style and others will be manual and therefore I personally prefer to use libraries like phpExcel, but to answer your concern if possible as I show you in the example and the respective bibliography ...

I hope you find it useful, greetings.

    
answered by 13.04.2018 в 22:07