我的代码有点奇怪,我不知道我的代码会有什么问题。这是我的密码。
$project_name = $_POST['project_name'];//example the retrieved data is Testing Project
$quote_id = $_POST['quote_id'];//example the retrieved data is 34425
$date = date("M/d/y");
$as_agent = $_POST['as_agent'];//example the retrieved data is John Doe
$name_for_project = $project_name.' '.$quote_id.' '.$date.' '.$as_agent;
header("Content-Type: application/vnd.ms-excel");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("content-disposition: attachment;filename='".$name_for_project.".xls'");
ob_start();
//The rest of the code is Workbook
echo"
<?xml version='1.0'?>
<?mso-application progid='Excel.Sheet'?>
<Workbook
xmlns='urn:schemas-microsoft-com:office:spreadsheet'
xmlns:o='urn:schemas-microsoft-com:office:office'
xmlns:x='urn:schemas-microsoft-com:office:excel'
xmlns:ss='urn:schemas-microsoft-com:office:spreadsheet'
xmlns:html='http://www.w3.org/TR/REC-html40'>
<DocumentProperties xmlns='urn:schemas-microsoft-com:office:office'>
<Version>11.8036</Version>
</DocumentProperties>
<ExcelWorkbook xmlns='urn:schemas-microsoft-com:office:excel'>
<WindowHeight>6795</WindowHeight>
<WindowWidth>8460</WindowWidth>
<WindowTopX>120</WindowTopX>
<WindowTopY>15</WindowTopY>
<ProtectStructure>False</ProtectStructure>
<ProtectWindows>False</ProtectWindows>
</ExcelWorkbook>";
//so on and so fort...当这段代码运行时,它只捕获$project_name值。请帮帮我..。谢谢。。
发布于 2013-08-31 02:05:08
下面的行header("...将提示将带有撇号的文件保存到文件的开头和结尾。
示例:'project_quote_id_2013-08-31_as_agent.xls'
header("content-disposition: attachment;filename='".$name_for_project.".xls'");应改为:
header("content-disposition: attachment;filename=".$name_for_project.".xls");下面的代码将生成/回显一个名为:project_quote_id_2013-08-31_as_agent.xls的可保存文件(截至今天的测试日期)。
如果正如艾登在他的回答中所说的那样,你使用斜杠作为你的$date变量的分隔器,你就会遇到问题。
尽量避免为分隔符使用空格,使用连字符和/或下划线分隔值。
例如,这将保存为提示到一个带有虚拟内容的文件。
<?php
$date = gmdate('Y-m-d', time() - 3600 * $hours);
$project_name = "project";
$quote_id = "quote_id";
$as_agent = "as_agent";
$name_for_project = $project_name.'_'.$quote_id.'_'.$date.'_'.$as_agent;
$file = $name_for_project.".xls";
// start buffering
ob_start();
// sample dynamically generated data
echo '<table border="1"> ';
echo '<tr><th>Name</th><th>Age</th></tr>';
for ($i=0; $i<=5; $i++) { echo "<tr><td>Name$i</td><td>".($i+1)."</td></tr>";
}
echo '</table>';
$content = ob_get_contents();
ob_end_clean();
header("Expires: 0");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Content-type: application/vnd.ms-excel;charset:UTF-8");
header('Content-length: '.strlen($content));
header('Content-disposition: attachment; filename='.basename($file));
// output all contents
echo $content;
exit;
?>将生成一个名为:project_quote_id_2013-08-31_as_agent.xls的文件(截至今天的测试日期),并将提示用户以该名称保存它。
其余的代码,即插入实际内容的代码,将需要相应地插入,因为在变量或与问题/代码相关的文本中没有发布任何其他内容。
发布于 2013-08-31 01:41:36
您的其他变量是否包含文件名的无效字符?例如,如果您将$date声明为30/08/2013,那么php将不会将您的变量连接到无效字符上。
发布于 2013-08-31 05:24:33
空格会影响文件名的命名。我所做的就是把我的代码转换成。
$project_name = $_POST['project_name'];//example the retrieved data is Testing Project
$quote_id = $_POST['quote_id'];//example the retrieved data is 34425
$date = date("M/d/y");
$as_agent = $_POST['as_agent'];//example the retrieved data is John Doe
$proj_name = str_replace(' ', '', $project_name);
$as_agent = str_replace(' ', '', $as_agent);
$name_for_project = $proj_name."-".$quote_id."-".$date."-".$as_agent;
header("Content-Type: application/vnd.ms-excel");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("content-disposition: attachment;filename=$name_for_project.xls");多亏了@Fred
https://stackoverflow.com/questions/18543584
复制相似问题