$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - How to remove html code in excel downloads using phpspreadsheet with wordpress plugins shortcode|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - How to remove html code in excel downloads using phpspreadsheet with wordpress plugins shortcode

matteradmin8PV0评论

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 : 
     */
    \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:  fb: " class="no-js ie ie9"> <![endif]-->
<!--[if IE 10 ]><html lang="ko-KR" prefix="og:  fb: " class="no-js ie ie10"> <![endif]-->
<!--[if (gte IE 11)|!(IE)]><!--><html lang="ko-KR" prefix="og:  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="">
    <link rel="pingback" href=".php">
    <link rel="stylesheet" href=".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.

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/xfn/11">
    <link rel="pingback" href="https://test/xmlrpc.php">
    <link rel="stylesheet" href="https://test/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.

Share Improve this question edited Feb 18, 2019 at 9:06 Won-Ik Jang asked Feb 18, 2019 at 8:20 Won-Ik JangWon-Ik Jang 134 bronze badges 4
  • Where do you put that download code? – Krzysiek Dróżdż Commented Feb 18, 2019 at 8:42
  • @KrzysiekDróżd call shortcode page (not post, not admin) – Won-Ik Jang Commented Feb 18, 2019 at 8:47
  • what do you mean by “shortcode page”? – Krzysiek Dróżdż Commented Feb 18, 2019 at 8:47
  • @KrzysiekDróżdż it means just page (not in admin panel, for users). check the pickture – Won-Ik Jang Commented Feb 18, 2019 at 8:49
Add a comment  | 

1 Answer 1

Reset to default 1

OK, so you have some PHP code that will return binary data and you want to use it.

So if you want to return only that binary data, then you can't but it inside a page as a shortcode. Shortcodes are printed as article content, so all other parts of page will be printed also (header, navigation menus, and so on).

So there are 2 solutions:

  1. Changing the way you want to do this
  2. Cheating and using Output Buffering (but it's not a nice solution).

1. Printing the content earlier

The best solution would be to use template_redirect hook (or some other early hook) and printing all the contents in there.

The only problem is that you have to know, what file should be printed, so you'll have to put that info as meta values to the post, and not as a shortcode.

add_action( 'template_redirect', function () {
    if ( <CONDITION> ) { // check if excel file should be printed
        // put your excel printing code in here
    }
} );

2. Output buffering

The other way (but not very nice one) would be to start buffering before any content gets printed and discard anything that gets printed before the shortcode.

add_action( 'init', function () {
    ob_start();
} );

add_action( 'wp_footer', function () {
    ob_end_flush();
} );

And in your shortcode:

ob_end_clean();
// the rest of your code
Post a comment

comment list (0)

  1. No comments so far