PHPEXCEL下载与PDO数据库错误文本成为符号

时间:2016-04-29 03:22:16

标签: pdo phpexcel

我正在使用示例exel代码并使用PDO添加数据导出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();

我正在编辑一些属性

// 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");

我还为标题excel添加了一些数据,直到这段代码,当尝试下载时没有错误。

// Add some data
$objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue('A1', 'Tanggal Masuk')
        ->setCellValue('B1', 'Nomor Register')
        ->setCellValue('C1', 'Cabang');

然后当添加循环PDO以添加数据内容表并下载它时,来自该数据库的值使符号数据变得更好

$host = 'localhost'; 
$username = 'root'; 
$password = 'server';
$dbname = 'primadana';
// Create your database query

这是我的PDO,用于从数据库到excel的打印数据,使文本符号

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;
?>

1 个答案:

答案 0 :(得分:0)

我发现自己大声笑。

WITH CTE1 AS 
(
SELECT [Team_Code],[ID] FROM Fireman_Team WHERE Team_Code in (
  SELECT Answer.Team_Code FROM Answer WHERE
  Answer.Call_Number IN(
    SELECT Call_Number FROM (SELECT * FROM Call WHERE Call.Time_Started
      BETWEEN ('2016-01-01 00:00:00') AND 
      ('2016-01-31 23:59:59')) AS SSS)) 
)

;WITH CTE2 AS
(
SELECT COUNT(Team_Code) as number_of_times, 
Team_Code FROM (SELECT Answer.Team_Code FROM Answer WHERE
  Answer.Call_Number IN(
    SELECT Call_Number FROM (SELECT * FROM Call WHERE Call.Time_Started
      BETWEEN ('2016-01-01 00:00:00') AND 
      ('2016-01-31 23:59:59')) AS SSS)
) AS re GROUP BY Team_Code
)

SELECT b.[number_of_times], b.[Team_Code], a.[ID]
FROM CTE1 a
INNER JOIN CTE2 b
ON a.[Team_Code] = b.[Team_Code]