繁体   English   中英

当PHP导出到XLS时要保存的命名文件

[英]Naming file to save when php export to xls

我的代码有点奇怪,我不知道我的代码会有什么问题..这是我的代码。

$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值。.请帮助我...谢谢。

以下行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的文件(截至今天的测试日期),并提示用户以该名称保存该文件。

剩下的用于插入实际内容的代码将需要相应地插入,因为关于变量或与问题/代码相关联的文本没有其他内容发布。

您的其他任何变量是否包含无效的文件名字符? 例如,如果您将$ date声明为2013年8月30日,则php不会将变量连接到无效字符之后。

空格会影响文件名的命名。 我要做的就是将我的代码转换成。

$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 -ii-

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM