下载生成的excel只能在本地工作|WordPress



我有一个word press页面,它将报告显示为html表,单击按钮,该报告应以xlsx文件的形式下载。

问题是,虽然下载功能在我的本地机器上运行得很好,但在服务器上,我会在屏幕上听到这样的胡言乱语,而不是文件下载:

PKǂ�RG�D�Z�[Content_Types].xml���N

以下是相关代码:

  • page-export.php

    <?php
    require_once 'vendor/autoload.php';
    global $wpdb;
    use PhpOfficePhpSpreadsheetSpreadsheet;
    use PhpOfficePhpSpreadsheetWriterXlsx;
    use PhpOfficePhpSpreadsheetIOFactory;
    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 PhpOfficePhpSpreadsheetSpreadsheet();
    $spreadsheet->setActiveSheetIndex(0);
    $spreadsheet->getActiveSheet()->fromArray($array,NULL,'A1');
    $spreadsheet->getActiveSheet()->setTitle($title);
    ob_end_clean();
    $writer = PhpOfficePhpSpreadsheetIOFactory::createWriter($spreadsheet, 'Xlsx');
    $writer->save('php://output');   
    }
    

我尝试添加我在类似问题上发现的其他标头,切换ob_end_clean((,在outputXlsx之后退出函数。。。等等,但它仍然只在本地工作。

提前感谢!

我终于修复了它!

在wordpress中启用调试模式给了我解决这两个警告的线索:

  • 警告:无法修改标头信息-标头已发送
  • 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 PhpOfficePhpSpreadsheetSpreadsheet;
use PhpOfficePhpSpreadsheetWriterXlsx;
use PhpOfficePhpSpreadsheetIOFactory;
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>...

相关内容

  • 没有找到相关文章

最新更新