QAds
12 Ay Taksit
Coby MP305-4G MP3 Player
$11.07
Samsung SL30 10MP Digital Camera with 3x Optical Zoom and 2.5 inch LCD
$48.10
Nikon D3000 10.2MP Digital SLR Camera with 18-55mm f/3.5-5.6G AF-S DX VR Nikkor Zoom Lens
$232.76
Sony Ericsson T715 Slider 3G Phone in Rouge Pink
$149.96
:: 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.


PHP Export Excel PHP Export Excel PHP Export Excel
PHP Export Excel
NEW QUESTION
 
Your E-Mail Address
»
Your e-mail will never be shown!  
   
 
Question
»
Please type your question and please use [CODE] tag for your codes!  
  Add CODE Quote BOLD ITALIC Underline Align Left Align Center Align Right Add Line  
   
 
Human Check
»
Enter human check code below!  
   
 
       
 
* Your Question will be answered as soon as possible, probably within 24 hours!
PHP Export Excel PHP Export Excel PHP Export Excel
PHP Export Excel
 
Website Designer, Search Engine Optimization Expert, Ajax Developer,Developed And Designed By Mumin Celik With Using PHP, MySQL, CSS, JavaScript, Ajax And Search Engine Optimizer Sinema ve Film Rehberi Sudoku Oyunu