Page 1 of 1

table to excel

Posted: Sun Apr 11, 2004 3:32 pm
by nikko50
Trying to get a table to print to an excel sheet. All i get is page not found in my browser. I have the link pointing to
shiplistxls.php/shiplistxls.xls

Here is the code for the excel sheet.

<?
Header("Content-Type: application/vnd.ms-excel");


$query_text = "SELECT * FROM shiplist";

$result = mysql_query($query_text);
$fieldcounts = mysql_num_fields($result);

for($i = 0; $i < $fieldcounts; $i++) {
$fieldtype = mysql_fetch_field($result, $i);
echo "$fieldtype->name [$fieldtype->type]\t";
}
echo "\n";

while ($myrow = mysql_fetch_array($result)) {
for($i = 0; $i < $fieldcounts; $i++) {
$fieldname = mysql_field_name($result, $i);
echo "$myrow[$fieldname]\t";
}
echo "\n";
}
?>

Posted: Sun Apr 11, 2004 4:42 pm
by feyd
looks like you are writing out plain text. excel files are binary. It may be easier to export your table in csv format.

Posted: Sun Apr 11, 2004 6:15 pm
by nikko50
Hello.I got this to output fine in Mozilla. But it does nor being up Excel in Explorer. Why is this happeneing??

if(!$getuserline[realname])
{
header("Location: login.php");
}
else
{header("Content-Type: application/vnd.ms-excel");
$query_text = "SELECT * FROM shiplist";
$result = mysql_query($query_text);
$fieldcounts = mysql_num_fields($result);
for($i = 0; $i < $fieldcounts; $i++) {
$fieldtype = mysql_fetch_field($result, $i);
echo "$fieldtype->name [$fieldtype->type]\t";
}
echo "\n";

while ($myrow = mysql_fetch_array($result)) {
for($i = 0; $i < $fieldcounts; $i++) {
$fieldname = mysql_field_name($result, $i);
echo "$myrow[$fieldname]\t";
}
echo "\n";
}
}
?>

Posted: Tue Apr 13, 2004 8:24 pm
by ckuipers
I've been doing writes to excel myself and I've done it like this:

Code: Select all

#Create an object instance
$excel= new COM("Excel.Application") or die("Excel could not be started");

#pop open the Excel application
$excel->Visible= 1;

#turn off alerts
$excel->DisplayAlerts= 0;

$Directory= _Dir ."po_excel/" . $country_id . ".xls";

#specify workbook
$workbook= $excel->Workbooks->Open($Directory);

#Activate the first worksheet
$sheets= $workbook->Worksheets(1);
$sheets->activate;

# Cells(5,4) => Permanent Employee Name
$cell= $sheets->Cells(5,4);
$cell->activate;
$cell->value= $full_name;

// More cells

$sheets->Calculate;

$Directory= _Dir ."po_excel/" . $SessionID . ".xls";

#save
$excel->Workbooks&#1111;1]->SaveAs($Directory);

#close the application
$excel->Quit();

unset($sheets);
unset($cell);
unset($workbook);

unset($excel);
And this works perfectly. Of course, you need to have excel on your webserver for this to work in the first place...