上下文:
我计划使用PHPSpreadsheet扩展wordpress上的事件计划程序插件,以在wordpress中创建导出。我正在使用PHPSpreadsheet软件包,从PHP Spreadsheet Doc site.
上给出的基本示例开始这是一个自我处理页面。他们单击一个导出按钮,它使用查询字符串再次触发页面,该查询字符串确定要在电子表格中放入哪些数据。
问题:
除了遵循简单的PHPSpreadsheet示例之外,该脚本当前不执行任何操作。下载触发正常,但是当我打开文件时出现以下错误。
我们发现'storage_report.xlsx'中的某些内容存在问题。您是否希望我们尽力恢复原状?
单击是。
Microsoft Excel试图打开和修复文件。要再次开始此过程,请从“打开文件”对话框中选择“打开并修复”。
单击确定。
然后打开一个空白文档。这是代码。
<?php
require dirname( __FILE__ ) . '/../../vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
if(!defined('ABSPATH')) {die('You are not allowed to call this page directly.');}
add_action( 'admin_menu', 'registryExport::menu' );
add_action( 'init', 'registryExport::export' );
class registryExport
{
/*
TODO:
- Display into submenu in WP Admin
- Find and retrieve epl-data
*/
/*
Add button to plugin dropdown
Clicking Export sidebar submenu item triggers admin_page()
*/
public static function menu()
{
add_submenu_page('edit.php?post_type=epl_event', __('Export', 'epl_event_manager'), __('Export', 'epl_event_manager'), 'export', 'registryExport', 'registryExport::admin_page');
}
/*
Display the page in WP Admin for selecting export options
When button is clicked, export() is triggered
*/
public static function admin_page()
{
?>
<h1>
Export Registrations
</h1>
<hr>
<p>
<a class="button button-primary button-large" href="?post_type=epl_event&page=registryExport&type=all">
Export All
</a>
</p>
<?
}
/*
Checks URI for validity and export type
Triggers data retrieval and download of export file
*/
public static function export()
{
if ( self::isValidUri() )
{
$type = $_GET[ 'type' ]; // Type of export
// Use switch to allow for more types to be added later on
switch ( $type )
{
case( 'all' ):
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World !');
$writer = new Xlsx($spreadsheet);
try
{
$writer->save( "php://output" );
}
catch ( Exception $e )
{
self::pre( $e ); // just a print_r( $var ) wrapped in <pre>
die( "FAILED TO SAVE SPREADSHEET" );
}
// TODO: output headers so that the file is downloaded rather than displayed
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); //for Excel2007
header('Content-Disposition: attachment; filename="storage_report.xlsx"');
die( "Export Button Triggered" );
break;
default:
die( 'Invalid Type selected' );
break;
}
}
}
/**
* Check if GET query values are set and valid
* @return boolean
*/
public static function isValidUri()
{
return ( isset( $_GET[ 'page'] ) && $_GET[ 'page' ] == 'registryExport' && isset( $_GET[ 'type' ] ) )
? true
: false;
}
/*
Nicely output pre-formated text
*/
public static function pre( $var )
{
if ( $var )
{
echo "<pre>";
print_r( $var );
echo "</pre>";
}
}
}
我尝试过的解决方案:
$writer->save()
。php://output
答案 0 :(得分:1)
只需删除页面上的所有输出,这些标题就会在执行时绊倒它。
die( "Export Button Triggered" );
die( 'Invalid Type selected' );