table to excel

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nikko50
Forum Commoner
Posts: 43
Joined: Thu Apr 08, 2004 6:28 am

table to excel

Post 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";
}
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
nikko50
Forum Commoner
Posts: 43
Joined: Thu Apr 08, 2004 6:28 am

Post 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";
}
}
?>
ckuipers
Forum Commoner
Posts: 61
Joined: Mon Mar 24, 2003 6:10 am

Post 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...
Post Reply