Forum Moderators: coopster

Message Too Old, No Replies

Formatting data from table export in XL

works but not formatted!

         

henry0

11:38 am on May 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am trying to export the db fields and data from a table to an XL doc
As is it does the export but
I cannot manage to get it organized
The fist XL line lists all the fields in one “sentence” with no spaces and all in the same cell
Data are also exported and unformatted as above mentioned
Any way to fix that?
I should mention that XP pro is the export receiver but it does the same on an old 98SE with office 2000 pro that I also tried out of curiosity.
Thanks
<edit> Typo</edit>
<?
require_once ("config.inc.php");

$conn = db_connect();

$select = "SELECT * FROM link";
$export = mysql_query($select);
$fields = mysql_num_fields($export);

// loop and extract all field names from db
for ($i = 0; $i < $fields; $i++) { // ### OK checked get field names # echo mysql_field_name($export, $i);
$header .= mysql_field_name($export, $i) . "t";
}

// loop and export the values from the db and write them into the XL columns
while($row = mysql_fetch_row($export)) {
$line = '';
foreach($row as $value) {
if ((!isset($value)) OR ($value == "")) {
$value = "t";
} else {
$value = str_replace('"', '""', $value);
$value = '"' . $value . '"' . "t";
}
$line .= $value;
}
$data .= trim($line)."n";
}
$data = str_replace("r","",$data);

// default message
if ($data == "") {
$data = "n(0) Records Found!n";
}

// Auto D-load
header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=extraction.xls");
header("Pragma: no-cache");
header("Expires: 0");

// as is in XL first line lists all fields without separation
//and the bulk of data in one line, without data separations

print "$header\n$data";
?>

jatar_k

4:32 pm on May 16, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



try doing it to csv instead, I use this function

Generating a CSV for Download from MySQL [webmasterworld.com] msg9

henry0

5:42 pm on May 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks
I will try it but will modify it to get the fields from a loop instead of typing fields name in.
so it can be reused on the fly with other tables

I will post it later or tomorrow
Henry