tehlivi
tehlivi

Reputation: 810

PHPExcel Header Line covering first entry

Okay, next PHPExcel question. I have an HTML form that users fill out and the data will be sent to a database. All this works with no problems. I am using PHPExcel to send that information to an Excel worksheet so the President of the organization can view reports that have been sent in (he chose that he wanted it in Excel). When he clicks the button, the information is downloaded to Excel. This all works fine, except for the first line of the worksheet is covered by the Header information. I know that it is the header, because if I change $headings and delete value(s) in the array, part(s) of the hidden query then shows.

I got this code from: http://phpexcel.codeplex.com/discussions/246121/

There may be a coding error, or I may not have uploaded all the files and classes to the internet...

Any help is appreciated. Thanks.

// connection with the database 
$dbhost = "I removed this for privacy"; 
$dbuser = "I removed this for privacy"; 
$dbpass = "I removed this for privacy"; 
$dbname = "I removed this for privacy"; 

mysql_connect($dbhost,$dbuser,$dbpass); 
mysql_select_db($dbname); 

// require the PHPExcel file 
require 'Classes/PHPExcel.php'; 

// simple query 

$query = "

SELECT dueDate, reporterName, program, noReportReason, recentActivity, impacted, fundsGenerated, fundsSpent, volunteers, input, potentialContacts, potentialSources
FROM reports
ORDER BY id

"; 

if ($result = mysql_query($query) or die(mysql_error())) 
{   
// Create a new PHPExcel object 
$objPHPExcel = new PHPExcel(); 
$objPHPExcel->getActiveSheet()->setTitle('Quarterly Project Reports'); 

// Loop through the result set 
$rowNumber = 1; 
while ($row = mysql_fetch_row($result)) 
{ 
$col = 'A'; 
foreach($row as $cell) 
{ 
$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$cell); 
$col++; 
} 
$rowNumber++; 
}

$rowNumber = 1;
$headings = array('Due Date','Reporter Name','Name of Program','No Report This Quarter',
    'Recent Activity','Number of People Impacted','Funds Generated',
    'Funds Spent and Fuding Sources','Volunteers Mobilized','WNP Input',
    'Potential Contacts','Potential Sources'); 
$objPHPExcel->getActiveSheet()->fromArray(array($headings),NULL,'A'.$rowNumber);  

$rowNumber++; 
// Loop through the result set 
while ($row = mysql_fetch_row($result)) 
{
$col = 'A';
foreach($row as $cell) 
{
$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$cell);
$col++;
}
$rowNumber++; 
}

//bold Header row
$objPHPExcel->getActiveSheet()->getStyle('A1:L1')->getFont()->setBold(true); 

//wrap column D
$objPHPExcel->getActiveSheet()->getStyle('D1:D'.$objPHPExcel->getActiveSheet()->getHighestRow())->getAlignment()->setWrapText(true);

//wrap column E
$objPHPExcel->getActiveSheet()->getStyle('E1:E'.$objPHPExcel->getActiveSheet()->getHighestRow())->getAlignment()->setWrapText(true); 

//set header row height
$objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(20);

//set column widths
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(15);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(30);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(50);
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(50);
$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(30);
$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(30);
$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(30);
$objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(30);
$objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(30);
$objPHPExcel->getActiveSheet()->getColumnDimension('K')->setWidth(30);
$objPHPExcel->getActiveSheet()->getColumnDimension('L')->setWidth(30); 

// Save as an Excel BIFF (xls) file 
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); 

header('Content-Type: application/vnd.ms-excel'); 
header('Content-Disposition: attachment;filename="WNPQuarterlyReports.xls"'); 

$objWriter->save('php://output'); 
exit(); 
} 
echo 'a problem has occurred... no data retrieved from the database'; 

Upvotes: 2

Views: 6691

Answers (1)

Mark Baker
Mark Baker

Reputation: 212402

You do have two loops through the resultset in this code, one starting at row #1, you then overwrite row 1 with your headers, then loop through the resultset again (which won't actually loop because you've reached the end of the resultset in the first loop) that starts at row 2

Upvotes: 2

Related Questions