Write to excel sheet in PHP from MySQL tables


Hi folks,

Some time we need to have a CSV file and store our database data in it for reporting and other things. To create such file on the fly we will need PHP, Apache and MySQL installed which i am sure is installed. What this script will be capable of:

  • Get data from MySQL table
  • Export multiple rows from db
  • Save file with different file name with date & time

Here is the script. One would need to connect to database and the rest of explanation i write in the comments inside the script to explain the lines.

<?php
//db.php connection to your database

// Query to get results from mysql table
$query = "SELECT id,name,phone,address FROM user";
$res = mysql_query($query);

// Functions for export to excel.
function xlsBOF() {
echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
return;
}
function xlsEOF() {
echo pack("ss", 0x0A, 0x00);
return;
}
function xlsWriteNumber($Row, $Col, $Value) {
echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
echo pack("d", $Value);
return;
}
function xlsWriteLabel($Row, $Col, $Value ) {
$L = strlen($Value);
echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
echo $Value;
return;
}
// Unique file name with datetime
$file_name = "csv-".date('dmy-His');
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");;
header("Content-Disposition: attachment;filename=$file_name.xls ");
header("Content-Transfer-Encoding: binary ");

xlsBOF();

// Top of the excel sheet to write header
xlsWriteLabel(0,0,"User Data - CSV");

// Make column labels. (at line 3)
xlsWriteLabel(2,0,"Id");
xlsWriteLabel(2,1,"Name");
xlsWriteLabel(2,2,"Phone");
xlsWriteLabel(2,3,"Address");

// To start writing from row three from top
$xlsRow = 3;

// Put data records from mysql by while loop.
while($row=mysql_fetch_array($res)){

xlsWriteNumber($xlsRow,0,$row['id']);// 0 indicates column number
xlsWriteLabel($xlsRow,1,$row['name']);
xlsWriteLabel($xlsRow,2,$row['phone']);
xlsWriteLabel($xlsRow,3,$row['address']);

$xlsRow++;
}
xlsEOF();
exit();
?>

This script is tested and working nicely.

  • Anono

    How do you set column widths in this script?

  • http://anl4u.com/blog Admin

    Hi Anono,
    Maybe you would refer to PHP COM class for more details.
    COM PHP

    Here is another example of COM, would help to get you start using it, there are some MS libs you will need.
    COM Examples

  • Anamikha

    Hi simply super. And thanks a lot. I exported the data from database to excel usinh PHP. But i couldnt export an image to excel. do u have any idea for this?

  • wiki

    how can i make a row

  • wiki

    i means row title

  • http://anl4u.com/blog Admin

    If you mean title for the row, then i already explained it above.
    xlsWriteLabel(2,1,”Name”);
    Check the above line in the script and you will see how it print out row titles. For example the line i pasted here will output Name at the header of the row.

  • newbie

    How to save the file, or where is the file being saved?

    • http://anl4u.com/blog Admin

      By default it will save the file in the directory where the script is located/saved. If you want to change that, you can add path string to $file_name variable.

  • newmember

    Hello,

    Is it possible to write an image in excel? I want in each row a new image. I have a product table in which with each product image is attached the same I would like to get reflected in excel.

    Please let me know if having idea to anyone.

  • Rob

    Is there any way to create a separate worksheet?

Search

Articles

What's Hot

Recent Comments