Generated Excel Files have errors (part II)

Topics: User Forum
Nov 26, 2009 at 10:10 PM
Edited Nov 26, 2009 at 10:41 PM

Hi,

I have the same problem as posted by Urkman Oct 14 at 10:58 AM.

My generated file is valid on localhost using XAMP but the same code from server bring me broken xls file. I checked iconv and works fine in my case. I'm using latest (1.7.1) version of library. I alsow check if any errors will occur by using:

error_reporting(E_ALL);
ini_set('display_errors', '1');

but nothing showed.

I used Tests\01simple-download-xls.php to generate both files. Localhost PHP 5.2.9, server PHP 5.2.4

 

If i save file on serwer and then download everything works fine and file is valid but encoding fails.

Nov 26, 2009 at 10:14 PM

I posted files in generateErrors.zip

Developer
Nov 27, 2009 at 2:46 AM

Somehow a line break got inserted at the beginning of the file called 76354-01simple(server).xls as seen when opened in a text editor



It should look like in 76354-01simple(local).xls

Perhaps you have a newline before the opening <?php in your script?

Nov 27, 2009 at 9:35 AM
Edited Nov 27, 2009 at 9:35 AM

Thank you for fast replay. You were right, that was empty line in my application. I have another question about speed. My code:

 

 

		$objPHPExcel = new PHPExcel();
	
		// Set properties
		$objPHPExcel->getProperties()->setCreator("System");
			
		// Add some data
		$objPHPExcel->setActiveSheetIndex(0);
		
		$styleHeaderArray = array(
			'font' => array(
				'bold' => true,
			),
			'alignment' => array(
				'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
				'vertical' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
			),
			'borders' => array(
				'allborders' => array(
					'style' => PHPExcel_Style_Border::BORDER_THIN,
				),
				'color' => array(
					'rgb' => PHPExcel_Style_Color::COLOR_BLACK,			 
				),
			),
			'fill' => array(
				'type' => PHPExcel_Style_Fill::FILL_SOLID,
				'startcolor' => array(
					'rgb' => 'FFFFCC',
				),
			),
		);
		
		// Set headers & autosize
		foreach($this->headers as $key => $val){
			$objPHPExcel->getActiveSheet()->setCellValueExplicitByColumnAndRow($key, 1, $val);
			$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($key, 1)->applyFromArray($styleHeaderArray);
			$objPHPExcel->getActiveSheet()->getColumnDimensionByColumn($key)->setAutoSize(true);
		}
	
		$objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(25);
		
		foreach($this->result as $row => $rowData){
			//set values 
			set_time_limit(20);
			foreach($rowData as $column => $cellValue){
				$objPHPExcel->getActiveSheet()->setCellValueExplicitByColumnAndRow($column, ($row+2), $cellValue);
			}
			
			//row height
			$objPHPExcel->getActiveSheet()->getRowDimension(($row+2))->setRowHeight(15);
		}
		
		// Rename sheet
		$objPHPExcel->getActiveSheet()->setTitle('Simple');	
		
		
		// Redirect output to a client’s web browser (Excel5)
		header('Content-Type: application/vnd.ms-excel');
		header('Content-Disposition: attachment;filename="test.xls"');
		header('Cache-Control: max-age=0');
		
		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
		$objWriter->save('php://output'); 

		exit;

 

 

9 columns and 7245 rows generation time is about 34s. Is there any way to speed up this process?

Developer
Jan 22, 2010 at 7:25 AM
pawaroti wrote:

9 columns and 7245 rows generation time is about 34s. Is there any way to speed up this process?

AutoSize columns is known to slow down the process.

Sep 20, 2013 at 10:01 AM
Hello all,

I have the same proble. When I open a generated file i have`t a empty line. Can you help me?
Sep 20, 2013 at 10:51 AM
I FIND THE SOLUTION OF MY PROBLEM!!!!!

Just must clean the buffer before settin the header part like this:

if (ob_get_contents()) ob_end_clean();
Coordinator
Sep 20, 2013 at 11:03 AM
Cleaning the output buffer is hiding the problem: somewhere in your script, there is still something that's generating output, whether that output is whitespace, a BOM header, an error message or whatever