好的,下一个 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';