继续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();
?>
我们说我在文本框中键入Wong并单击“导出”按钮。
单击导出按钮后,找到包含Wong字的名称并将数据导出到excel。
我的问题是如何将$ record变量传递给download.php,以便将数据导出到[?]上的excel文件中?有人能帮助我吗?
答案 0 :(得分:1)
而不是您的[?]
,require
文件。
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
}
因为变量$record
已经定义($record = FindName($name);
),该变量可以在download.php
中使用