如何使用PHPExcel生成excel文件?

我正在尝试使用PHPExcel在excel文件中反映一些数据,当调用一个函数时 . 该文件正在下载,但excel文件中显示的所有内容都是主菜单中的超链接,而且数据库中没有数据 . 该查询在MySql中正常工作 .

function downloadBNF() {

            global $DB;   

                    $fileName = 'barcodefound';

头文件

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="' . $fileName . '.xls"');
        header('Cache-Control: max-age=0');

类文件

require_once 'protectExcel/Classes/PHPExcel.php';

require_once 'protectExcel/Classes/PHPExcel/IOFactory.php';

生成excel文件的代码

$objPHPExcel = new PHPExcel();

$objPHPExcel->getActiveSheet()->getProtection()->setSheet(true);

$objPHPExcel->getActiveSheet()->getProtection()->setInsertRows(true);



$objPHPExcel->setActiveSheetIndex(0);

$objPHPExcel->getActiveSheet()->setTitle('Favorite');

$objPHPExcel->getActiveSheet()->mergeCells('A1:R1');

$objPHPExcel->getActiveSheet()->setCellValue('A1',$head);

$objPHPExcel->getActiveSheet()->setCellValue('A3','Party Code');

$objPHPExcel->getActiveSheet()->setCellValue('B3','Party Name');

$objPHPExcel->getActiveSheet()->setCellValue('C3','Barcode');

$objPHPExcel->getActiveSheet()->setCellValue('D3','Date');

$objPHPExcel->getActiveSheet()->setCellValue('E3','Serial No');

$row =4;

$fromDate = date('Y-m-d', strtotime($_REQUEST['BNFFrom'])); 

$toDate =  date('Y-m-d', strtotime($_REQUEST['BNFTo']));

SQL查询

$sql = "SELECT * FROM `".$DB->pre."sample_not_found` where date >='$date%' and date <='$date1%' and partyCode !='0' ";  

 $rows = $DB->dbRows($sql);
 $DB->numRows;

其余的代码

if($DB->numRows > 0) {
 foreach($rows as $d){
                  $pcode = $_COOKIE['PARTYCODE'];
                  $sql1 ="select * from mx_party_master where kunnr = $pcode ";
                  $res = $DB->dbRow($sql1);
                  $pname = $res['NAME1'];

    $objPHPExcel->getActiveSheet()->setCellValue('A'.$row, $d['partyCode']);

    $objPHPExcel->getActiveSheet()->setCellValue('B'.$row, $pname);

    $objPHPExcel->getActiveSheet()->setCellValue('C'.$row, $d['barcode']);

    $objPHPExcel->getActiveSheet()->setCellValue('D'.$row, $d['date']);

    $objPHPExcel->getActiveSheet()->setCellValue('E'.$row, $srno) ;
                $row++;

}

生成输出

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
                 $objWriter->save('php://output');

                $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
                $objWriter->save('excel-files/' . $fileName . '.xls');

        exit;   

}
}

回答(0)