好的,下一个PHPExcel问题。我有一个用户填写的HTML表单,数据将被发送到数据库。所有这一切都没有问题。我正在使用PHPExcel将该信息发送到Excel工作表,以便组织总裁可以查看已发送的报告(他选择了他想要的Excel中的报告)。单击该按钮时,信息将下载到Excel。这一切都正常,除了标题信息涵盖工作表的第一行。我知道它是标题,因为如果我更改$ headings并删除数组中的值,则显示隐藏查询的部分。
我的代码来自:http://phpexcel.codeplex.com/discussions/246121/
可能存在编码错误,或者我可能没有将所有文件和类上传到互联网......
感谢任何帮助。感谢。
// 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';
答案 0 :(得分:2)
在这段代码中你通过结果集有两个循环,一个从第1行开始,然后用你的标题覆盖第1行,然后再循环遍历结果集(由于你已经到达了从第2行开始的第一个循环中结果集的结尾