Script PHP untuk export Query MySQl ke Excel

ini script mencetak script php dengan database mysql ke  Ms.Excel

<form id=”form1″ name=”form1″ method=”post” action=”excel.php”>
<p>
QUERY : <br />
<textarea name=”sql” cols=”50″ rows=”6″ id=”sql”></textarea>
</p>
<p>
<input type=”submit” name=”Submit” value=”Submit” />
</p>
</form>
<p style=”text-align: justify;”><strong>Nama File : excel.php</strong>
<?php
$hostname = “localhost”;
$username = “root”;
$database = “a”;
$password = “”;
$conn = mysql_connect($hostname,$username,$password);
mysql_select_db($database);

if (isset($_POST['sql']))
{
$q = mysql_query(stripslashes($_POST['sql'])) or die (“Not valid query !”);
$res = “”;
while ($f = mysql_fetch_field($q)) {
$res .= $f->name . “\t”;
}
$res .= “\r\n”;
while ($r = mysql_fetch_array($q)) {
for ($i=0; $i<mysql_num_fields($q); $i++) {
$res .= $r[$i] . “\t”;
}
$res .= “\r\n”;
}
mysql_free_result($q);
header(“Content-disposition: attachment; filename=aan.xls”);
header(“Content-type: Application/exe”);
header(“Content-Transfer-Encoding: binary”);
echo $res;
}
?>

script ini untuk melakukan koneksi dengan database my sql
$hostname = “localhost”; // deklarasikan host komputer anda
$username = “root”; // deklarasikan user database
$database = “___”;    // deklarasikan nama databasenya disini
$password = “”;  // deklarasikan user database
$conn = mysql_connect($hostname,$username,$password);
mysql_select_db($database);

script mencetak php ke ms excel
header(“Content-disposition: attachment; filename=____.xls”); // diisi nama file ms.excel
header(“Content-type: Application/exe”);
header(“Content-Transfer-Encoding: binary”);

link asal

Categories: PHP | Leave a comment

Post navigation

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com. The Adventure Journal Theme.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: