下载生成的 excel 只能在本地使用 | WordPress的
Downloading generated excel only works locally | WordPress
我有一个 word press 页面,将报告显示为 html table,单击按钮后,该报告应下载为 xlsx 文件。
问题是,虽然下载功能在我的本地机器上运行良好,但在服务器上,我在屏幕上看到这样的乱码,而不是文件下载:
PKǂ�RG�D�Z�[Content_Types].xml���N ...
相关代码如下:
page-export.php
<?php
require_once 'vendor/autoload.php';
global $wpdb;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\IOFactory;
if ( !post_password_required() ):
// fetching data
$query = "SELECT *,
(SELECT COUNT(*) FROM library_votes
WHERE libraries.id = library_votes.library_id) AS votes
FROM libraries ORDER BY votes DESC";
$libraries = $wpdb->get_results ( $query );
// download on click
if(isset($_GET['action']) && $_GET['action'] == 'download') {
$header = ['Col1', 'Col2', 'ColN];
$libArray = json_decode(json_encode($libraries), true);
array_unshift($libArray, $header);
array_columns_delete($libArray, ['ID']);
// BUILD EXCEL FILE!
outputXlsx($libArray, 'excel_filename', 'sheetname');
exit();
}
?>
// download btn
<a href="<?php echo esc_url(get_permalink())?>?action=download" class="btn btn-secondary btn-lg" tabindex="-1" role="button" aria-disabled="true">Download</a>
// loop & html for table
...
<?php
else:
echo get_the_password_form();
endif;
?>
functions.php
function outputXlsx($array, $filename = 'zones',$title = 'Sheet', $debug = false) {
if (!$debug) {
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'. $filename .'.xlsx"');
header('Cache-Control: max-age=0');
}
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$spreadsheet->setActiveSheetIndex(0);
$spreadsheet->getActiveSheet()->fromArray($array,NULL,'A1');
$spreadsheet->getActiveSheet()->setTitle($title);
ob_end_clean();
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');
}
我尝试添加额外的 headers 我在类似的问题上发现,切换 ob_end_clean(),在 outputXlsx 之后退出函数...等等,但它仍然只能在本地工作。
提前致谢!
我终于修好了!
在 wordpress 中启用调试模式给了我解决这两个警告的线索:
- 警告:无法修改 header 信息 - header 已发送
- ob_end_clean(): 删除缓冲区失败。没有要删除的缓冲区
原来我要做的就是把生成excel的代码放在页面顶部html之前,加上ob_start ().
page-export.php:
//line 1 of the file
<?php
ob_start(); // create buffer fo ob_end_clean()
require_once 'vendor/autoload.php';
global $wpdb;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\IOFactory;
if ( !post_password_required() ):
$query = "SELECT *,
(SELECT COUNT(*) FROM library_votes
WHERE libraries.id = library_votes.library_id) AS votes
FROM libraries ORDER BY votes DESC";
$libraries = $wpdb->get_results ( $query );
if(isset($_GET['action']) && $_GET['action'] == 'download') {
$header = ['Col1', 'Col2', 'ColN'];
$libArray = json_decode(json_encode($libraries), true);
array_unshift($libArray, $header);
array_columns_delete($libArray, ['ID']);
outputXlsx($libArray, 'filename', 'sheetname');
exit();
}
?>
// html after
<html>...
我有一个 word press 页面,将报告显示为 html table,单击按钮后,该报告应下载为 xlsx 文件。
问题是,虽然下载功能在我的本地机器上运行良好,但在服务器上,我在屏幕上看到这样的乱码,而不是文件下载:
PKǂ�RG�D�Z�[Content_Types].xml���N ...
相关代码如下:
page-export.php
<?php require_once 'vendor/autoload.php'; global $wpdb; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Writer\Xlsx; use PhpOffice\PhpSpreadsheet\IOFactory; if ( !post_password_required() ): // fetching data $query = "SELECT *, (SELECT COUNT(*) FROM library_votes WHERE libraries.id = library_votes.library_id) AS votes FROM libraries ORDER BY votes DESC"; $libraries = $wpdb->get_results ( $query ); // download on click if(isset($_GET['action']) && $_GET['action'] == 'download') { $header = ['Col1', 'Col2', 'ColN]; $libArray = json_decode(json_encode($libraries), true); array_unshift($libArray, $header); array_columns_delete($libArray, ['ID']); // BUILD EXCEL FILE! outputXlsx($libArray, 'excel_filename', 'sheetname'); exit(); } ?> // download btn <a href="<?php echo esc_url(get_permalink())?>?action=download" class="btn btn-secondary btn-lg" tabindex="-1" role="button" aria-disabled="true">Download</a> // loop & html for table ... <?php else: echo get_the_password_form(); endif; ?>
functions.php
function outputXlsx($array, $filename = 'zones',$title = 'Sheet', $debug = false) { if (!$debug) { header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="'. $filename .'.xlsx"'); header('Cache-Control: max-age=0'); } $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet(); $spreadsheet->setActiveSheetIndex(0); $spreadsheet->getActiveSheet()->fromArray($array,NULL,'A1'); $spreadsheet->getActiveSheet()->setTitle($title); ob_end_clean(); $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx'); $writer->save('php://output'); }
我尝试添加额外的 headers 我在类似的问题上发现,切换 ob_end_clean(),在 outputXlsx 之后退出函数...等等,但它仍然只能在本地工作。
提前致谢!
我终于修好了!
在 wordpress 中启用调试模式给了我解决这两个警告的线索:
- 警告:无法修改 header 信息 - header 已发送
- ob_end_clean(): 删除缓冲区失败。没有要删除的缓冲区
原来我要做的就是把生成excel的代码放在页面顶部html之前,加上ob_start ().
page-export.php:
//line 1 of the file
<?php
ob_start(); // create buffer fo ob_end_clean()
require_once 'vendor/autoload.php';
global $wpdb;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\IOFactory;
if ( !post_password_required() ):
$query = "SELECT *,
(SELECT COUNT(*) FROM library_votes
WHERE libraries.id = library_votes.library_id) AS votes
FROM libraries ORDER BY votes DESC";
$libraries = $wpdb->get_results ( $query );
if(isset($_GET['action']) && $_GET['action'] == 'download') {
$header = ['Col1', 'Col2', 'ColN'];
$libArray = json_decode(json_encode($libraries), true);
array_unshift($libArray, $header);
array_columns_delete($libArray, ['ID']);
outputXlsx($libArray, 'filename', 'sheetname');
exit();
}
?>
// html after
<html>...