i trying to make excel download in front page (not admin pages) with call own wordpress plugin shortcode
here is my excel download code :
<?php
global $wpdb;
$item = get_items( $_GET[\'id\'] );
$product_info = $wpdb->get_row(\'SELECT * FROM \'.$wpdb->prefix.\'_table_product WHERE id="\'.$item->prd_id.\'"\', ARRAY_A);
/* PhpSpreadsheet Load */
require dirname( __FILE__ ).\'/phpspreadsheet/autoload.php\';
use PhpOffice\\PhpSpreadsheet\\Spreadsheet;
use PhpOffice\\PhpSpreadsheet\\Writer\\Xlsx;
// use PhpOffice\\PhpSpreadsheet\\IOFactory;
/* Settings
* url : https://phpspreadsheet.readthedocs.io/en/develop/topics/recipes/#write-a-newline-character-n-in-a-cell-altenter
*/
\\PhpOffice\\PhpSpreadsheet\\Cell\\Cell::setValueBinder( new \\PhpOffice\\PhpSpreadsheet\\Cell\\AdvancedValueBinder() );
/* Get Template Excel */
$template_file = dirname(__FILE__) . \'/template.xlsx\';
$reader = new \\PhpOffice\\PhpSpreadsheet\\Reader\\Xlsx();
$spreadsheet = $reader->load($template_file);
/* Write Value Current Registration Form Data */
... some set value codes ...
/* Save Excel and Download */
$spreadsheet->getActiveSheet()->setTitle(\'registration\');
$spreadsheet->setActiveSheetIndex(0);
$reg_date = date(\'Ymd\', strtotime($item->reg_date));
$filename = $reg_date.\'_\'.$item->user_name;
ob_start();
// Redirect output to a client’s web browser (Xlsx)
header(\'Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet\');
header(\'Content-Disposition: attachment;filename="\'.$filename.\'.xlsx"\');
header(\'Cache-Control: max-age=0\');
// If you\'re serving to IE 9, then the following may be needed
// header(\'Cache-Control: max-age=1\');
$writer = new Xlsx($spreadsheet);
ob_get_clean();
$writer->save(\'php://output\');
?>
after download,
and open file.
but, didn\'t opened excel file with error messages
like "it is not available excel type ..."
and open excel file using VSCODE
excel files contents below :
<!DOCTYPE html>
<!--[if IE 9 ]><html lang="ko-KR" prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb#" class="no-js ie ie9"> <![endif]-->
<!--[if IE 10 ]><html lang="ko-KR" prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb#" class="no-js ie ie10"> <![endif]-->
<!--[if (gte IE 11)|!(IE)]><!--><html lang="ko-KR" prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb#" class="no-js"><!--<![endif]-->
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="naver-site-verification" content="dfc8906033b9ecd79b0a715f6e12e05589e78947"/>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="description" content="">
<link rel="profile" href="http://gmpg.org/xfn/11">
<link rel="pingback" href="https://test.com/xmlrpc.php">
<link rel="stylesheet" href="https://test.com/wp-content/themes/testtheme/style.css">
<title>TITLE</title>
...
▼ MORE Header Code
...
▼ BINARY CODE (it seems excel code)
PK
phpspreadsheet v1.6.0 (i guess)
anybody had this issues? and solving?
error page is this
please help, experts.
thanks.
最合适的回答,由SO网友:Krzysiek Dróżdż 整理而成
好的,您有一些PHP代码,它将返回二进制数据,您想使用它。
因此,如果您只想返回二进制数据,那么您不能将其作为短代码放在页面中。短代码被打印为文章内容,因此页面的所有其他部分也将被打印(标题、导航菜单等)。
因此有两种解决方案:
改变你想要做这件事的方式,欺骗和使用输出缓冲(但这不是一个好的解决方案)
1。尽早打印内容最好的解决方案是template_redirect
钩子(或其他早期钩子)并在其中打印所有内容。
唯一的问题是,你必须知道应该打印什么文件,所以你必须将这些信息作为元数据放到帖子中,而不是作为快捷码。
add_action( \'template_redirect\', function () {
if ( <CONDITION> ) { // check if excel file should be printed
// put your excel printing code in here
}
} );
2。输出缓冲另一种方法(但不是很好的方法)是在打印任何内容之前开始缓冲,并丢弃在短代码之前打印的任何内容。
add_action( \'init\', function () {
ob_start();
} );
add_action( \'wp_footer\', function () {
ob_end_flush();
} );
在您的短代码中:
ob_end_clean();
// the rest of your code