美好的一天,我试图用phpexcel从我的桌子中导出一些记录。请先检查一下myscript。
function ceks()
{
$this->load->library('excels');
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setTitle("export")->setDescription("Export Penilaian");
$objPHPExcel->setActiveSheetIndex(0);
$query = $this->db->query("SELECT * from
(
select distinct TOP 201233 row, a.tanggal,b.OutletCode,i.Description areaoutlet,c.Nilai,a.Nip,b.Fullname,a.KodePenilaian,f.Description as posisilama,
d.Description as posisibaru
from penilaian_header a
left join Employee b on a.Nip = b.Nip
left join Position f on b.PositionCode = f.PositionCode
left join Position d on a.PositionCode = d.PositionCode
left join arealeader g on g.OutletCode = b.OutletCode
left join outlet h on g.OutletCode = h.OutletCode
left join area i on i.areacode = h.areacode
left join (select ROW_NUMBER() OVER(PARTITION BY KodePenilaianH ORDER BY idPenilaiand DESC) AS Row,
Nilai,KodePenilaianH from penilaian_Detail
) c on a.KodePenilaian = c.KodePenilaianH where h.AreaCode like '%$area%' and Periode like '%$periode%' $kondisi order by i.Description asc) nilai
pivot
(
sum(nilai)
for row in ([1],[2],[3],[4],[5])
) piv;");
$col = 0;
foreach ($fields as $field)
{
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 1, $field);
$col++;
}
$row = 2;
foreach($query->result_array() as $data)
{
$col = 0;
foreach ($fields as $field)
{
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $data->$field);
$col++;
}
$row++;
}
$objPHPExcel->setActiveSheetIndex(0);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="Products_'.date('dMy').'.xls"');
header('Cache-Control: max-age=0');
ob_end_clean();
ob_start();
$objWriter->save('php://output');
}
}
我在上面的脚本中使用result_array()
。导出的excel文件中没有任何结果。但是,当我只使用result()
时,结果会显示出来。但是,像[1],[2],[3],[4]& [5]没有表现出来。
来自print_r($query->result_array())
Array
(
[0] => Array
(
[tanggal] => 2016-05-30
[OutletCode] => K-ARN3
[areaoutlet] => JAKARTA
[Nip] => 1411751
[Fullname] => SITI MASITOH
[KodePenilaian] => PE0000001
[posisilama] => ADMINISTRASI OUTLET
[posisibaru] => VISUAL MERCHANDISE OUTLET
[1] => 5
[2] => 5
[3] => 6
[4] => 7
[5] => 8
)
[1] => Array
(
[tanggal] => 2016-05-30
[OutletCode] => K-BJDM2
[areaoutlet] => BANJARMASIN
[Nip] => 1500281
[Fullname] => HENDRIYANI
[KodePenilaian] => PE0000005
[posisilama] => ADMINISTRASI OUTLET
[posisibaru] => VISUAL MERCHANDISE OUTLET
[1] => 8
[2] => 7
[3] => 7
[4] => 8
[5] => 7
)
)