很显然,我是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');
}
}
?>
最佳答案
您的保存声明
$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
会将文件发送到浏览器您的Reader也有类似的问题:应该使用
Excel5
Reader时,使用Excel2007
Reader读取.xlsx文件。关于php - PHPExcel;来自MySQL的数组问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20787017/