houssam_ballout Posted July 8, 2011 Share Posted July 8, 2011 Hello, I need help to export or create an excel file through php. The data are being collected from mysql table. Thanks in advance Quote Link to comment Share on other sites More sharing options...
jcbones Posted July 8, 2011 Share Posted July 8, 2011 I would suggest the first link Quote Link to comment Share on other sites More sharing options...
IrOnMaSk Posted July 8, 2011 Share Posted July 8, 2011 is your output going to be display as webpage? one of the best ways is do the read/write to file if you only need specific data from the table... select the data then write it to file... Super simple instruction here, might just do the trick http://www.w3schools.com/php/php_file.asp goodluck Quote Link to comment Share on other sites More sharing options...
houssam_ballout Posted July 9, 2011 Author Share Posted July 9, 2011 no need to display it on webpage, just export it as a file, then send it back via php mail Quote Link to comment Share on other sites More sharing options...
jcbones Posted July 9, 2011 Share Posted July 9, 2011 I posted a link to a tutorial that fit your request exactly. Quote Link to comment Share on other sites More sharing options...
houssam_ballout Posted July 9, 2011 Author Share Posted July 9, 2011 well, its nice tutorial How can I save the file to my server? Thanks Quote Link to comment Share on other sites More sharing options...
jcbones Posted July 10, 2011 Share Posted July 10, 2011 Well, you take his code snippet: <?PHP function cleanData($str) { $str = preg_replace("/\t/", "\\t", $str); $str = preg_replace("/\r?\n/", "\\n", $str); } header("Content-Type: text/plain"); $flag = false; foreach($data as $row) { if(!$flag) { # display field/column names as first row echo implode("\t", array_keys($row)) . "\r\n"; $flag = true; } array_walk($row, 'cleanData'); echo implode("\t", array_values($row)) . "\r\n"; } ?> And make some changes: <?PHP function cleanData($str) { $str = preg_replace("/\t/", "\\t", $str); $str = preg_replace("/\r?\n/", "\\n", $str); } //header("Content-Type: text/plain"); //<-lose the header function. $filename = 'excel_export.xls'; $output = NULL; //define our holding variable. $flag = false; foreach($data as $row) { if(!$flag) { # display field/column names as first row $output .= implode("\t", array_keys($row)) . "\r\n"; //set this row to our variable. $flag = true; } array_walk($row, 'cleanData'); $output .= implode("\t", array_values($row)) . "\r\n"; //set this row to our variable. } file_put_contents($filename,$output); //write the output to a file; ?> Quote Link to comment Share on other sites More sharing options...
houssam_ballout Posted July 10, 2011 Author Share Posted July 10, 2011 in the array where can I put data. The data are in mysql table, when I do the while() to get the info from table, how can I add array dynamically to hold all data from that table,, Thx Quote Link to comment Share on other sites More sharing options...
jcbones Posted July 11, 2011 Share Posted July 11, 2011 Well, I guess that was on me, as I thought you knew how to populate an array from the database. Being that I don't know your database structure, I will give you arbitrary code. <?php include 'database_connection.php'; $sql = "SELECT * FROM table"; $result = mysql_query($sql) or trigger_error($sql . ' has an error.<br/>' . mysql_error()); if(mysql_num_rows($result) > 0) { while($row = mysql_fetch_assoc($result)) { $data[] = $row; } } if(!is_array($data)) { exit('There is no data to create an excel file from'); } function cleanData($str) { $str = preg_replace("/\t/", "\\t", $str); $str = preg_replace("/\r?\n/", "\\n", $str); } //header("Content-Type: text/plain"); //<-lose the header function. $filename = 'excel_export.xls'; $output = NULL; //define our holding variable. $flag = false; foreach($data as $row) { if(!$flag) { # display field/column names as first row $output .= implode("\t", array_keys($row)) . "\r\n"; //set this row to our variable. $flag = true; } array_walk($row, 'cleanData'); $output .= implode("\t", array_values($row)) . "\r\n"; //set this row to our variable. } file_put_contents($filename,$output); //write the output to a file; ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.