I want to export all array to Exel file

This is my code

<?php
declare(strict_types=1); 
  $file     = 'Reg.dat'; 
  $current  = file_get_contents($file);
  preg_match_all('!\d+!', $current, $matches);
   foreach ($matches[0] as $id => $match ) :
     echo '
	 <table id="txtArea1" width="200" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td>N'.$match.'</td>
    <td>'.$match.'</td>
  </tr>
</table>'; 
  endforeach ;
  
?>

How can i do

it’s almost done but error

<?
header("Content-Disposition: attachment; filename=\"user".date("dmYHis") .".xls\"");
header("Content-Type: application/vnd.ms-excel;");
header("Pragma: no-cache");
header("Expires: 0");
$out = fopen("php://output", 'w');
foreach ($array as $data)
{
    fputcsv($out, $data,"\t");
}
fclose($out);
?>

‘but error’.

Define.

<?php
declare(strict_types=1); 
  $file     = 'Reg.dat'; 
  $current  = file_get_contents($file);
  preg_match_all('!\d+!', $current, $matches);
   foreach ($matches[0] as $id => $match ) :
     echo '
	 <table id="txtArea1" width="200" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td>N'.$match.'</td>
    <td>'.$match.'</td>
  </tr>
</table>'; 
  endforeach ;
  
header("Content-Disposition: attachment; filename=\"user".date("dmYHis") .".xls\"");
header("Content-Type: application/vnd.ms-excel;");
header("Pragma: no-cache");
header("Expires: 0");
$out = fopen("php://output", 'w');
foreach ($array as $data)
{
    fputcsv($out, $data,"\t");
}
fclose($out);
  
?>

Error warning


Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\encode.php:11) in C:\xampp\htdocs\encode.php on line 16

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\encode.php:11) in C:\xampp\htdocs\encode.php on line 17

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\encode.php:11) in C:\xampp\htdocs\encode.php on line 18

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\encode.php:11) in C:\xampp\htdocs\encode.php on line 19

Notice: Undefined variable: array in C:\xampp\htdocs\encode.php on line 21

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\encode.php on line 21

Well the first 4 are because you cant send headers after echo’ing something. All headers must come before any output.

the fifth and sixth lines are because you’re saying foreach ($array as $data) without actually defining $array.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.