制作phpexcel目录保存下载?

时间:2014-08-15 01:23:05

标签: php phpexcel

如何让用户选择保存文件的位置,就像您知道的下载类型一样,以下是代码:

echo date('H:i:s').' Set title row bold'.EOL;
$objPHPExcel->getActiveSheet()->getStyle('A1:F1')->getFont()->setBold(true);

// Set autofilter
echo date('H:i:s').' Set autofilter'.EOL;
// Always include the complete filter range!
// Excel does support setting only the caption
// row, but that's not a best practise...
$objPHPExcel->getActiveSheet()->setAutoFilter($objPHPExcel->getActiveSheet()-           >calculateWorksheetDimension());

// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);

// Save Excel 2007 file
echo date('H:i:s') , " Write to Excel2007 format" , EOL;
$callStartTime = microtime(true);

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save(str_replace(__FILE__, 'C:/DOSTX.xlsx', __FILE__));

$callEndTime = microtime(true);
$callTime = $callEndTime - $callStartTime;

echo date('H:i:s') , " File written to " , str_replace('.php', '.xlsx',     pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL;
echo 'Call time to write Workbook was ' , sprintf('%.4f',$callTime) , " seconds" , EOL;

// Echo memory usage

echo date('H:i:s') , ' Current memory usage: ' , (memory_get_usage(true) / 1024 / 1024)  , " MB" , EOL;

// Save Excel 95 file
echo date('H:i:s') , " Write to Excel5 format" , EOL;
$callStartTime = microtime(true);

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save(str_replace('.php', '.xls', __FILE__));
$callEndTime = microtime(true);
$callTime = $callEndTime - $callStartTime;
ECHO $filename= str_replace('.php', '.xlsx', pathinfo(__FILE__, PATHINFO_BASENAME)) ,     EOL;
echo date('H:i:s') , " File written to " , str_replace('.php', '.xls',    pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL;
echo 'Call time to write Workbook was ' , sprintf('%.4f',$callTime) , " seconds" , EOL;

// Echo memory usage

echo date('H:i:s') , ' Current memory usage: ' , (memory_get_usage(true) / 1024 / 1024)  , " MB" , EOL;


// Echo memory peak usage

echo date('H:i:s').' Peak memory usage: '.(memory_get_peak_usage(true) / 1024 / 1024).'   MB'.EOL;

// Echo done
echo date('H:i:s').' Done writing files'.EOL;

echo "<script>alert('File saved at drive C')</script>";

之前我做了很多标题,但是不能正常使用phpoutput但是excel是不可接受的输出,excel文件无法读取,因为我希望它输出请帮助我。

1 个答案:

答案 0 :(得分:0)

现在我明白了

取代:

$objWriter->save(str_replace('.php', '.xls', __FILE__));

使用:

header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="your_name.xls"');
header('Cache-Control: max-age=0');


// This line will force the file to download
$objWriter->save('php://output');

您必须删除所有输出,否则标题调用将失败。