Quantcast
Channel: Techathon | Techathon
Viewing all articles
Browse latest Browse all 36

Best Way To Create Excel File In PHP

$
0
0

There are several php library available to export excel in php. Here I am explaining most easiest way to create excel file in php without using any external library in PHP.

Below is the sample code to create excel file with using any external PHP library :

 public function exportToExcel($array, $filename) {
        header('Content-Disposition: attachment; filename=' . $filename . '.xls');
        header('Content-type: application/force-download');
        header('Content-Transfer-Encoding: binary');
        header('Pragma: public');
        //print "\xEF\xBB\xBF"; // UTF-8 BOM
        $h = array();
        foreach ($array as $row) {
            foreach ($row as $key => $val) {
                if (!in_array($key, $h)) {
                    $h[] = $key;
                }
            }
        }
        echo '<table border="1"><tr>';
        foreach ($h as $key) {
            $key = ucwords($key);
            echo '<th>' . $key . '</th>';
        }
        echo '</tr>';

        foreach ($array as $row) {
            echo '<tr>';
            foreach ($row as $val) {
                echo '<td>' . strip_tags($val) . '</td>';
            }
            echo '</tr>';
        }

        echo '</table>';
    }

The post Best Way To Create Excel File In PHP appeared first on Techathon.


Viewing all articles
Browse latest Browse all 36

Trending Articles