I am using sample exel code and add using PDO to add data export excel
if (PHP_SAPI == 'cli')
die('This example should only be run from a Web Browser');
/** Include PHPExcel */
require_once dirname(__FILE__) . '/Classes/PHPExcel.php';
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
i am edit some properties
// Set document properties
$objPHPExcel->getProperties()->setCreator("Team IT KSP PRIMADANA")
->setLastModifiedBy("Team IT KSP PRIMADANA")
->setTitle("DATA NASABAH")
->setSubject("KSP PRIMADANA")
->setDescription("Data Perusahaan.")
->setKeywords("office 2007 openxml php")
->setCategory("Penting");
i am also add some data for header excel, until this code, when try download is okay an not error.
// Add some data
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'Tanggal Masuk')
->setCellValue('B1', 'Nomor Register')
->setCellValue('C1', 'Cabang');
then when add looping PDO to add data content table and download it, value from this database make symbol data excel
$host = 'localhost';
$username = 'root';
$password = 'server';
$dbname = 'primadana';
// Create your database query
this is my PDO for print data from database to excel which make text to symbol
try{
$conn = new PDO("mysql:host=$host;dbname=$dbname",$username,$password);
//$stmt = $conn->prepare('select * from tblpdo where age=:age');
$stmt = $conn->prepare('SELECT * FROM data_berkas_masuk where user_id = 6 order by user_id');
$result=$stmt->fetchAll();
$no=2;
if(count($result))
{
foreach ($result as $row) {
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$no, ($row['in_tgl']));
$no++;
}
}
else{echo "no rows returned";}
}
catch(PDOException $e){
echo 'ERROR: '.$e->getMessage();
}
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('Simple');
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="01simple.xls"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
?>