网站有多级权限查看/编辑。
我一直在为一个网站开发一个页面,该页面收集存储在 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');
}
}
?>
您的保存声明
$objWriter->save('WarrantyClaim.xlsx');
正在将一个名为
WarrantyClaim.xlsx
的文件写入磁盘(在脚本的当前工作目录中)。
但是您有标头表明您要将输出发送到浏览器
除了发送您正在编写的文件类型的正确标头之外:
Filetype Writer Content type
.xls Excel5 application/vnd.ms-excel
.xlsx Excel2007 application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
保存到
php://output
会将文件发送到浏览器
您的阅读器也有类似的问题:当您应该使用
Excel5
阅读器时却使用
Excel2007
阅读器来读取 .xlsx 文件
您的
$this->FetchArray()
方法似乎返回both枚举和关联数据...我不知道参数到底是什么,但应该有一些设置允许您指定任一枚举或 关联,或诸如 FetchAssoc()
之类的方法调用,专门仅返回关联数组