PHP Export ke Excel dan Word

Export ke Microsoft Excel

Untuk bisa mengexport ke format Excel dari PHP menggunakan script :

header(“Content-type: application/octet-stream”);
header(“Content-Disposition: attachment; filename=namafile.xls“);
header(“Pragma: no-cache”);
header(“Expires: 0”);

Script di atas letakkan di baris atas dalam file  php sebelum menampilkan content /isi file

sebagai contoh untuk export sebuah data table :

<?php

$nama_file = “namafilekita.xls”;

header(“Content-type: application/octet-stream”);
header(“Content-Disposition: attachment; filename=”.$nama_file);
header(“Pragma: no-cache”);
header(“Expires: 0″);

?>

<table border=”0″ width=”100%” cellpadding=”0″ cellspacing=”0″>
<tr>
<th width=”28″>No</th>
<th width=”150″>Nama Karyawan</th>
<th width=”70″>Departement</th>
</tr>
<tr>
<td>1.</td>
<td nowrap=”nowrap”>Mahrizal</td>
<td nowrap=”nowrap”>IT</td>
</tr>
<tr>
<td>2.</td>
<td nowrap=”nowrap”>Wawan</td>
<td nowrap=”nowrap”>IT</td>
</tr>
</table>

Untuk table bisa diganti dengan query mysql kita

maka ketika akan dijalankan akan menghasilkan file bentuk excel dengan nama ‘namafile.xls’

Export ke Microsoft Word

Sedangkan untuk Microsoft word dengan memakai script :

 header(“Content-Type: application/vnd.ms-word”);
header(“Expires: 0”);
header(“Cache-Control: must-revalidate, post-check=0, pre-check=0”);
header(“Content-disposition: attachment; filename=namafile.doc“);

Penggunaannya sama seperti di atas diletakkan di atas baris dalam file sebelum menampilkan content

selanjutnya bisa dimodifikasi sesuai gaya kita , sebagai contoh modifikasi dari contoh di atas :

<?php

$content =  ‘<table border=”0″ width=”100%” cellpadding=”0″ cellspacing=”0″>
<tr>
<th width=”28″>No</th>
<th width=”120″>Nama Karyawan</th>
<th width=”70″>Departement</th>
</tr>
<tr>
<td>1.</td>
<td nowrap=”nowrap”>Mahrizal</td>
<td nowrap=”nowrap”>IT</td>
</tr>
<tr>
<td>2.</td>
<td nowrap=”nowrap”>Wawan</td>
<td nowrap=”nowrap”>IT</td>
</tr>
</table>’;

$nama_file = “namafile.doc”;

header(“Content-Type: application/vnd.ms-word”);
header(“Expires: 0”);
header(“Cache-Control: must-revalidate, post-check=0, pre-check=0”);
header(“Content-disposition: attachment; filename=”.$nama_file);

echo $content;
?>

Maka ketika dijalankan akan menghasilkan file word dengan nama : ‘namafile.doc’

Referensi :

http://www.daniweb.com/web-development/php/threads/124300/how-to-php-export-to-excel

http://phpcodesearch.wordpress.com/2010/12/23/php-export-to-word/

 

15 pemikiran pada “PHP Export ke Excel dan Word

  1. Assalamu’alaikum . Wah trimakasih mas tutorialnya . . . 🙂
    Tapi ada sedikit error ketika saya buka file hasil export tadi, terdapat pesan error ” The file you are trying to open ,’namafile.xls’, is different format than specified . . . . . . . ”
    😦

    Suka

    • Waalaikumsalam
      oh ada error ya
      saya tadi nyoba sih normal ke excel atau ke word
      memang tadi ada script lain di tulisan paling bawah
      sekarang sudah saya hilangkan

      btw terima kasih ya mas Junaidi atas informasinya

      Suka

  2. terima kasih mas atas infonya. sangat membantu sekali. saya nyari referensi dari web luar kurang ok . hehehe

    ijin copas ya ke blog saya .:D

    Suka

  3. thx mass,, oya mau tambahin dikit buat yg masih newbie seperti saya, kalaw ada erorr dari scrib di atas gak usah kwatir hapus aja tanda petiknya semua dan ganti dengan tanda petik yang baru,,, !
    semoga membantu,,,,,,,,,,,,!

    Suka

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout /  Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout /  Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout /  Ubah )

Connecting to %s