Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save menjaraz/11398364 to your computer and use it in GitHub Desktop.
Save menjaraz/11398364 to your computer and use it in GitHub Desktop.
<?php
require_once(VENDORS_PATH.DS.'PHPExcel'.DS.'PHPExcel.php');
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setCreator("Oka Prinarjaya")
->setLastModifiedBy("Oka Prinarjaya")
->setTitle("Test Export Data ke Dokumen Microsoft Excel");
$style = array(
'font' => array('bold' => true),
'alignment' => array('horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER)
);
$sheet = $objPHPExcel->setActiveSheetIndex(0);
$sheet->setCellValue('A1', 'Nama Item');
$sheet->setCellValue('B1', 'Kategori');
$sheet->setCellValue('C1', 'Supplier');
$sheet->setCellValue('D1', 'Stock');
$sheet->setCellValue('E1', 'Harga');
$startRow = 2;
foreach ($items as $item) {
$sheet->setCellValue('A'.$startRow, $item['PROD_NAME']);
$sheet->setCellValue('B'.$startRow, $item['CAT_NAME']);
$sheet->setCellValue('C'.$startRow, $item['SUPP_NAME']);
$sheet->setCellValue('D'.$startRow, $item['stock']);
$sheet->setCellValue('E'.$startRow, number_format($item['price']));
$startRow++;
}
$objPHPExcel->getActiveSheet()->getStyle('A1:E1')->applyFromArray($style);
$objPHPExcel->getActiveSheet()->getStyle('A1:E'.$startRow)->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment