Array-to-CSV-export 函数在 WordPress 插件中面临问题

Array-to-CSV-export function is facing an issue in WordPress-plugin

提问人:dipanjan chakraborty 提问时间:4/23/2015 最后编辑:Stephan Weinholddipanjan chakraborty 更新时间:3/30/2021 访问量:847

问:

我在我的插件页面中使用了一个简单的数组到 CSV 导出函数来生成报告。

当我运行此代码时,我收到一个错误,它将导出整个 html 内容以及我预期的数组。

这是我的代码:

function convert_to_csv($input_array, $output_file_name, $delimiter)
{
    clearstatcache();
    /** open raw memory as file, no need for temp files */
    $temp_memory = fopen('php://memory', 'w');
    /** loop through array  */



    foreach ($input_array as $line) {
        /** default php csv handler **/
        fputcsv($temp_memory, $line, $delimiter);
    }

    //echo '<pre>';
    //print_r($temp_memory); exit;
    /** rewrind the "file" with the csv lines **/
    fseek($temp_memory, 0);
    /** modify header to be downloadable csv file **/
    header('Content-Type: application/csv');
    header('Content-Disposition: attachement; filename="' . $output_file_name . '";');
    /** Send file to browser for download */
    fpassthru($temp_memory);
}

/** Array to convert to csv */

$array_to_csv = Array(
    Array(12566,
        'Enmanuel',
        'Corvo'
    ),
    Array(56544,
        'John',
        'Doe'
    ),
    Array(78550,
        'Mark',
        'Smith'
    )

);

clearstatcache();

convert_to_csv($array_to_csv, 'report.csv', ',');
PHP 数组 wordpress csv

评论

0赞 Rasclatt 4/23/2015
运行此代码时没有出现错误。我按预期下载了 csv。
0赞 dipanjan chakraborty 4/23/2015
当我在 WordPress 的插件环境中运行此代码时,我得到的是 csv,它显示了所有带有列表的 html 代码
0赞 geeves 11/19/2016
代码按预期运行,并下载了一个 csv 文件。添加有关插件和 Wordpress 版本的信息,以帮助我们解决这个问题。
0赞 Paul Wenzel 5/16/2018
您是否启用了任何类型的调试(设置为 true 或 XDebug)?如果是这样,这可能会导致使用 CSV 文件呈现无关的 HTML。WP_DEBUG

答:

0赞 james-geldart 12/5/2016 #1

我猜 WP 在调用此函数后继续进行其正常操作,因此您将在 CSV 之后获得 HTML 模板输出。之后放一个声明应该可以做到,但你需要注意,这不会弄乱 Wordpress 在每个页面响应末尾所做的任何其他事情。例如,Drupal有一个功能就是用于这个目的。我对 WP 不够熟悉,无法确定,但 wp_die() 函数的文档表明您可以使用 PHP 而不会出现太多问题exitfpassthrudrupal_exit()exit

0赞 Manoj Swami 4/26/2018 #2

您可以使用此代码生成扩展名为 .xlsx 的 excel 文件

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/alasql/0.3/alasql.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.7.12/xlsx.core.min.js"></script>

<script type="text/javascript">
    $(document).ready(function() {
    var insuranceArray = new Array(insuranceInfo);
    var bills = db.bills;

    //Create tabs in excel file
    var opts = [{sheetid:'Insurance Information',header:true},{sheetid:'bills Info',header:false}];

    alasql('SELECT * INTO XLSX("Data.xlsx",?) FROM ?', [opts, insuranceArray,bills]]);

    // Close the window once you save the file
    window.onfocus=function(){ 
        window.close();
    }
</script>

这段代码对我有用......

0赞 mujuonly 4/27/2020 #3

在导出功能的顶部尝试此操作。

    global $wpdb;
    $wpdb->hide_errors();
    @set_time_limit(0);
    if ( function_exists( 'apache_setenv' ) )
        @apache_setenv( 'no-gzip', 1 );
    @ini_set('zlib.output_compression', 0);

    //@ob_clean();
    @ob_end_clean(); // to prevent issue that unidentified characters when opened in MS-Excel in some servers


        header( 'Content-Type: text/csv; charset=UTF-8' );
        header( 'Content-Disposition: attachment; filename=export.csv' );
        header( 'Pragma: no-cache' );
        header( 'Expires: 0' );

        $fp = fopen('php://output', 'w');
0赞 baijugoradia 3/30/2021 #4

在不将文件保存在服务器上的情况下生成 csv 的最简单方法

<?php
$array = Array(
    Array(
        12566,
        'Enmanuel',
        'Corvo'
    ),
    Array(
        56544,
        'John',
        'Doe'
    ),
    Array(
        78550,
        'Mark',
        'Smith'
    )
    
);
arr_to_csv($array);
function arr_to_csv($array)
{
    header("Content-type: application/csv");
    header("Content-Disposition: attachment; filename=test.csv");
    $fp = fopen('php://output', 'w');
    foreach ($array as $row) {
        fputcsv($fp, $row);
    }
}
1赞 Syed Ali Ahmed 11/23/2023 #5

我希望你一切顺利。

解决问题的方法是。

我们在 init 上添加一个操作,因为我们不希望我们的网站有任何 HTML 或其他数据,wp_loaded之后最高的钩子是 init 并添加 exit 以不从网站获取其他信息。

<?php

add_action('init', function () {

    if (isset($_GET['generate_csv'])) {
        $array = array(
            array(12566, 'Enmanuel', 'Corvo'),
            array(56544, 'John', 'Doe'),
            array(78550, 'Mark', 'Smith'),
        );

        generate_csv($array);
        exit;
    }

});

function generate_csv($array)
{
    header("Content-type: application/csv");
    header("Content-Disposition: attachment; filename=test.csv");
    $fp = fopen('php://output', 'w');
    foreach ($array as $row) {
        fputcsv($fp, $row);
    }
    fclose($fp);
}

问候

赛义德·阿里·艾哈迈德