无需其他任何库即可生成excel文件

时间:2019-06-13 04:35:08

标签: codeigniter-3

我使用以下代码,但不会生成excel文件 那么在不使用任何其他库的情况下,数组数据不会输入到xl文件中

如果(!function_exists('array_to_xml')){

function array_to_xml($array, $download = "") {
    if ($download != "") {
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachement; filename="' . $download . '"');
    }
    $f = fopen('w') or show_error("Can't open php://output");
    $n = 0;
    foreach ($array as $line) {
        $n++;
        if (!fwrite($f, $line)) {
            show_error("Can't write line $n: $line");
        }
    }
    fclose($f) or show_error("Can't close php://output");
    if ($download == "") {
        return $str;
    } else {
        echo $str;
    }
}

} 如果(!function_exists('query_to_xml')){

function query_to_xml($query, $headers = TRUE, $download = "") {
    if (!is_object($query) OR ! method_exists($query, 'list_fields')) {
        show_error('invalid query');
    }
    $array = array();
    if ($headers) {
        $line = array();
        foreach ($query->list_fields() as $name) {
            $line[] = $name;
        }
        $array[] = $line;
    }
    foreach ($query->result_array() as $row) {
        $line = array();
        foreach ($row as $item) {
            $line[] = $item;
        }
        $array[] = $line;
    }
    echo array_to_xml($array, $download);
}

}

0 个答案:

没有答案