PHPexcel不会导出查询结果字段

时间:2013-10-18 11:14:55

标签: php export-to-excel phpexcel

我在试图找出错误的地方时遇到了一些麻烦。

我尝试使用从MySQL查询中检索到的数据填充excel(xlsx)文件到浏览器,然后在某些情况下对其进行编辑。

这是我目前的代码......

if (isset($_POST['export']))
    {
    $SQL = "SELECT * FROM calls WHERE status = 'O'";
    $result = mysql_query($SQL);

    $filename = "Open_Calls_(". date("Y-m-d") . ").xlsx";
    header("Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=utf-8");
    header("Content-disposition: attachment; filename=$filename");
    header('Cache-Control: max-age=0');  

    $data_exp = PHPExcel_IOFactory::createReader('Excel2007');
    $data_exp = $data_exp->load('../template/Helpdesk - Open Calls.xlsx');

    $data_exp->setActiveSheetIndex(0)
        ->setTitle('Open Calls');



    $rowN = 2;

    while ($row = mysql_fetch_row($result))
            {
                    $contact1 = $row['contact_no1'];
                    $contact1 = substr_replace($contact1, "-", 3, 0);
                    $contact1 = substr_replace($contact1, "-", 7, 0);

                    if($row['contact_no2'] != "")
                            {
                                    $contact2 = $row['contact_no2'];
                                    $contact2 = substr_replace($contact2, "-", 3, 0);
                                    $contact2 = substr_replace($contact2, "-", 7, 0);
                            }
                    else
                            {
                                    $contact2 = "";
                            }

                    $date1 = new DateTime($row['doc']);
                    $date2 = new DateTime(date("Y-m-d"));
                    $col = 'A';

                    $data_exp->getActiveSheet()
                            ->setCellValue(A.$rowN, $row['ref_no'])
                            ->setCellValue(B.$rowN, $row['ref_type'])
                            ->setCellValue(C.$rowN, $row['dem_code'])
                            ->setCellValue(D.$rowN, $row['contact_name'])
                            ->setCellValue(E.$rowN, $contact1)
                            ->setCellValue(F.$rowN, $contact2)
                            ->setCellValue(G.$rowN, $row['query_main'])
                            ->setCellValue(H.$rowN, $row['query_sub'])
                            ->setCellValue(I.$rowN, $row['icn_product'])
                            ->setCellValue(J.$rowN, $row['iv_no'])
                            ->setCellValue(K.$rowN, $row['order_no'])
                            ->setCellValue(L.$rowN, $row['section'])
                            ->setCellValue(M.$rowN, $row['action'])
                            ->setCellValue(N.$rowN, $row['logged'])
                            ->setCellValue(O.$rowN, $row['doc'])
                            ->setCellValue(P.$rowN, $row['toc'])
                            ->setCellValue(Q.$rowN, datediff($date1, $date2));

                    $rowN++;
            }

    $objWriter = PHPExcel_IOFactory::createWriter($data_exp, 'Excel2007');
    ob_end_clean();

    $objWriter->save('php://output');

    exit;
    }

所以我设法将文件输出为excel2007格式,但查询结果字段是空白的...例如。 - > setCellValue(A. $ rowN,$ row ['ref_no'])返回空白。更具体地说,$ row ['ref_no']返回一个空白。事实上所有这些。

但是如果我要导出查询,因为它来自foreach循环中的查询而根本没有处理数据,那么excel文件就可以很好地填充了。例如...

$rowN = 2;
            while ($row = mysql_fetch_row($result))
                    {
                            $col = 'A';
                            foreach($row as $cell) 
                                    {
                                            $data_exp->getActiveSheet()->setCellValue($col.$rowN,$cell);
                                            $col++;
                                    }

                            $rowN++;
                    }

为什么?

有人可以这么好,让我知道我哪里出错了。有什么我想念的吗?为什么我不能单独调用查询结果字段?

我对php很新,更不用说phpexcel了,如果我把错误的标题放入或者我的格式错误,我会很高兴有人告诉我。

1 个答案:

答案 0 :(得分:0)

有可能正在生成关于未定义常量A的通知

A.$rowN应为'A'.$rowN

但你可以简化它并做......

$col = 'A';
$data_exp->getActiveSheet()
    ->setCellValue($col++.$rowN, $row['ref_no'])
    ->setCellValue($col++.$rowN, $row['ref_type'])
    ->setCellValue($col++.$rowN, $row['dem_code'])
    ....
    ->setCellValue($col++.$rowN, datediff($date1, $date2));