QAds
Apple iPad 64gb Wi-Fi
$648.97
Sony Ericsson T715 Slider 3G Phone in Rouge Pink
$149.96
Apple iPad 16gb Wi-Fi 3G
$578.62
Apple iPad 64gb Wi-Fi
$648.97
:: HOME PHP Export Excel
 
Export Excel

If we want to create an excel file with PHP, we just need to use some new headers:

    header("Content-type: application/octet-stream");
    header("Content-Disposition: attachment; filename=ExcelFileName.xls;");
    header("Pragma: no-cache");
    header("Expires: 0");

This will force the browser to save this page as an excel file. Here, instead of ExcelFileName, you can type file name.

Now lets see how to enter data into our file.

    $data = "";
    $data .= "Title 1 \t";
    $data .= "Title 2 \t";
    $data .= "Title 3 \t";
    $data .= "\n";
    $data .= "Content 1 \t";
    $data .= "Content 2\t";
    $data .= "Content 3 \t";
    $data .= "\n";

    header("Content-type: application/octet-stream");
    header("Content-Disposition: attachment; filename=ExcelFileName.xls;");
    header("Pragma: no-cache");
    header("Expires: 0");
   
    echo $data;

This will create an excel file for you. we use \t for next cell and \n for next row. you can add any value into excel cells by this way.

you can do adding value into excel cells with many ways. Mostly, you will use it for reporting of database values.

So you will use it to export from database into excel.

this is one example:

<?PHP

    $date = date("m-d-Y H:i"); // we get current date

    $query = MySQL_query("SELECT * FROM inventories");


    $data = "";
    $data .= "Inventory Report ".$date."\t";
    $data .= "\n";
    $data .= "\n";
   
    $data .= "Inventory Code"."\t";
    $data .= "Inventory Title"."\t";
    $data .= "Quantity"."\t";
    $data .= "Price"."\t";
    $data .= "\n";

    while($result = MySQL_fetch_array($query))
    {        
        $data .= $result['InvCode']."\t";  
        $data .= $result['InvTitle']."\t";    
        $data .= $result['InvQty']."\t";
        $data .= $result['InvPrice']."\t";
       
        $total = $total + ($result['InvQty'] * $result['InvPrice']);
   
        $data .= "\n";
    } 
   
    $data .= "\n";
    $data .= "Total Inventory Value: "."\t";  
    $data .= $total."\t"; 
    $data .= "\n";

    $report_name = "InventoryReport_".$date;
   
    header("Content-type: application/octet-stream");
    header("Content-Disposition: attachment; filename=".$report_name.".xls;");
    header("Pragma: no-cache");
    header("Expires: 0");
    echo $data; 

?>

Here I supposed that you connect to database and pulls inventory information and write them into excel file.