0

i got this code from the internet to export mysql query to excel using php..i just modify it to meet my need. i need some help on this..thanks..

<?php
$DB_Server = "localhost"; 
$DB_Username = "root";    
$DB_Password = "";              
$DB_DBName = "dcde";        
$DB_TBLName = "po"; 
$filename = "filename";        

$sql = "Select poNo,date,supplier,tAmount,code from $DB_TBLName WHERE  code='2'";
$Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password) or die("Couldn't connect to MySQL:<br>" . mysql_error() . "<br>" . mysql_errno());

$Db = @mysql_select_db($DB_DBName, $Connect) or die("Couldn't select database:<br>" . mysql_error(). "<br>" . mysql_errno());   

$result = @mysql_query($sql,$Connect) or die("Couldn't execute query:<br>" . mysql_error(). "<br>" . mysql_errno());    
$file_ending = "xls";

header("Content-Type: application/xls");    
header("Content-Disposition: attachment; filename=$filename.xls");  
header("Pragma: no-cache"); 
header("Expires: 0");

$sep = "\t"; 

for ($i = 0; $i < mysql_num_fields($result); $i++) {
echo mysql_field_name($result,$i) . "\t";
}
print("\n");    

    while($row = mysql_fetch_row($result))
    {
        $schema_insert = "";
        for($j=0; $j<mysql_num_fields($result);$j++)
        {
            if(!isset($row[$j]))
                $schema_insert .= "NULL".$sep;
            elseif ($row[$j] != "")
                $schema_insert .= "$row[$j]".$sep;
            else
                $schema_insert .= "".$sep;
        }
        $schema_insert = str_replace($sep."$", "", $schema_insert);
        $schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
        $schema_insert .= "\t";
        print(trim($schema_insert));
        print "\n";
    }   
?>

attached are the two images..1st is the the output of this code..and the 2nd file is my desired output which is already formatted.. like 1) the name of the field is change, bold..2) column should be automatically adjusted to fit the entry 3)..additional entry like PREPARED: NAME

thanks a lot..

Attachments 1.png 8.12 KB 2.png 4.56 KB
3
Contributors
2
Replies
11
Views
3 Years
Discussion Span
Last Post by prateeksha
0

Hi

Use PEAR Spreadsheet Excel library. it is really cool. You can set the colour and width of the columns, add formulas etc.

It is easy to use and very versitle

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.