簡體   English   中英

數組到 CSV 導出功能在 WordPress 插件中面臨問題

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

我在我的插件頁面中使用了一個簡單的數組到 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', ',');

我猜想 WP 在調用此函數后會繼續其正常操作,因此您將在 CSV 之后獲得 HTML 模板輸出。 fpassthru之后放入exit語句應該這樣做,但是您需要注意這不會混淆 Wordpress 在每個頁面響應結束時所做的任何其他事情。 例如,Drupal 有一個drupal_exit()函數就是為了這個目的。 我對 WP 不夠熟悉,無法確定,但是wp_die() 函數的文檔表明您可以使用 PHP exit而不會有太多問題

您可以使用此代碼生成擴展名為 .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>

這段代碼對我有用...

在導出功能的頂部試試這個。

    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');

無需在服務器上保存文件即可生成 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);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM