PHPExcel不会将数据导出到电子表格

时间:2014-08-22 19:44:42

标签: phpexcel

我正在尝试将数据从数据库导出到Excel电子表格,但它会保存一个空的电子表格。我在对数据进行硬编码之前对其进行了测试,并且工作正常。我还测试了打印我的查询,一切看起来很好,尝试将数据保存到电子表格时出错了。 谁能帮我?请参阅以下代码:

 <?php
    error_reporting(E_ALL);
    ini_set('display_errors', TRUE);
    ini_set('display_startup_errors', TRUE);
    date_default_timezone_set('Europe/London');

    define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
    /** PHPExcel */
    require_once 'C:\PHPExcel\Classes\PHPExcel.php';
    /** PHPExcel_Writer_Excel2007 */
    include 'C:\PHPExcel\Classes\PHPExcel/Writer/Excel2007.php';

    //connecting to the database
    $con = pg_connect('host=localhost dbname=database user=user password=pass');

    //$queryPerson =pg_query('select p.fname, cv,ml,fd,hspt,f_1572 from data_review.ctms_dashboard d, data_review.ctms_personnel p where d.fname=p.fid');
    $queryPerson = "select cv,ml,fd,hspt,f_1572 from data_review.ctms_dashboard d, data_review.ctms_personnel p where d.fname=p.fid";
    $query =pg_query($queryPerson);
    //return records
    /*$resultXC1 = pg_query($queryPerson) or die ('Could not find total');  

         For ($row=0;$resXC1= pg_fetch_array($resultXC1);$row++)
         {
             For ($col=0;$col<6;$col++)
             {  
                      $myrowperson[$row][$col]=$resXC1[$col];
                print  $myrowperson[$row][$col];           
            } //2nd for
         }//1st for
    */
    $rowNumber=1;
    while ($row=pg_fetch_array($query)) {
        $col ='A';
        //$name = $row[0]; //Investigator Name
        $cv = $row['cv']; //Curriculum Vitae sign date
        $ml = $row['ml']; //
        $fd = $row['fd'];//
        $hspt=$row['hspt'];//
        $f_1572=$row['f_1572'];//Form 1572 sign date
        $rows = array($cv,$ml,$fd, $hspt, $f_1572); //this I create custom array
        foreach($rows as $cell) {
            //$objPHPExcel->getActiveSheet(0)->setCellValue($col.$rowNumber,$cell);
            $col++;
                  //print $rows[1];
        }
        $rowNumber++;
    }

    $objPHPExcel = new PHPExcel();

    $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
                                 ->setLastModifiedBy("Maarten Balliauw")
                                 ->setTitle("PHPExcel Test Document")
                                 ->setSubject("PHPExcel Test Document")
                                 ->setDescription("Test document for PHPExcel, generated using PHP classes.")
                                 ->setKeywords("office PHPExcel php")
                                 ->setCategory("Test result file");

    $objPHPExcel->setActiveSheetIndex(0);

    $callStartTime = microtime(true);

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));
$callEndTime = microtime(true);
$callTime = $callEndTime - $callStartTime;

// Save Excel 2007 file
echo date('H:i:s') . " Write to Excel2007 format\n";
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));

// Echo done
echo date('H:i:s') . " Done creating file.\r\n";
//mysqli_close($con);
exit;   
?> 

1 个答案:

答案 0 :(得分:0)

您似乎试图在 之前设置单元格值 ,您已经实例化了PHPExcel对象

移动你的

$objPHPExcel = new PHPExcel();

<强>

`while ($row=pg_fetch_array($query)) {`