我正在使用PHP(PHPExcel)来处理所有excel文件。 目前我已在文件夹中保存了“New Template.xlsx”。现在我希望用户只需点击一下按钮即可下载“New Template.xlsx”。
我如何使用php或phpexcel(我也使用Yii PHP框架)?
我不知道如何开始,我现在所拥有的只是
$filePath = Yii::app()->basePath . '\\vendors\\Excel Template\\New Template.xlsx';
Yii::import('application.vendors.PHPExcel', true);
$objPHPExcel = PHPExcel_IOFactory::load($filePath);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
修改
感谢Miqi180的评论,我现在可以下载excel文件,但由于文件扩展名/文件格式无效,我无法打开它。
以下是我的代码:
<?php
ignore_user_abort(true);
set_time_limit(0); // disable the time limit for this script
//$path = "/absolute_path_to_your_files/"; // change the path to fit your websites document structure
$path = Yii::app()->basePath . '\\vendors\\Excel Template\\';
//$dl_file = preg_replace("([^\w\s\d\-_~,;:\[\]\(\].]|[\.]{2,})", '', $_GET['download_file']); // simple file name validation
$dl_file = preg_replace("([^\w\s\d\-_~,;:\[\]\(\].]|[\.]{2,})", '', 'NewTemplate.xlsx'); // simple file name validation
$dl_file = filter_var($dl_file, FILTER_SANITIZE_URL); // Remove (more) invalid characters
$fullPath = $path.$dl_file;
if ($fd = fopen ($fullPath, "r")) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
switch ($ext) {
case "xlsx":
//header("Content-type: application/vnd.ms-excel");
header("Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a file download
break;
// add more headers for other content types here
default;
header("Content-type: application/octet-stream");
header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
break;
}
header("Content-length: $fsize");
header("Cache-control: private"); //use this to open files directly
while(!feof($fd)) {
$buffer = fread($fd, 2048);
echo $buffer;
}
}
fclose ($fd);
exit;
答案 0 :(得分:1)
xlsx
文件的相应标头php://output
查看PHPExcel Examples
文件夹中的一些示例,例如01simple-download-xlsx.php
但除非你在加载模板和发送给用户之间更改模板,否则不需要使用PHPExcel
答案 1 :(得分:1)
基本上,您需要做的是执行一个php脚本,该脚本在加载另一个页面时触发,例如“download.php”然后通过该脚本中的header命令发送文件。
完整的工作代码示例取自here:
<?php
ignore_user_abort(true);
set_time_limit(0); // disable the time limit for this script
$path = "/absolute_path_to_your_files/"; // change the path to fit your websites document structure
$dl_file = preg_replace("([^\w\s\d\-_~,;:\[\]\(\].]|[\.]{2,})", '', $_GET['download_file']); // simple file name validation
$dl_file = filter_var($dl_file, FILTER_SANITIZE_URL); // Remove (more) invalid characters
$fullPath = $path.$dl_file;
if ($fd = fopen ($fullPath, "r")) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
switch ($ext) {
case "pdf":
header("Content-type: application/pdf");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a file download
break;
// add more headers for other content types here
default;
header("Content-type: application/octet-stream");
header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
break;
}
header("Content-length: $fsize");
header("Cache-control: private"); //use this to open files directly
while(!feof($fd)) {
$buffer = fread($fd, 2048);
echo $buffer;
}
}
fclose ($fd);
exit;
但是,您需要先正确设置内容类型(请参阅this帖子)。
这
header("Content-type: application/pdf");
到
header("Content-type: application/vnd.ms-excel");
或For Excel2007及以上.xlsx文件
header("Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
修改强>
根据此MSDN blog article,Excel .xlsx文件的正确扩展MIME类型实际上是:
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet