Advertisement
cdsatrian

phpexcel report r1

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