cdukes
asked on
Spreadsheet/Excel/Writer.php and MySQL
I'm trying to write code that will output to excel based on an SQL query using the pear excel/writer function.
The following code only returns a blank excel file:
I'd also like to allow for an incoming array such as:
for id in @array, output to spreadsheet
Basically, I have a table listing ID, IP, HOST, DOMAIN and need a way to export to excel.
For now, I'm just dumping all (via select *), but if you know how, I'd like to do a select * from where id=[array of id's]
Thanks!
if ($export == 1) {
require_once "Spreadsheet/Excel/Writer. php";
mysql_connect($db_host,$my sql_userna me,$mysql_ password) or die (mysql_error());
mysql_select_db($mysql_db) or die(mysql_error());
$select = "SELECT ip,hostname,domainname FROM $mysql_table";
$export = mysql_query($select) or die (mysql_error());
$numfields = mysql_num_fields($export) or die (mysql_error());
$xls =& new Spreadsheet_Excel_Writer() ;
// Send HTTP headers to tell the browser what's coming
$xls->send("$mysql_table.x ls");
// Add a worksheet to the file, returning an object to add data to
$sheet =& $xls->addWorksheet("$mysql _table");
// Write some numbers
for ($i = 0; $i < $numfields; $i++) {
// Write data
$sheet->write($i,1,$export [$i]);
}
// Finish the spreadsheet, dumping it to the browser
$xls->close();
} else {
The following code only returns a blank excel file:
I'd also like to allow for an incoming array such as:
for id in @array, output to spreadsheet
Basically, I have a table listing ID, IP, HOST, DOMAIN and need a way to export to excel.
For now, I'm just dumping all (via select *), but if you know how, I'd like to do a select * from where id=[array of id's]
Thanks!
if ($export == 1) {
require_once "Spreadsheet/Excel/Writer.
mysql_connect($db_host,$my
mysql_select_db($mysql_db)
$select = "SELECT ip,hostname,domainname FROM $mysql_table";
$export = mysql_query($select) or die (mysql_error());
$numfields = mysql_num_fields($export) or die (mysql_error());
$xls =& new Spreadsheet_Excel_Writer()
// Send HTTP headers to tell the browser what's coming
$xls->send("$mysql_table.x
// Add a worksheet to the file, returning an object to add data to
$sheet =& $xls->addWorksheet("$mysql
// Write some numbers
for ($i = 0; $i < $numfields; $i++) {
// Write data
$sheet->write($i,1,$export
}
// Finish the spreadsheet, dumping it to the browser
$xls->close();
} else {
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Here's what I ended up with in case anyone else ever needs it:
[code]
if ($export == 1) {
require_once "Spreadsheet/Excel/Writer.
// $idarr = "1,2";
mysql_connect($db_host,$my
mysql_select_db($mysql_db)
if ( $idarr ) {
$select = "SELECT * from $mysql_table where FIND_IN_SET(id, '$idarr')>0;";
} else {
$select = "SELECT * from $mysql_table";
}
$export = mysql_query($select) or die (mysql_error());
$numfields = mysql_num_fields($export) or die (mysql_error());
$xls =& new Spreadsheet_Excel_Writer()
// Send HTTP headers to tell the browser what's coming
$xls->send("$mysql_table.x
// Add a worksheet to the file, returning an object to add data to
$sheet =& $xls->addWorksheet("$mysql
// Write data
// $sheet->write($rownum,$col
// Top Column Names
$rownum = 0;
$colnum = 0;
$sheet->write($rownum,$col
$colnum++;
$sheet->write($rownum,$col
$colnum++;
$sheet->write($rownum,$col
$rownum = 1;
// Row Data
while ($row = mysql_fetch_assoc($export)
$colnum = 0;
$sheet->write($rownum,$col
$colnum++;
$sheet->write($rownum,$col
$colnum++;
$sheet->write($rownum,$col
$rownum++;
}
// Finish the spreadsheet, dumping it to the browser
$xls->close();
} else {
[/code]
...rest of the page code