PDA

View Full Version : php to .xls format



deletedUser2352352
November 10th, 2006, 12:08 PM
Howdy people

Just wondering how would i go about getting php to export an excel format?

So an example would be.

Someone would go onto a site pull some information from a database then export as an execl file.

i don't know myself yet want i need as fa as information in the file would be but just a jest would help me rolling on this.

Many thanks

DHDesign
November 10th, 2006, 03:22 PM
i found this and had used it...(ps - im not taking credit for this...just found it through some googling):



<?php
//Written by Dan Zarrella. Some additional tweaks provided by JP Honeywell
//pear excel package has support for fonts and formulas etc.. more complicated
//this is good for quick table dumps (deliverables)

include('DB_connection.php');
$result = mysql_query('select * from excel_test', $linkID);
$count = mysql_num_fields($result);

for ($i = 0; $i < $count; $i++){
$header .= mysql_field_name($result, $i)."\t";
}

while($row = mysql_fetch_row($result)){
$line = '';
foreach($row as $value){
if(!isset($value) || $value == ""){
$value = "\t";
}else{
# important to escape any quotes to preserve them in the data.
$value = str_replace('"', '""', $value);
# needed to encapsulate data in quotes because some data might be multi line.
# the good news is that numbers remain numbers in Excel even though quoted.
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$data .= trim($line)."\n";
}
# this line is needed because returns embedded in the data have "\r"
# and this looks like a "box character" in Excel
$data = str_replace("\r", "", $data);


# Nice to let someone know that the search came up empty.
# Otherwise only the column name headers will be output to Excel.
if ($data == "") {
$data = "\nno matching records found\n";
}

# This line will stream the file to the user rather than spray it across the screen
header("Content-type: application/octet-stream");

# replace excelfile.xls with whatever you want the filename to default to
header("Content-Disposition: attachment; filename=excelfile.xls");
header("Pragma: no-cache");
header("Expires: 0");

echo $header."\n".$data;
?>

Mak Valley
November 10th, 2006, 04:50 PM
and there's this tutorial: http://www.phpfreaks.com/tutorials/114/0.php
if you would like to understand what you're doing..

bwh2
November 10th, 2006, 11:21 PM
this may also help: http://pear.php.net/package/Spreadsheet_Excel_Writer

deletedUser2352352
November 11th, 2006, 04:12 PM
cheers guys big help. Bookmarked all them and trying that scrip out.

After looking myself whats the dirrference between a .xsl file and .cvs? just wondering

bwh2
November 11th, 2006, 09:44 PM
xls is the standard excel format. csv is a comma separated. it's a format that many DBs export from. if you open an excel file in a text editor, it will look like garbage. if you open a csv in a text editor, you will see the values just separated by commas. excel knows to parse csvs into a spreadsheet view.

deletedUser2352352
November 12th, 2006, 06:06 AM
so in hindsight what format would be better to export to?

bwh2
November 12th, 2006, 10:14 AM
i prefer tab-delimited text file over both xls and csv.

deletedUser2352352
November 12th, 2006, 11:35 AM
hmm its now getting tricky.

I think for what i need this for the most easliy used file format would be best so its not just for excel. I'll take a look a tab-delimited text file now.

Cheers

bwh2
November 12th, 2006, 05:57 PM
yeah, then you would be best off using a tab-delimited text file. my second option would be csv.