简体   繁体   English

PHPexcel不会导出查询结果字段

[英]PHPexcel won't export query result fields

I am having a bit of a trouble trying to figure out as to where I am going wrong. 尝试弄清楚我要去哪里哪里时,我有些麻烦。

I am trying to export an excel (xlsx) file to the browser after populating it with data retrieved from a MySQL query which is thereafter edited in some cases. 我试图用从MySQL查询中检索到的数据填充一个excel(xlsx)文件到浏览器中,然后在某些情况下对其进行编辑。

This is my current code... 这是我当前的代码...

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;
    }

So I managed to get the file to output into an excel2007 format, but the query result fields are coming out blank.. eg. 因此,我设法将文件输出为excel2007格式,但是查询结果字段空白。 ->setCellValue(A.$rowN, $row['ref_no']) is returning a blank. -> setCellValue(A. $ rowN,$ row ['ref_no'])返回一个空白。 More specifically the $row['ref_no'] is returning a blank. 更具体地说,$ row ['ref_no']返回一个空白。 All of them in fact. 实际上都是。

But if I were to export the query as it comes from a query in a foreach loop without handling the data at all then the excel file is populated perfectly fine. 但是,如果我要导出查询,因为它来自foreach循环中的查询,而根本不处理数据,那么excel文件就可以很好地填充。 eg... 例如...

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

                            $rowN++;
                    }

Why is that? 这是为什么?

Could someone be so kind as to let me know where I have gone wrong. 有人可以让我知道我哪里出了问题。 Is there something I am missing? 我有什么想念的吗? Why can I not call the query result fields individually? 为什么不能单独调用查询结果字段?

I am quite new to php, let alone phpexcel and would appreciate anyone letting me know if I put the wrong headers in or if my format is wrong. 我对php很陌生,更不用说phpexcel了,如果有人输入错误的标题或格式错误,我将不胜感激。

The chances are that notices about undefined constant A are being generated 可能会生成有关未定义常数A的通知

A.$rowN should be 'A'.$rowN , etc A.$rowN应该为'A'.$rowN ,等等

but you could simplify it and do... 但您可以简化它并执行...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM