如何使用PHPExcel生成Excel文件?

时间:2018-12-10 10:48:24

标签: php phpexcel

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

withLatestFrom

头文件

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');

用于生成Excel文件的代码

require_once 'protectExcel/Classes/PHPExcel.php';

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

SQL查询

$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 = "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++;

} 

0 个答案:

没有答案