PHPExcel错误:未定义的索引

时间:2015-02-09 04:25:39

标签: php mysql phpexcel

我想将名为“lokasi”的mysql表导出为ex​​cel,但这些是我得到的错误:

  

注意:未定义的索引:no_lokasi in   第14行的C:\ xampp \ htdocs \ inventory \ pages \ report.php

     

注意:未定义索引:nama_lokasi in   第15行的C:\ xampp \ htdocs \ inventory \ pages \ report.php

     

注意:未定义索引:keterangan_lokasi in   第16行的C:\ xampp \ htdocs \ inventory \ pages \ report.php

     

注意:未定义的索引:no_lokasi in   第14行的C:\ xampp \ htdocs \ inventory \ pages \ report.php

     

注意:未定义索引:nama_lokasi in   第15行的C:\ xampp \ htdocs \ inventory \ pages \ report.php

这是我的代码:

<?php
    if($_SESSION["role"]!=1){
        header('location:login.php');
    }
    else {
        require_once dirname(__FILE__) . '\..\bower_components\phpexcel\Classes\PHPExcel.php';

        if(isset($_POST["submit"])){

            $objPHPExcel = new PHPExcel();
            $objWorksheet = $objPHPExcel->setActiveSheetIndex(0);

            $result = mysql_query("SELECT * FROM lokasi",$conn);

            $rowCount= 1;
            while($row=mysql_fetch_row($result)){
                $objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount, $row['no_lokasi']); 
                $objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowCount, $row['nama_lokasi']);
                $objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowCount, $row['keterangan_lokasi']);             
                $rowCount++; 
            }

            $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel); 
            $objWriter->save('some_excel_file.xlsx'); 
        }
?>      
        <div class="row">
            <div class="col-lg-12">
                <h1 class="page-header">Print Preview</h1>
            </div>
        </div>
        <div class="row">
            <div class="col-lg-12">
                <div class="panel panel-default">
                    <div class="panel-body">
                        <div class="dataTable_wrapper">
                            <table class="table table-striped table-bordered table-hover" id="">
                                <thead>
                                    <tr>
                                        <center>
                                            <th>Nama Lokasi</th>
                                            <th>Keterangan</th>
                                            <th>Action</th>
                                        </center>
                                    </tr>
                                </thead>
                                <tbody>
                                <?php
                                    $result = mysql_query('SELECT * from lokasi');
                                    while($list = mysql_fetch_row($result)){
                                        echo'
                                            <tr class="odd gradeA">
                                                <td>'.$list[0].'</td>
                                                <td>'.$list[1].'</td>
                                                <td>'.$list[2].'</td>     
                                            </tr>           
                                        ';
                                    }
                                ?>                                             
                                </tbody>
                            </table>
                        </div>
                        <div class="row">
                            <div class="col-lg-6">
                                <form role="form" method="post">
                                    <button type="submit" class="btn btn-primary" name="submit">Submit</button>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
<?php 
    };
?>

1 个答案:

答案 0 :(得分:0)

您正在使用mysql_fetch_row来获取结果,并且您正在像这样索引

while($row=mysql_fetch_row($result)){
                $objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount, $row['no_lokasi']); 
                $objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowCount, $row['nama_lokasi']);
                $objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowCount, $row['keterangan_lokasi']);             
                $rowCount++; 
}

将此更改为

while($row=mysql_fetch_row($result)){
                $objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount, $row[0]); // to corresponding row indexes
                $objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowCount, $row[1]);
                $objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowCount, $row[2]);             
                $rowCount++; 
}

因为mysql_fetch_row()函数从结果集中获取一行并将其作为枚举数组返回。