简体   繁体   中英

Naming file to save when php export to xls

My code kinda weird, i don't know what would be the problem of my code.. here's my code.

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

When this code runs it only capture the $project_name value.. Please Help me... Thank you..

The following line header("... will prompt to save a file with apostrophes to the beginning and end of the file.

Example : 'project_quote_id_2013-08-31_as_agent.xls'

header("content-disposition: attachment;filename='".$name_for_project.".xls'");

which should be changed to:

header("content-disposition: attachment;filename=".$name_for_project.".xls");

The code below will produce/echo a saveable file called: project_quote_id_2013-08-31_as_agent.xls (as of today's date for testing).

If, and as Aiden stated in his answer, you are using slashes as your seperator for your $date variable, you will encounter problems.

Try to avoid using spaces for seperators, use hyphens and/or underscores to seperate your values.

For example, this will save to prompt to a file with some dummy content.

<?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;
?>

will produce a file called: project_quote_id_2013-08-31_as_agent.xls (as of today's testing date) and will prompt the user to save it under that name.

The rest of the code that is to insert the actual content, will need to be inserted accordingly, because there was nothing else posted in regards to variables or text associated with the question/code.

Do any of your other variables contain invalid characters for a file name? Eg If you declared your $date as 30/08/2013 then php will not concatenate your variables past the invalid character.

Spaces affect in naming filename. what i do is transform my code into.

$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");

Thanks to @Fred -ii-

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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