【发布时间】:2013-12-26 15:13:46
【问题描述】:
显然,我是 PHPExcel 新手。我对 PHP 本身也很陌生。
该网站具有多个级别的查看/编辑权限。
我一直在为一个网站制作一个页面,该页面收集存储在 SQL 数据库中的信息并填充一个 Excel 模板。
application.php 包含所有数据库连接等。
基本上,我遇到的问题是当我调用数组来填充单元格时,它显示为相同数组值的重复项。不仅如此,还会将它们全部塞到同一列中。
我需要填充的值是:
部件号 描述 U/价格 数量 总计
15666562003 灯组件 $20.00 1 $20.00
运费 131514 $12.35 1 $12.35
数据会这样显示:
部件号 描述 U/价格 数量 总计
货运 货运 131514 131514 $12.35 $12.35 1 1 $12.35 $12.35
任何帮助将不胜感激!
<?php
include "./include/application.php";
require './Classes/PHPExcel.php';
error_reporting(E_ALL ^ E_NOTICE);
class CForm extends CApplication
{
function CForm()
{
$this->CApplication();
}
//**********************************************************
function Export($msg = "")
{
if ($_SESSION['aID'] == "" && $_SESSION['mID'] == "237" && $_SESSION['mID'] == "178" && $_SESSION['mID'] == "551")
{
$sWhere = " AND dID = '$_SESSION[mID]'";
}
$sql = "SELECT * FROM dl_warranty_claims
INNER JOIN dealers ON (dID = wDealerID)
WHERE 1 ".$sWhere." AND wID = '$_GET[id]'";
$res = $this->SQL($sql);
if (!($row = $this->FetchArray($res)))
{
print "You do not have access to view this report.";
exit();
}
error_reporting(E_ALL ^ E_NOTICE);
// Read the template file
$inputFileType = 'Excel2007';
$inputFileName = './templates/warrantyclaimtemplate2.xlsx';
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
// Adding data to the template
$objPHPExcel->getActiveSheet()->setCellValue('A3', ($row['wDealerName']));
$objPHPExcel->getActiveSheet()->setCellValue('B3', ($row['wID']));
$objPHPExcel->getActiveSheet()->setCellValue('C3', ($row['wCustomerName']));
$objPHPExcel->getActiveSheet()->setCellValue('D3', ($row['wModelName']));
$objPHPExcel->getActiveSheet()->setCellValue('E3', ($row['wChassisSN']));
$objPHPExcel->getActiveSheet()->setCellValue('F3', ($row['wEngineSN']));
$objPHPExcel->getActiveSheet()->setCellValue('G3', ($row['wDateDelivery']));
$objPHPExcel->getActiveSheet()->setCellValue('H3', ($row['wDateFailure']));
$objPHPExcel->getActiveSheet()->setCellValue('I3', ($row['wDateClaim']));
$objPHPExcel->getActiveSheet()->setCellValue('J3', ($row['wOperatingHours']));
$sql = "SELECT * FROM dl_warranty_parts
WHERE wpWarrantyClaimID = '$_GET[id]'";
$res = $this->SQL($sql);
while($rowp = $this->FetchArray($res))
{
$objPHPExcel->getActivesheet()->FromArray($rowp, NULL, 'A4');
}
// Write and save file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
header('Content-Type: application/vnd.openxmlformats- officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment; filename="warrantyclaimreport.xlsx"');
$objWriter->save('php://output');
}
}
?>
【问题讨论】:
-
那么实际的问题是什么?错误信息?
-
请注意,您应该为 OfficeOpenXML
.xlsx文件使用Excel2007读取器和写入器;Excel5Reader 和 Writer 用于 BIFF 格式.xls文件 -
你好,问题是它把我带到一个空白页面而不是下载excel文档。
-
另外,我已经改变了读者和作者,但我仍然面临同样的问题。我还将保存选项更改为 $objWriter->save('php://output');
标签: php mysql session phpexcel