Jump to content

Exporting database to Excel (via website, NOT phpMyAdmin)?


Edward

Recommended Posts

 

Hi,

 

I know via phpMyAdmin I can export my database in Microsoft Excel 2000 format, and can check a box to 'Create field names as first row', but it doesn't give me the php code used to achieve this. I need to have a page on a website so the administrators will be able to do this for themselves, does anyone know if this is possible?

 

Thanks in advance...

Link to comment
Share on other sites

 

phpMyAdmin doesn't give me the option of viewing the source code for this operation. The phpfreaks tutorial on this has been removed (http://www.phpfreaks.com/tutorials/114/0.php) and most Google searches eventually lead back too that. Can anyone offer any help?

 

What I mean is, Get phpmyadmin, and view it's source files(the .php ones)

Link to comment
Share on other sites

try

<form method="POST">
Database name: <input type="text" name="db_name" value="test"><br />
Table name: <input type="text" name="table"><br >
Replace NULL by <input type="text" name="nul" value="NULL"><br />
<input type="checkbox" name="header_row"> Put fields names at first row<br />
Field name: <input type="text" name="field" value="test">.xls<br />
<input type="submit" name="Submit" value="Convert to Excel 2000">
</form>
<?php
function convert_to_xls($table_name, $first_row = false, $data_base = 'test', $null_to = 'NULL') {
mysql_connect('localhost','root');
$result = mysql_query('show databases');
$test = false;
while ($row = mysql_fetch_row($result)){
	if ($row[0] == $data_base) $test = true;
}
if (!$test){
	die('Not exsist DB');
}
mysql_select_db($data_base);
$result = mysql_query('show tables');
$test = false;
while ($row = mysql_fetch_row($result)){
	if ($row[0] == $table_name) $test = true;
}
if (!$test){
	die('Not exsist table: '. $table_name);
}
$out = "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\"
xmlns:x=\"urn:schemas-microsoft-com:office:excel\"
xmlns=\"http://www.w3.org/TR/REC-html40\">

<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<html>
<head>
<meta http-equiv=\"Content-type\" content=\"text/html;charset=utf-8\" />
<style id=\"Classeur1_16681_Styles\">
</style>

</head>
<body>

<div id=\"Classeur1_16681\" align=center x:publishsource=\"Excel\">

<table x:str border=0 cellpadding=0 cellspacing=0 width=100% style='border-collapse: collapse'>\n";
$result = mysql_query('describe '.$table_name);
 $num_types = array( 'NUMERIC', 'DECIMAL', 'INTEGER', 'SMALLINT', 'FLOAT', 'REAL', 'DOUBLE PRECISION');
$out .=$first_row ? "<tr>\n" : '';
while ($row = mysql_fetch_array($result)){
	$f_name[] = $row['Field'];
	$out .=$first_row ? '<td class=xl2216681 nowrap><b>'.$row['Field'].'</b></td>'."\n" : '';
}
$out .= $first_row ? "</tr>\n" : '';
$result = mysql_query('select * from '.$table_name);
while ($row = mysql_fetch_array($result)){
	$out .= '<tr>';
	foreach ($f_name as $k) $out .= '<td class=xl2216681 nowrap>'. ($row[$k]== null ? $null_to : $row[$k] ).'</td>'."\n";
	$out .= "</tr>\n";
}
return $out .= '</table>
</div>
</body>
</html>';
}
if ($_POST['Submit']){
$hr = isset($_POST['header_row']) ? true : false;
$file = $_POST['field'];
if ($file){
	$file .= eregi('.xls',$_POST['field']) ? '' : '.xls';
	$fp = fopen($file, 'w');
	fwrite($fp ,convert_to_xls($_POST['table'],$hr,$_POST['db_name'],$_POST['nul']));
	fclose($fp);
	echo '<hr /><a href="',$file,'">FILE</a>';
} else echo '<hr />No file name';
//echo convert_to_xls($_POST['table'],$hr,$_POST['db_name'],$_POST['nul']);
}
?>

Link to comment
Share on other sites

I´ve been following this thread being redirected from another board on this forum. My question is, i can get it working if i want to have the full table into an xls file. But if i want to select some fields from a table and passing them  into an xls file it does not work properly, because i can´t get the name of the fields properly.

 

The query that selects me some fields is

 



$query = " SELECT " .$select_cols . " FROM " .$_SESSION["nomtab"]. " ";
$result = mysql_query($query); 

 

where select_cols contains the name of the fields i want to get and nomtab is the name of the table.

 

My modification to the great code provided before is this :

 


$query = " SELECT " .$select_cols . " FROM " .$_SESSION["nomtab"]. " ";
$result = mysql_query($query); 
$num_types = array( 'NUMERIC', 'DECIMAL', 'INTEGER', 'SMALLINT', 'FLOAT', 'REAL', 'DOUBLE PRECISION');

$out .=$first_row ? "<tr>\n" : '';
while ($row = mysql_fetch_array($result)){
	$f_name[] = $row['Field'];
	$out .=$first_row ? '<td class=xl2216681 nowrap><b>'.$row['Field'].'</b></td>'."\n" : '';
}
$out .= $first_row ? "</tr>\n" : '';







$query = " SELECT " .$select_cols . " FROM " .$_SESSION["nomtab"]. " ";
$result = mysql_query($query); 
//echo mysql_errno($db) . ": " . mysql_error($db) . "\n";
while ($row = mysql_fetch_array($result)){
	$out .= '<tr>';
	foreach ($f_name as $k) $out .= '<td class=xl2216681 nowrap>'. ($row[$k]== null ? $null_to : $row[$k] ).'</td>'."\n";
	$out .= "</tr>\n";
}


 

But it does not work...please, can you help me?

Link to comment
Share on other sites

try

//query = " SELECT " .$select_cols . " FROM " .$_SESSION["nomtab"]. " ";
//$result = mysql_query($query); 
//$num_types = array( 'NUMERIC', 'DECIMAL', 'INTEGER', 'SMALLINT', 'FLOAT', 'REAL', 'DOUBLE PRECISION');

$out .=$first_row ? "<tr>\n" : '';
        $result = explode(',', $select_cols);  
//while ($row = mysql_fetch_array($result)){
        foreach($result as $field){
	$f_name[] = trim($field);;
	$out .=$first_row ? '<td class=xl2216681 nowrap><b>'.trim($field).'</b></td>'."\n" : '';
}
$out .= $first_row ? "</tr>\n" : '';

Link to comment
Share on other sites

Thanks a lot, it does work.

 

I´ve implemented your code in only one php file, extracting the names of the tables, cols and db from another previous forms. It does work properly.

 

Only one minor question. The table i want to pass has numbers. When i see the table in excel it shows me as a kind of error and i have to tell excel to treat some text as numbers. Is there a way to force excel to treat numbers as numbers?

Link to comment
Share on other sites

thanks it worked (i used the second option x:str in table and x:num in td ) perfectly.

 

I have one minor problem related maybe with  the language i spoke (spanish) where some words have ´ above some letters (accent ) like Málaga . When i see the string in table format, not xls , i see it properly, i see Málaga but when i pass the information to excel, it does not work like it should, showing something like Mᬡga.

 

Is there any way to solve this or it is a language issue? why it does not pass the string as it is and makes changes ?

 

 

Link to comment
Share on other sites

  • 9 months later...

hi! I'm also using namespace for generating excel files.. there are  several columns where data are formatted (i.e., there are <p>, <br/>, etc..) the problem is that when excel encounters <p> or <br/> tag it moves from the current cell to next row's cell.. Is there a way to prevent this?? I know that ALT+Enter makes it possible in MS Excel but how do I say ALT+Enter in my php code??

 

Thanks in advance!! :D

Link to comment
Share on other sites

hi! I'm also using namespace for generating excel files.. there are  several columns where data are formatted (i.e., there are <p>,

<br/>

, etc..) the problem is that when excel encounters <p> or

<br/>

tag it moves from the current cell to next row's cell.. Is there a way to prevent this?? I know that ALT+Enter makes it possible in MS Excel but how do I say ALT+Enter in my php code??

 

Thanks in advance!! :D

 

I think it is also worth mentioning that the data of the columns i'm referring to is somewhat of TEXT or LONG TEXT category in MySQL.. :D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.