PDA

View Full Version : [PHP] Esportare MySql in Excel


ShadowThrone
19-01-2016, 22:40
Premessa: non sono un programmatore, ma per necessità devo esportare delle tabelle MySql in Excel. Per fare ciò ho creato due file ed il tutto funziona, peccato che il file excel si porti dietro anche il form html.

http://s16.postimg.org/62etw9lgl/problem.png

INDEX.PHP
<?php
$con = mysql_connect("IP","user","pwd");
$db = mysql_select_db("DB",$con);
$get = mysql_query("SHOW TABLES FROM DB");
$option = '';
while($row = mysql_fetch_array($get))
{
$option .= '<option value = "'.$row[0].'">'.$row[0].'</option>';
}
?>
<html>
<body>
<form action="#" method="post">
<h3>Select table</h3>
<select name="table">
<?php echo $option; ?>
</select>
<input type="submit" name="submit" value="Get Table" />
</form>
<?php
if(isset($_POST['submit']))
{
$selected_val = $_POST['table'];
echo "You have selected :" .$selected_val;
$myFile="/tmp/".test;
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $selected_val);
fclose($fh);
include 'SelectTable.php';
}
?>
</body>
</html>


SELECTABLE.PHP
<?php
header("Content-type: application/vnd-ms-excel");
header("Content-Disposition: attachment; filename=TEST.xls");
$tablesel=file_get_contents("/tmp/".test.txt);
$result = mysql_query('SELECT Time, Temp1, Temp2, Temp3, Temp4 FROM '.$tablesel) or die('cannot show columns from '.$tablesel);
if(mysql_num_rows($result))
{
echo '<table cellpadding="0" cellspacing="0" class="db-table">';
echo '<tr><th>Time</th><th>Temp1</th><th>Temp2</th><th>Temp3</th><th>Temp4</tr>';
while($row2 = mysql_fetch_row($result))
{
echo '<tr>';
foreach($row2 as $key=>$value)
{
echo '<td>',$value,'</td>';
}
echo '</tr>';
}
echo '</table><br />';
}
?>

deffe2
20-01-2016, 20:53
Non ho capito se il form risulta nel txt o nell Excel in ogni caso potresti invece che generare ogni volta un txt passare i dati del form tramite $_get ora sono dal cellulare ma domani sarò più dettagliato

deffe2
21-01-2016, 12:25
<?php
session_start();
$con = mysql_connect("IP","user","pwd");
$db = mysql_select_db("DB",$con);
$get = mysql_query("SHOW TABLES FROM DB");
$option = '';
while($row = mysql_fetch_array($get))
{
$option .= '<option value = "'.$row[0].'">'.$row[0].'</option>';
}
?>
<html>
<body>
<form action="#" method="post">
<h3>Select table</h3>
<select name="table">
<?php echo $option; ?>
</select>
<input type="submit" name="submit" value="Get Table" />
</form>
<?php
if(isset($_POST['submit']))
{
$selected_val = $_POST['table'];
echo "You have selected :" .$selected_val;
$_SESSION['id'] = $selected_val;
include 'SelectTable.php';
}
?>
</body>
</html>




<?php
header("Content-type: application/vnd-ms-excel");
header("Content-Disposition: attachment; filename=TEST.xls");

$tablesel=$_SESSION['id'];
$result = mysql_query('SELECT Time, Temp1, Temp2, Temp3, Temp4 FROM '.$tablesel) or die('cannot show columns from '.$tablesel);
if(mysql_num_rows($result))
{
echo '<table cellpadding="0" cellspacing="0" class="db-table">';
echo '<tr><th>Time</th><th>Temp1</th><th>Temp2</th><th>Temp3</th><th>Temp4</tr>';
while($row2 = mysql_fetch_row($result))
{
echo '<tr>';
foreach($row2 as $key=>$value)
{
echo '<td>',$value,'</td>';
}
echo '</tr>';
}
echo '</table><br />';
}
?>

les2
21-01-2016, 14:57
magari ci hai già pensato ma per farla facile con dati da importare in excel di solito si usa il formato csv.
di fatto generi un txt dove scrivi le rige (e vai a capo) e dividi le colonne usando ";" come divisore.
veloce, facile, performante e puoi scriverlo come ti pare (oggetti, procedurale, da schifo etc... :D)

spero di averti dato una buona dritta ;)