PHPSpreadsheet 以无效格式保存文件
PHPSpreadsheet saving file in invalid format
上下文:
我计划使用 PHPSpreadsheet 扩展 wordpress 上的活动策划插件,以在 wordpress 中创建导出。我正在使用 PHPSpreadsheet 包,从 PHP Spreadsheet Doc site.
上给出的基本示例开始
这是一个自我处理的页面。他们单击一个导出按钮,它会再次触发页面,其中包含一个查询字符串,该查询字符串确定要将哪些数据放入电子表格。
问题:
除了遵循简单的 PHPSpreadsheet 示例之外,该脚本目前没有做任何事情。下载触发正常,但当我打开文件时出现以下错误。
We found a problem with some content in 'storage_report.xlsx'. Do you want us to try to recover as much as we can?
点击是。
Microsoft Excel was attempting to open and repair the file. To start this process again, choose Open and Repair from the Open file dialog.
单击“确定”。
然后打开一个空白文档。这是代码。
<?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>";
}
}
}
我尝试过的解决方案:
- 在 header 更改之后移动
$writer->save()
。
- 保存为文件名而不是
php://output
只需删除页面上的任何输出,那些 headers 会在执行时偶然发现它。
die( "Export Button Triggered" );
die( 'Invalid Type selected' );
上下文:
我计划使用 PHPSpreadsheet 扩展 wordpress 上的活动策划插件,以在 wordpress 中创建导出。我正在使用 PHPSpreadsheet 包,从 PHP Spreadsheet Doc site.
上给出的基本示例开始这是一个自我处理的页面。他们单击一个导出按钮,它会再次触发页面,其中包含一个查询字符串,该查询字符串确定要将哪些数据放入电子表格。
问题:
除了遵循简单的 PHPSpreadsheet 示例之外,该脚本目前没有做任何事情。下载触发正常,但当我打开文件时出现以下错误。
We found a problem with some content in 'storage_report.xlsx'. Do you want us to try to recover as much as we can?
点击是。
Microsoft Excel was attempting to open and repair the file. To start this process again, choose Open and Repair from the Open file dialog.
单击“确定”。
然后打开一个空白文档。这是代码。
<?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>";
}
}
}
我尝试过的解决方案:
- 在 header 更改之后移动
$writer->save()
。 - 保存为文件名而不是
php://output
只需删除页面上的任何输出,那些 headers 会在执行时偶然发现它。
die( "Export Button Triggered" );
die( 'Invalid Type selected' );