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

admin2025-06-03  3

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
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748895954a314622.html

最新回复(0)