Click to See Complete Forum and Search --> : Export to Excel


madddidley
09-26-2006, 08:08 PM
Hello,

I found this tutorial that takes whats in the database and puts it into an excel document. It exports the information to the file but for some reason the "postContent" gets messed up or something because the text becomes these little squares. I have no idea what would cause this, so I'm going to post the script. Maybe someone can see if I'm doing something wrong.

// Connect database.




// 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;
}


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=blogFaceExport.xls ");
header("Content-Transfer-Encoding: binary ");

xlsBOF();

/*
Make a top line on your excel sheet at line 1 (starting at 0).
The first number is the row number and the second number is the column, both are start at '0'
*/

xlsWriteLabel(0,0,"Export Posts");

// Make column labels. (at line 3)
xlsWriteLabel(2,0,"Post Title");
xlsWriteLabel(2,1,"Post Content");
xlsWriteLabel(2,2,"Post Date");

$xlsRow = 3;


// Get data records from table.
$result=mysql_query("select * from posts order by postId asc");

while($row=mysql_fetch_array($result)){


$month=substr($row[postDate], 4, 2);
$day=substr($row[postDate], 6, 2);
$year=substr($row[postDate], 0, 4);
$thedate="$month-$day-$year $row[postTime]";


$postTitle=stripslashes($row['postTitle']);
$postContent=strip_tags(stripslashes($row['postContent']));


xlsWriteLabel($xlsRow,0,$postTitle);
xlsWriteLabel($xlsRow,1,$postContent);
xlsWriteLabel($xlsRow,2,$thedate);

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


mysql_close($db); //clean up

Again, if someone could help point me in the right direction. That would be sweet.

www.maddapps.com

AndreyAlferov
09-29-2006, 02:28 AM
require ("../../pssp/clients_pass.php");
mysql_connect($hst,$nm,$ps);
mysql_select_db($db) or die("...");

$result = mysql_query('select * from report9');

mysql_close();

$count = mysql_num_fields($result);

for ($i = 0; $i < $count; $i++){
$field_name=mysql_field_name($result, $i);

switch($field_name){
case "bic_client": $field_name="BIC";break;
case "name_client": $field_name="Name";break;
case "namefull_client": $field_name="Full Name";break;
case "address_client": $field_name="Address";break;
case "addressustav_client": $field_name="Full Address";break;
}

$header .= $field_name."\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);


// creating the page
header("Content-type: application/octet-stream");

// name of the Excel file
header("Content-Disposition: attachment; filename=report9.xls");
header("Pragma: no-cache");
header("Expires: 0");

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