Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting data from a database into Excel spreadsheet

Status
Not open for further replies.

GigaG

Technical User
Aug 28, 2007
83
US
Does anyone know or could you please point me in the correct direction of how I would go about taking queried data from a mysql database and insert it nicely into an excl spreadsheet??

Any assistance is appreciated

MCP ACA-I CTP
 
You can easily create a CSV file and have that opened in excel.

You would need to use the fopen and fwrite functions to write the information, and then deliver that to the browser:

Code:
$filename="filename.csv";
$handle=fopen($filename,"w");
fwrite($handle,$commaseparatedcontents]);
fclose($handle);

header('Content-type: application/xls');
header('Content-Disposition: attachment; filename="filename.csv"');
readfile($filename);


Something like that will take the contents of a variable write it into a file and deliver it to the browser. It can then be opened by Excel.


Its just a matter of getting your database records ordered and comma separated into the variable


----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thank u very much. ok so I get what your saying, but now how in my variable comma do I define the table by rows and columns?

MCP ACA-I CTP
 
output it as html table. save the resultant html and then import it into excel.

that's the easiest way.

there are other ways that are more direct, including the csv method mentioned by vacunita.
 
When you read your information for your DB you take each row value and insert and concatenate it into your variable for example:


Code:
while (you are running through your DB){
$myvariable.="$row['fieldname1'][red],[/red]$row['fieldname2'][red],[/red]...";
}



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Also... I did just try as you mentioned and it is not spitting it out the the browser as an excel sheet to download... it is only echoing it out to the browser

MCP ACA-I CTP
 
jpadie.... thank you for the response but i am looking to create a automated process that a user can do on their own through an activate button... that would be too many steps for a user to create human error

MCP ACA-I CTP
 
Who's method are you referring to, mine or jpadie's?

And can you describe in more detail what you did?

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Yours.... ok I just tested a few things... my exact programing is below:

<?
$commaseparatedcontents='1,2,3';
$filename="filename.csv";
$handle=fopen($filename,"w");
fwrite($handle,$commaseparatedcontents);
fclose($handle);
header('Content-type: application/xls');
header('Content-Disposition: attachment; filename="filename.csv"');
readfile($filename);


?>

While running this script... it echos it out the browser, but then there is nothing that pops up that it wll download it as a file... Not sure if it is possible to do this or not... also when i do a save as... it saves it as filename_csv.txt file . If I rename this to filename.csv.... it works fine, but i would rather the user not to have to do this

MCP ACA-I CTP
 
you can mix the two methods. the automation is caused by the header content-type to application/vnd.ms-excel (or other valid excel related content-type) and the formatting is done either by a comma delimited file (make sure you are using string delimiters too) or my outputting a formatted html table. either way, if excel is properly installed, the browser should launch it or open a file save prompt.
 
I just copied and pasted your code,and ran it as it was, and I get a download prompt in both Firefox ands IE7.

There might be something wrong with your excel installation, or your browser is configured to know what to do with files of type CSV and opens them directly.

The question being does it open it just like text, or does it open the excel in-browser plug in?




----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Just text... it is showing in IE7 like a regular web page with 1,2,3... Now i did just try Firefox and it did try to download it... so what gives with IE7?? Any ideas? Also.... would I just start a new row with a new variable?

MCP ACA-I CTP
 
ok i got the download issue... it's tabbing i guess... dunno why, but when I opened a new window it tried to download it.... stupid IE

MCP ACA-I CTP
 
Not really, you can just concatenate the new row to the new variable, making sure to use an end of line character. such as "\r\n" to terminate a row.

Using the while loop, will automatically concatenate the following row. but don't forget the period before the equal sign and don;t forget to initialize your variable before suing it inside your loop.

Code:
$contents[red].[/red]="$row['fieldname1'],$row['fieldame2']...\r\n";




----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Not really, you can just concatenate the new row to the new variable,
I meant the new row to the same variable. Not new variable.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Ok so another thing I was woundering.... is there a way to do anything fancy such as highlighting or bolding things in the excel spreadsheet with php?? And also the question about multiple rows

MCP ACA-I CTP
 
Hmmm.... ok well i did this:

$commaseparatedcontents.='1,2,3,';
$commaseparatedcontents.='4,5,6,7';

and in my excel sheet instead of getting

123
4567

I'm getting:

1234567

Where am i going wrong??

MCP ACA-I CTP
 
making sure to use an end of line character. such as "\r\n" to terminate a row.

And use double quotes, around your contents.

In other words:
Code:
$commaseparatedcontents.="1,2,3[red]\r\n[/red]";
$commaseparatedcontents.="4,5,6,7";

And sorry no fancy things. if you want bolding or color, you'll need to use HTML and send that to excel.



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thats ok... thanks for the fix... I knew it was something simple i was missing and I completely over looked what you said about the delimiters... now as for the HTML... how would you "send that the excel"?

MCP ACA-I CTP
 
Try this:
Code:
$commaseparatedcontents.="<table><tr><td><font color=#FF0000>Red</font></td><td><font color=#0000FF>Blue</font></td></tr><tr><td><b>Bold</bold></td><td>Normal</td></tr></table>";




----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top