QAds
Motorola A1200 MING Smartphone
$112.49
Apple iPod shuffle 1 GB Gumus
$35.00
Apple iPod shuffle 1 GB Yesil
$35.00
Nokia N900 Phone/Mobile Computer
$412.49
:: 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.