Home » Php » While exporting mysql table into .xls file using php server gets slowdown and exported file gets crashed

While exporting mysql table into .xls file using php server gets slowdown and exported file gets crashed

Posted by: admin June 30, 2018 Leave a comment

Questions:

I have a table in mysql database with 20 columns and 1000000 rows.i have tried to export it in .xls file using phpexcel but it slows down the system and the exported file is corrupted.please help me to export it using php.

set_include_path(get_include_path() . PATH_SEPARATOR . 'Classes/');
include 'PHPExcel/IOFactory.php';
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);

$Qry= $con -> query("SELECT * FROM `table_1`");
$fn="excel_report";
$rowCount = 1;
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount,'COL 1')->getStyle('A'.$rowCount)->applyFromArray($headerColor);
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowCount,'COL 2')->getStyle('B'.$rowCount)->applyFromArray($headerColor);
$objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowCount,'COL 3')->getStyle('C'.$rowCount)->applyFromArray($headerColor);
$objPHPExcel->getActiveSheet()->SetCellValue('D'.$rowCount,'COL 4')->getStyle('D'.$rowCount)->applyFromArray($headerColor);
$objPHPExcel->getActiveSheet()->SetCellValue('E'.$rowCount,'COL 5')->getStyle('E'.$rowCount)->applyFromArray($headerColor);
while($row=$Qry->fetch_array()){
$rowCount++;
$col_1=$row['col_1'];
$col_2=$row['col_2'];
$col_3=$row['col_3'];
$col_4=$row['col_4'];
$col_5=$row['col_5'];
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount, $col_1)->getStyle('A'.$rowCount)->applyFromArray($allBorder);
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowCount, $col_2)->getStyle('B'.$rowCount)->getAlignment()->setWrapText(true);
$objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowCount, $col_3)->getStyle('C'.$rowCount)->getAlignment()->setWrapText(true);
$objPHPExcel->getActiveSheet()->SetCellValue('D'.$rowCount, $col_4)->getStyle('D'.$rowCount)->getAlignment()->setWrapText(true);
$objPHPExcel->getActiveSheet()->SetCellValue('E'.$rowCount, $col_5)->getStyle('E'.$rowCount)->getAlignment()->setWrapText(true);
}
$sheet = $objPHPExcel->getActiveSheet();
$cellIterator = $sheet->getRowIterator()->current()->getCellIterator();
$cellIterator->setIterateOnlyExistingCells( true );
foreach( $cellIterator as $cell ) {
$sheet->getColumnDimension( $cell->getColumn() )->setAutoSize( true );
$sheet->getStyle()->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
$sheet->getStyle()->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP);
}
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
Answers: