Advertisement
cdsatrian

phpexcelreport

Oct 30th, 2013
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.18 KB | None | 0 0
  1. <?php
  2. session_start();
  3. error_reporting(E_ALL);
  4. /** PHPExcel */
  5. include 'PHPExcel.php';
  6. include '../../conn.php';
  7. function chappsdatetime($waktu)
  8. {
  9.   if (isset($waktu))
  10.   {
  11.     $waktu_bulan= substr($waktu,5,2);
  12.     $bulan_aray= array('Januari','Februari','Maret','April','Mei','Juni','Juli','Agustus','September','Oktober','November','Desember');
  13.     return substr($waktu,8,2)." ".$bulan_aray[$waktu_bulan]." ".substr($waktu,0,4)." ".substr($waktu,11,8);
  14.   }
  15. }
  16.  
  17. //variabel
  18. $bulan = $_POST['bulan'];
  19. $thn = $_POST['thn'];
  20. $cab = "Semarang";
  21. //query perulangan sheet menurut jenis barang
  22. $sheet = 0;
  23. $sql="SELECT
  24.        nama
  25.      FROM
  26.        jenis_barang
  27.      ORDER BY
  28.        id_brg ASC"
  29. $jenis_brg = mysql_query($sql)or die(mysql_error());
  30. // Create new PHPExcel object
  31. $objPHPExcel = new PHPExcel();
  32. // Set properties
  33. $objPHPExcel->getProperties()->setCreator("Rifaqu Emprit")
  34.             ->setLastModifiedBy("Administrator")
  35.             ->setTitle("Office 2007 XLSX Test Document")
  36.             ->setSubject("Office 2007 XLSX Test Document")
  37.             ->setDescription("Laporan Barang Semarang");
  38. while ($nama = mysql_fetch_array($jenis_brg))
  39. {
  40.   if($sheet>0) $objPHPExcel->createSheet();
  41.   $objPHPExcel->setActiveSheetIndex($sheet);
  42.   //autosize
  43.   $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
  44.   $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
  45.   $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
  46.   $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
  47.   $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
  48.   $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true);
  49.   $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setAutoSize(true);
  50.   $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setAutoSize(true);
  51.   $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setAutoSize(true);
  52.   $objPHPExcel->getActiveSheet()->getColumnDimension('J')->setAutoSize(true);
  53.   $objPHPExcel->getActiveSheet()->getColumnDimension('K')->setAutoSize(true);
  54.   $objPHPExcel->getActiveSheet()->getColumnDimension('L')->setAutoSize(true);
  55.   $objPHPExcel->getActiveSheet()->getColumnDimension('M')->setAutoSize(true);
  56.   $objPHPExcel->getActiveSheet()->getColumnDimension('N')->setAutoSize(true);
  57.   //pembuatan sheet
  58.   $objPHPExcel->getActiveSheet()
  59.               ->SetCellValue('A7', 'No.')
  60.               ->SetCellValue('B7', 'Tangal Transaksi')
  61.               ->SetCellValue('C7', 'Pembelian')
  62.               ->SetCellValue('D7', 'Penjualan')
  63.               ->SetCellValue('E7', 'Stock Barang')
  64.               ->SetCellValue('F7', 'Harga Beli')
  65.               ->SetCellValue('G7', 'Harga Jual')
  66.               ->SetCellValue('H7', 'Laba Jual')
  67.               ->SetCellValue('I7', 'Total Laba Penjualan')
  68.               ->SetCellValue('J7', 'Total Pembelian')
  69.               ->SetCellValue('K7', 'Total Harga Jual')
  70.               ->SetCellValue('L7', 'Konsumen')
  71.               ->SetCellValue('M7', 'Alamat')
  72.               ->SetCellValue('N7', 'Keterangan');
  73.   //Membuat Garis atau tabel
  74.   $sharedStyle1 = new PHPExcel_Style();
  75.   $sharedStyle1->applyFromArray(
  76.                   array('borders' =>
  77.                     array(
  78.                      'bottom' => array('style' => PHPExcel_Style_Border::BORDER_THIN),
  79.                      'top' => array('style' => PHPExcel_Style_Border::BORDER_THIN),
  80.                      'right' => array('style' => PHPExcel_Style_Border::BORDER_MEDIUM),
  81.                      'left' => array('style' => PHPExcel_Style_Border::BORDER_MEDIUM)
  82.                     ),
  83.                   ));
  84.   //query transaksi
  85.   $baris = 6;
  86.   $no = 0;
  87.   $sql="SELECT *
  88.        FROM
  89.          transaksi
  90.        WHERE
  91.          kategori='".$nama['nama']."'
  92.          AND
  93.          tgl_transaksi LIKE '$thn-$bulan-%'";
  94.   $transaksi = mysql_query($sql) or die(mysql_error());
  95.   while($m=mysql_fetch_array($transaksi)){
  96.     ++$no;
  97.     ++$baris;
  98.     $tgl = chappsdatetime($m['tgl_transaksi']);
  99.     //isi
  100.     $objPHPExcel->getActiveSheet()
  101.                 ->setCellValue("A$baris", $no)
  102.                 ->setCellValue("B$baris", $tgl)
  103.                 ->setCellValue("C$baris", $m['masuk'])
  104.                 ->setCellValue("D$baris", $m['keluar'])
  105.                 ->setCellValue("E$baris", '')
  106.                 ->setCellValue("F$baris", $m['harga_beli'])
  107.                 ->setCellValue("G$baris", $m['harga_jual'])
  108.                 ->setCellValue("H$baris", '=SUM(G'.$baris.'-F'.$baris.')')
  109.                 ->setCellValue("I$baris", '=SUM(H'.$baris.'*D'.$baris.')')
  110.                 ->setCellValue("J$baris", '=SUM(C'.$baris.'*F'.$baris.')')
  111.                 ->setCellValue("K$baris", '=SUM(D'.$baris.'*G'.$baris.')')
  112.                 ->setCellValue("L$baris", $m['konsumen'])
  113.                 ->setCellValue("M$baris", $m['alamat'])
  114.                 ->setCellValue("N$baris", $m['ket']);
  115.    
  116.     $sharedStyle2 = new PHPExcel_Style();
  117.     $sharedStyle2->applyFromArray(
  118.                      array('borders' =>
  119.                        array(
  120.                         'bottom' => array('style' => PHPExcel_Style_Border::BORDER_THIN),
  121.                         'top' => array('style' => PHPExcel_Style_Border::BORDER_THIN),
  122.                         'right' => array('style' => PHPExcel_Style_Border::BORDER_MEDIUM),
  123.                         'left' => array('style' => PHPExcel_Style_Border::BORDER_MEDIUM)
  124.                        ),
  125.                      ));
  126.     $objPHPExcel->getActiveSheet()->setSharedStyle($sharedStyle2, "A$baris:N$baris");
  127.     $objPHPExcel->getActiveSheet()->getStyle("A$baris:H$baris")->getFont()->setName('Times');
  128.     $objPHPExcel->getActiveSheet()->getStyle("A$baris:H$baris")->getFont()->setSize(11);
  129.   }
  130.   // Mulai Merge cells Judul
  131.   $objPHPExcel->getActiveSheet()
  132.               ->mergeCells('A1:N1')
  133.               ->setCellValue('A1', "LAPORAN TRANSAKSI BAJA RINGAN")
  134.               ->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  135.   $objPHPExcel->getActiveSheet()->mergeCells('A2:N2');
  136.   $objPHPExcel->getActiveSheet()->setCellValue('A2', "CV. ANJASA");
  137.   $objPHPExcel->getActiveSheet()->getStyle('A2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  138.   $objPHPExcel->getActiveSheet()->mergeCells('A3:N3');
  139.   $objPHPExcel->getActiveSheet()->setCellValue('A3', "Jln. Tentara Pelajar No. 79");
  140.   $objPHPExcel->getActiveSheet()->getStyle('A3')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  141.   $objPHPExcel->getActiveSheet()->mergeCells('A5:N5');
  142.   $objPHPExcel->getActiveSheet()->setCellValue('A5', "Bulan $bulan Cabang $cab");
  143.   $objPHPExcel->getActiveSheet()->getStyle('A1:H1')->getFont()->setName('Times');
  144.   $objPHPExcel->getActiveSheet()->getStyle('A1:H1')->getFont()->setSize(12);
  145.   $objPHPExcel->getActiveSheet()->getStyle('A1:H1')->getFont()->setBold(true);
  146.   $objPHPExcel->getActiveSheet()->getStyle('A2:H2')->getFont()->setName('Times');
  147.   $objPHPExcel->getActiveSheet()->getStyle('A2:H2')->getFont()->setSize(12);
  148.   $objPHPExcel->getActiveSheet()->getStyle('A2:H2')->getFont()->setBold(true);
  149.   $objPHPExcel->getActiveSheet()->getStyle('A3:H3')->getFont()->setName('Times');
  150.   $objPHPExcel->getActiveSheet()->getStyle('A3:H3')->getFont()->setSize(12);
  151.   $objPHPExcel->getActiveSheet()->getStyle('A3:H3')->getFont()->setBold(true);
  152.   $objPHPExcel->getActiveSheet()->getStyle('A5:H5')->getFont()->setName('Times');
  153.   $objPHPExcel->getActiveSheet()->getStyle('A5:H5')->getFont()->setSize(12);
  154.   $objPHPExcel->getActiveSheet()->getStyle('A5:H5')->getFont()->setBold(true);
  155.   // Rename sheet
  156.   $objPHPExcel->getActiveSheet()->setTitle($nama['nama']);
  157.   $sheet++;
  158. }
  159. // Set active sheet index to the first sheet, so Excel opens this as the first sheet
  160. $objPHPExcel->setActiveSheetIndex(0);
  161. // Redirect output to a client’s web browser (Excel5)
  162. header('Content-Type: application/vnd.ms-excel');
  163. header('Content-Disposition: attachment;filename="Laporan Transaksi Barang-'.$bulan.$thn.'.xls"');
  164. header('Cache-Control: max-age=0');
  165. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  166. $objWriter->save('php://output');
  167. exit;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement