PHPExcel文件不再写入

时间:2019-02-24 04:46:09

标签: php sql phpexcel

我无法弄清楚这里出了什么问题,但以前这是在导出excel文件,但仅包含标头,没有数据。

现在正在打印它已成功连接到数据库,然后作业完成而没有错误,但它没有创建新的excel文件。我将其更改为使用PDO,这是它完全停止打印文件的时间。

如何更改此设置以正确创建/写入文件并实际打印查询中的结果行?

    $DB2Dev = new PDO("odbc:DSN=connnection1;", "user", "password");
    if($DB2Dev){
            $connectionMessage =  "DB2 connection successful";
            print_r($connectionMessage);
        }else{
            $connectionMessage = "DB2 NOT CONNECTED!";
            print_r($connectionMessage);
        }
    $DB2Dev->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


    //Create and run query
    $sql = "
    SELECT manager, count(*) as items
      from items
    where start_date >= '2019-01-01'
    group by manager
    order by manager";


    try {
        $itemDetails = $DB2conn->prepare($sql);
        $detailRslt = $itemDetails->execute();

    }catch(PDOException $ex) {
        $detailSelectError = "QUERY ONE FAILED!: " .$ex->getMessage();
    }

    $phpExcel = new PHPExcel;
    $sheet = $phpExcel->getActiveSheet();
    $sheet ->setTitle("Item Counts");
    $sheet ->getColumnDimension('A') -> setAutoSize(true);
    $sheet ->getColumnDimension('B') -> setAutoSize(true);



    //Create header row
    $sheet->setCellValue('A1', 'Manager');
    $sheet->setCellValue('B1', 'Items');

    $rowCount = 1;

    while($row = $itemDetails->fetch(PDO::FETCH_ASSOC)){
        var_dump($row);
        $sheet->setCellValue('A'.$rowCount, $row['manager']);
        $sheet->setCellValue('B'.$rowCount, $row['items']);

        $rowCount++;
    }

    $writer = PHPExcel_IOFactory::createWriter($phpExcel, "Excel2007");
    $writer->save('Item Counts.xlsx');

1 个答案:

答案 0 :(得分:1)

它不起作用,因为使用时'$ DB2conn'不存在。我认为应该是$ DB2Dev。如果您修复它,此脚本将正确执行。