MySQL PHP 未将大数据导出到 Excel

MySQL PHP is not exporting large data to Excel

我想从 mysql table name personen 导出一个 excel 文件,它有 35 列,包含大约 950 多条记录,如果我通过在查询中使用限制导出大约 300 行,它会完美地生成 excel 但是当我尝试导出 500 行或更多行时,它会在 0 字节 excel 文件

这是我的代码

ini_set('memory_limit','1024M');
require_once 'PHPExcel.php';  
require_once 'db_config.inc.php';
// Create new PHPExcel object  
$objPHPExcel = new PHPExcel();  

// Set properties  
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")  
->setLastModifiedBy("Habib ul Haque")  
->setTitle("Office 2007 XLSX Test Document")  
->setSubject("Office 2007 XLSX Test Document")  
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")  
->setKeywords("office 2007 openxml php")  
->setCategory("Test result file");  



$rows=1;

//This is the hard coded *non dynamic* cell formatting
    $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(5);  
    $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(5);  
    $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(5);  
    $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(5);
    $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(5);
    $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(5);
    $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(5);
    $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(5);
    $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(5);
    $objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(5);  
    $objPHPExcel->getActiveSheet()->getColumnDimension('K')->setWidth(5);  
    $objPHPExcel->getActiveSheet()->getColumnDimension('L')->setWidth(5);  
    $objPHPExcel->getActiveSheet()->getColumnDimension('M')->setWidth(5);
    $objPHPExcel->getActiveSheet()->getColumnDimension('N')->setWidth(5);
    $objPHPExcel->getActiveSheet()->getColumnDimension('O')->setWidth(5);
    $objPHPExcel->getActiveSheet()->getColumnDimension('P')->setWidth(5);
    $objPHPExcel->getActiveSheet()->getColumnDimension('Q')->setWidth(5);
    $objPHPExcel->getActiveSheet()->getColumnDimension('R')->setWidth(5);
    $objPHPExcel->getActiveSheet()->getColumnDimension('S')->setWidth(5);  
    $objPHPExcel->getActiveSheet()->getColumnDimension('T')->setWidth(5);  
    $objPHPExcel->getActiveSheet()->getColumnDimension('U')->setWidth(5);  
    $objPHPExcel->getActiveSheet()->getColumnDimension('V')->setWidth(5);
    $objPHPExcel->getActiveSheet()->getColumnDimension('W')->setWidth(5);
    $objPHPExcel->getActiveSheet()->getColumnDimension('X')->setWidth(5);
    $objPHPExcel->getActiveSheet()->getColumnDimension('Y')->setWidth(5);
    $objPHPExcel->getActiveSheet()->getColumnDimension('Z')->setWidth(5);
    $objPHPExcel->getActiveSheet()->getColumnDimension('AA')->setWidth(5);
    $objPHPExcel->getActiveSheet()->getColumnDimension('AB')->setWidth(5);  
    $objPHPExcel->getActiveSheet()->getColumnDimension('AC')->setWidth(5);  
    $objPHPExcel->getActiveSheet()->getColumnDimension('AD')->setWidth(5);  
    $objPHPExcel->getActiveSheet()->getColumnDimension('AE')->setWidth(5);
    $objPHPExcel->getActiveSheet()->getColumnDimension('AF')->setWidth(5);
    $objPHPExcel->getActiveSheet()->getColumnDimension('AG')->setWidth(5);
    $objPHPExcel->getActiveSheet()->getColumnDimension('AH')->setWidth(5);
    $objPHPExcel->getActiveSheet()->getColumnDimension('AI')->setWidth(5);
    $objPHPExcel->getActiveSheet()->getColumnDimension('AJ')->setWidth(5);
    $objPHPExcel->getActiveSheet()->getSheetView()->setZoomScale(85);

    $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A'.($rows+0),'Persoon-Id')
            ->setCellValue('B'.($rows+0),'Aanspreking')
            ->setCellValue('C'.($rows+0),'VoorNaam')
            ->setCellValue('D'.($rows+0),'Naam')
            ->setCellValue('E'.($rows+0),'Fonetisch')
            ->setCellValue('F'.($rows+0),'Extra Naam')
            ->setCellValue('G'.($rows+0),'Adres1')
            ->setCellValue('H'.($rows+0),'Adres2')
            ->setCellValue('I'.($rows+0),'Postcode')
            ->setCellValue('J'.($rows+0),'Plaats')
            ->setCellValue('K'.($rows+0),'Land')
            ->setCellValue('L'.($rows+0),'Taal')
            ->setCellValue('M'.($rows+0),'Tel1')
            ->setCellValue('N'.($rows+0),'Tel1-nota')
            ->setCellValue('O'.($rows+0),'Fax1')
            ->setCellValue('P'.($rows+0),'Fax1-nota')
            ->setCellValue('Q'.($rows+0),'Email1')
            ->setCellValue('R'.($rows+0),'Email1-nota')
            ->setCellValue('S'.($rows+0),'Tel2')
            ->setCellValue('T'.($rows+0),'Tel2-nota')
            ->setCellValue('U'.($rows+0),'Fax2')
            ->setCellValue('V'.($rows+0),'Fax2-nota')
            ->setCellValue('W'.($rows+0),'Email2')
            ->setCellValue('X'.($rows+0),'Email2-nota')
            ->setCellValue('Y'.($rows+0),'Nota-1')
            ->setCellValue('Z'.($rows+0),'Nota-2')
            ->setCellValue('AA'.($rows+0),'Mailing')
            ->setCellValue('AB'.($rows+0),'Datum-invoer')
            ->setCellValue('AC'.($rows+0),'Datum-laatste-wijziging')
            ->setCellValue('AD'.($rows+0),'Extra1')
            ->setCellValue('AE'.($rows+0),'Extra2')
            ->setCellValue('AF'.($rows+0),'Extra3')
            ->setCellValue('AG'.($rows+0),'Oude Publieksbestand-id')
            ->setCellValue('AH'.($rows+0),'Vantabel')
            ->setCellValue('AI'.($rows+0),'Correct Addres')
            ->setCellValue('AJ'.($rows+0),'Codes');
    //  SQl database connections
   /* $db = mysql_connect("localhost", "IMC_COE2", "IMC123");  
    mysql_select_db("IMC_COE2",$db);  
*/
    $mysqli= new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME) or die("couldnt connect to database".mysqli_connect_error());
    $sql="select * from personen";  
    $result=$mysqli->query($sql) or die($mysqli->error);   
        if ($result->num_rows>0)  
        {  
         $i=1;
            while($data=$result->fetch_array(MYSQLI_ASSOC))  
            {  



        $query="select personselectiecodes.SelectieCode from personselectiecodes join personen on(personen.PersonId=personselectiecodes.PersonId) where personselectiecodes.PersonId=".$data['PersonId']."";
        $res=$mysqli->query($query) or die($mysqli->error);

        $codes=array();
            while ($row1=$res->fetch_array(MYSQLI_ASSOC)) 
            {
                if ($row1['SelectieCode']!="") 
                {
                    $codes[]=$row1['SelectieCode'];
                }
            }
        $str=implode(';',$codes);
                //This section is the actual data import fromt he SQL database *dont touch*
                $objPHPExcel->setActiveSheetIndex(0)              
                            ->setCellValue('A'.($rows+$i), $data['PersonId']) //this will give cell C2. 
                            ->setCellValue('B'.($rows+$i), $data['Salutation']) // this will give cell B2 
                            ->setCellValue('C'.($rows+$i), $data['LastName']) //this will give C5 
                            ->setCellValue('D'.($rows+$i), $data['FirstName'])  // This will give D5
                            ->setCellValue('E'.($rows+$i), $data['Phonetical'])  //This will give C6
                            ->setCellValue('F'.($rows+$i), $data['ExtraName']) //This will give D6 etc...  
                            ->setCellValue('G'.($rows+$i), $data['Address1'])
                            ->setCellValue('H'.($rows+$i), $data['Address2'])
                            ->setCellValue('I'.($rows+$i), $data['PostalCode'])
                            ->setCellValue('J'.($rows+$i), $data['Area'])
                            ->setCellValue('K'.($rows+$i), $data['Country'])
                            ->setCellValue('L'.($rows+$i), $data['Language'])
                            ->setCellValue('M'.($rows+$i), $data['Tel1'])
                            ->setCellValue('N'.($rows+$i), $data['Tel1Note'])
                            ->setCellValue('O'.($rows+$i), $data['Fax1'])
                            ->setCellValue('P'.($rows+$i), $data['Fax1Note'])
                            ->setCellValue('Q'.($rows+$i), $data['Email1'])
                            ->setCellValue('R'.($rows+$i), $data['Email1Note'])
                            ->setCellValue('S'.($rows+$i), $data['Tel2'])
                            ->setCellValue('T'.($rows+$i), $data['Tel2Note'])
                            ->setCellValue('U'.($rows+$i), $data['Fax2'])
                            ->setCellValue('V'.($rows+$i), $data['Fax2Note'])
                            ->setCellValue('W'.($rows+$i), $data['Email2'])
                            ->setCellValue('X'.($rows+$i), $data['Email2Note'])
                            ->setCellValue('Y'.($rows+$i), $data['Note1'])
                            ->setCellValue('Z'.($rows+$i), $data['Note2'])
                            ->setCellValue('AA'.($rows+$i), $data['Mailing'])
                            ->setCellValue('AB'.($rows+$i), $data['InputDate'])
                            ->setCellValue('AC'.($rows+$i), $data['DateLastModified'])
                            ->setCellValue('AD'.($rows+$i), $data['Extra1'])
                            ->setCellValue('AE'.($rows+$i), $data['Extra2'])
                            ->setCellValue('AF'.($rows+$i), $data['Extra3'])
                            ->setCellValue('AG'.($rows+$i), $data['OldPublicFileId'])
                            ->setCellValue('AH'.($rows+$i), $data['FromTable'])
                            ->setCellValue('AI'.($rows+$i), $data['CorrectAddress']) 
                            ->setCellValue('AJ'.($rows+$i),$str);
                $i++;
                }  
        }    
    // Rename sheet  
    $objPHPExcel->getActiveSheet()->setTitle('Directory Tool Full dump');

    // Set active sheet index to the first sheet, so Excel opens this as the first sheet
    $objPHPExcel->setActiveSheetIndex(0);

    // Redirect output to a client’s web browser (Excel5) 
    ob_end_clean(); 
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');  
    header('Content-Type: application/vnd.ms-excel'); 
    header('Content-Disposition: attachment;filename="Export-Directory Tool.xls"'); 
    header('Cache-Control: max-age=0'); 
    $objWriter->save('php://output');  
    exit;  
    ?> 

PHPExcel 认为其中一个单元格值中包含公式,即以 = 开头的字符串值;但这实际上作为 Excel 公式无效,否则 PHPExcel 在尝试计算公式时不会失败。

您需要从数据库中确定该值是什么,并确保您使用 setCellValueExplicit() 方法告诉 PHPExcel 它应该被视为字符串而不是公式