Advertisement
cdsatrian

generate xls

May 21st, 2014
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.69 KB | None | 0 0
  1. <?php
  2. include('include/conn.php');
  3. $akun_id = isset($_GET['akun_id'])?$_GET['akun_id']:'1';
  4. $bulan = isset($_GET['bulan'])?$_GET['bulan']:date('m');
  5. $cabang= isset($_GET['cabang'])?$_GET['cabang']:'admin';
  6. $tahun = isset($_GET['tahun'])?$_GET['tahun']:date('Y');
  7. // nama file
  8. $namaFile = "laporan_arus_kas.xls";
  9. // Function penanda awal file (Begin Of File) Excel
  10. function xlsBOF() {
  11.     echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
  12.     return;
  13. }
  14. // Function penanda akhir file (End Of File) Excel
  15. function xlsEOF() {
  16.     echo pack("ss", 0x0A, 0x00);
  17.     return;
  18. }
  19. // Function untuk menulis data (angka) ke cell excel
  20. function xlsWriteNumber($Row, $Col, $Value) {
  21.     echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
  22.     echo pack("d", $Value);
  23.     return;
  24. }
  25. // Function untuk menulis data (text) ke cell excel
  26. function xlsWriteLabel($Row, $Col, $Value) {
  27.     $L = strlen($Value);
  28.     echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
  29.     echo $Value;
  30.     return;
  31. }
  32. header("Cache-Control: no-cache, no-store, must-revalidate");
  33. header("Content-Type: application/vnd.ms-excel");
  34. header("Content-Disposition: attachment; filename=laporan_arus_kas.xls");
  35. // memanggil function penanda awal file excel
  36. xlsBOF();
  37. // ------ membuat kolom pada excel --- //
  38. //mengisi pada cell A1 (baris ke-0, kolom ke-0)
  39. xlsWriteLabel(0, 0, "LAPORAN ARUS KAS TAHUN " . $tahun . " BULAN " . $bulan);
  40. // -------- menampilkan data --------- //
  41. // koneksi ke mysql
  42. mysql_connect("localhost", "root", "");
  43. mysql_select_db("akuntansix");
  44. // query menampilkan semua data
  45. $query = mysql_query(
  46.            "SELECT
  47.             SUM(IF(`akun`.`klasifikasi_id`='1' OR `akun`.`klasifikasi_id`='1',
  48.                `jurnal`.`debit`-`jurnal`.`kredit`,
  49.                `jurnal`.`kredit`-`jurnal`.`debit`)) AS saldo
  50.           FROM
  51.             `akun`
  52.           INNER JOIN `jurnal`
  53.             ON `jurnal`.`akun_id` =`akun`.`id`
  54.           INNER JOIN `transaksi`
  55.             ON `jurnal`.`transaksi_id` =`transaksi`.`id`
  56.           WHERE
  57.             `akun`.`id` = '$akun_id'
  58.             AND transaksi.username='$cabang'
  59.             AND MONTH(transaksi.tanggal) < '$bulan'
  60.             AND YEAR(transaksi.tanggal) = '$tahun'",
  61.           $conn) or die(mysql_error());
  62. $ra = mysql_fetch_array($query);
  63. $sa=$ra['saldo'];
  64. // nilai awal untuk baris cell
  65. xlsWriteLabel(2, 0, "NOTA");
  66. xlsWriteLabel(2, 1, "TANGGAL");
  67. xlsWriteLabel(2, 2, "KODE");
  68. xlsWriteLabel(2, 3, "KETERANGAN");
  69. xlsWriteLabel(2, 4, "DEBIT");
  70. xlsWriteLabel(2, 5, "KREDIT");
  71. xlsWriteLabel(2, 6, "SALDO");
  72. xlsWriteLabel(3, 0, "Saldo awal (saldo bulan sebelumnya)");
  73. xlsWriteNumber(3, 6, $sa);
  74. $noBarisCell = 4;
  75. // nilai awal untuk nomor urut data
  76. $noData = 1;
  77. $query = mysql_query("SELECT
  78.           `transaksi`.`tanggal`,
  79.           `akun`.`kode`,
  80.           `transaksi`.`keterangan`,
  81.           `akun`.`nama`,
  82.           IF(`transaksi`.`jenis`='KM',
  83.              IF(`akun`.`klasifikasi_id`='1' OR `akun`.`klasifikasi_id`='1',
  84.                `jurnal`.`debit`-`jurnal`.`kredit`,
  85.                `jurnal`.`kredit`-`jurnal`.`debit`),
  86.              IF(`akun`.`klasifikasi_id`='1' OR `akun`.`klasifikasi_id`='1',
  87.                `jurnal`.`kredit`-`jurnal`.`debit`,
  88.                `jurnal`.`debit`-`jurnal`.`kredit`)) AS saldo,
  89.           `transaksi`.`jumlah`,
  90.           `transaksi`.`jenis`
  91.         FROM
  92.           `akun`
  93.         INNER JOIN `jurnal`
  94.            ON `jurnal`.`akun_id` =`akun`.`id`
  95.         INNER JOIN `transaksi`
  96.            ON `jurnal`.`transaksi_id` = `transaksi`.`id`
  97.         WHERE
  98.           `transaksi`.`username` = '$cabang'
  99.           AND MONTH(transaksi.tanggal)='$bulan'
  100.           AND YEAR(transaksi.tanggal)='$tahun'
  101.         GROUP BY `transaksi`.`keterangan`
  102.         ORDER BY `jurnal`.`akun_id` ASC",
  103.         $conn) or die(mysql_error());
  104. $saldo = $sa;
  105. while ($data = mysql_fetch_array($query)) {
  106.   $saldo+=$data['saldo'];
  107. // menampilkan data nim
  108.   xlsWriteLabel($noBarisCell, 0, $noData);
  109. // menampilkan data nilai
  110.   xlsWriteLabel($noBarisCell, 1, date("Y-m-d", strtotime($data['tanggal'])));
  111. // menampilkan data nilai
  112.   xlsWriteLabel($noBarisCell, 2, $data['kode']);
  113. // menampilkan data nilai
  114.   xlsWriteLabel($noBarisCell, 3, $data['keterangan']);
  115. // menampilkan data nilai
  116.   if($data["jenis"] == "KM"){
  117.     xlsWriteNumber($noBarisCell, 4, $data['jumlah']);
  118.     xlsWriteNumber($noBarisCell, 5, 0);
  119.   }else{
  120.     xlsWriteNumber($noBarisCell, 4, 0);
  121.     xlsWriteNumber($noBarisCell, 5, $data['jumlah']);
  122.   }
  123.   xlsWriteNumber($noBarisCell, 6, $saldo);
  124. // increment untuk no. baris cell dan no. urut data
  125.   $noBarisCell++;
  126.   $noData++;
  127. }
  128. // memanggil function penanda akhir file excel
  129. xlsEOF();
  130. exit();
  131. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement