Cont on Excel cannot open the file created by XLSXWriter
test.php
<?php
if(isset($_POST['submit2']) && ($_POST['submit2'] == 'Export'))
{
//db connection
$name = $_POST['name'];
$record = FindName($name);
if(count($record) > 0)
{
[?]
}
}
else
{
?>
<form action="c.php" method="post">
Name: <input type="text" name="name" />
<input type="submit" name="submit2" value="Export" />
</form>
<?php
}
function FindName($name)
{
$query = "SELECT *
FROM user
WHERE Name like '".$name."%'";
$result = mysqli_query($dbc, $query);
$record = array();
while($row = mysqli_fetch_array($result))
{
$record[] = $row;
}
return $record;
}
?>
download.php
<?php
include('./PHP_XLSXWriter-master/xlsxwriter.class.php');
ini_set('display_errors', 0);
ini_set('log_errors', 1);
error_reporting(E_ALL & ~E_NOTICE);
$filename = "example.xlsx";
header('Content-disposition: attachment; filename="'.XLSXWriter::sanitize_filename($filename).'"');
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');
$header = array(
'ID'=>'string',
'Name'=>'string',
'Age'=>'string',
'Gender'=>'string',
);
for($i = 0; $i < count($record); $i++)
{
$data[$i] = array($record[$i]['ID'],$record[$i]['Name'],$record[$i]['Age'],$record[$i]['Gender']);
}
$writer = new XLSXWriter();
$writer->writeSheet($data,'Sheet1',$header);
$writer->writeToStdOut();
?>
Let's say I type Wong on the textbox and click Export button.
After click export button, find the name that contain Wong word and export the data to the excel.
My question is how do I pass the $record variable to the download.php in order to export the data into the excel file on the [?]? Can someone help me?
Instead of your [?]
, require
the file.
if(count($record) > 0)
{
require "PATH_TO/download.php";
exit; // <- IMPORTANT to add exit
// to not have in download response html data from test.php
}
as the variable $record
is already defined ( $record = FindName($name);
) that variable can be used in download.php