foreach无法正常工作。我究竟做错了什么?

时间:2019-08-12 14:16:01

标签: php phpspreadsheet

我想为$ salesperson数组中的每个ID创建一个电子表格,因为现在我只得到第一个ID的电子表格。我不确定问题出在哪里,我得到了第一个ID的电子表格,然后它停止了。


$salesperson=array('1name',
'2name',
'3name',
'4name',
'5name');
foreach($salesperson as $salesId ){

$commission_percent=0;
$price=0;
$commission=0;
//styling arrays end
$exrow=6;
//make a new spreadsheet object
$spreadsheet = new Spreadsheet();
//set default font
$spreadsheet->getDefaultStyle()
   ->getFont()
   ->setName('Arial')
   ->setSize(10);

//heading
$spreadsheet->getActiveSheet()
   ->setCellValue('A1',"Comission");

//merge heading
$spreadsheet->getActiveSheet()->mergeCells("A1:R1");

// set font style
$spreadsheet->getActiveSheet()->getStyle('A1')->getFont()->setSize(20);

// set cell alignment
$spreadsheet->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);

//setting column width



//header text//current row
$spreadsheet->getActiveSheet()
   ->setCellValue('A5',"INVOICE DATE")
   ->setCellValue('B5',"SALESPERSON")
   ->setCellValue('C5',"INVOICE ID")
   ->setCellValue('D5',"ORDER ID")
   ->setCellValue('E5',"ORDER DATE")
   ->setCellValue('F5',"CUSTOMER ID")
   ->setCellValue('G5',"NAME")
   ->setCellValue('H5',"GROUP ID")
   ->setCellValue('I5',"STATE")
   ->setCellValue('J5',"QTY")
   ->setCellValue('K5',"PART")
   ->setCellValue('L5',"UOM")
   ->setCellValue('M5',"GROUP")
   ->setCellValue('N5',"GROSS")
   ->setCellValue('O5',"DISCOUNT")
   ->setCellValue('P5',"NET")
   ->setCellValue('Q5',"CommPrcnt")
   ->setCellValue('R5',"Commission");




//set font style and background color
$spreadsheet->getActiveSheet()->getStyle('A5:R5')->applyFromArray($tableHead);
// the connection 
$connect =odbc_connect("removed");
if(!$connect) {
   exit("Connection Failed: " . $connect);
}

//the sql query
$sql="sql statement that gets data for each $salesId ";
//run the query
$result =odbc_exec($connect,$sql);
if(!$result){
exit("Error in SQL");
}

while ($row = odbc_fetch_array($result))
{


$prtcnt=floatval($row['commission_percent']);
$commission_percent=number_format($prtcnt, 2);
$price=floatval($row['ARLEXTENDEDPRICEBASE']);
$commission=$commission_percent*$price;
   $spreadsheet->getActiveSheet()
       ->setCellValue('A'.$exrow ,$row['INVOICEDATE'])
       ->setCellValue('B'.$exrow ,$row['ARJSALESEMPLOYEEID'])
       ->setCellValue('C'.$exrow ,$row['ARPARINVOICEID'])
       ->setCellValue('D'.$exrow ,$row['UARPSALESORDERNUMBER'])
       ->setCellValue('E'.$exrow ,$row['createdDate'])
       ->setCellValue('F'.$exrow ,$row['ARPCUSTOMERORGANIZATIONID'])
       ->setCellValue('G'.$exrow ,$row['CMONAME'])
       ->setCellValue('H'.$exrow ,$row['CMOCUSTOMERGROUPID'])
       ->setCellValue('I'.$exrow ,$row['cmoState'])
       ->setCellValue('J'.$exrow ,$row['ARLINVOICEQUANTITY'])
       ->setCellValue('K'.$exrow ,$row['ARLPARTID'])
       ->setCellValue('L'.$exrow ,$row['ARLUNITOFMEASURE'])
       ->setCellValue('M'.$exrow ,$row['ARLPARTGROUPID'])
       ->setCellValue('N'.$exrow ,$row['ARLFULLEXTENDEDPRICEBASE'])
       ->setCellValue('O'.$exrow ,$row['ARLEXTENDEDDISCOUNTBASE'])
       ->setCellValue('P'.$exrow ,$row['ARLEXTENDEDPRICEBASE'])
       ->setCellValue('Q'.$exrow ,$row['commission_percent'])
       ->setCellValue('R'.$exrow ,$commission);



   //set row style
   if( $exrow % 2 == 0 ){
       //even row
       $spreadsheet->getActiveSheet()->getStyle('A'.$exrow.':R'.$exrow)->applyFromArray($evenRow);
   }else{
       //odd row
       $spreadsheet->getActiveSheet()->getStyle('A'.$exrow.':R'.$exrow)->applyFromArray($oddRow);
   }
   //increment row
   $exrow++;
}
odbc_close($connect);

//define first row and last row
$firstRow=5;
$lastRow=$exrow-1;
$totalrow=$exrow+1;
$percentrow=$totalrow+1;

//SUBtotal and SUM rows

// Total All

//Percent

//Format currency 

//
$spreadsheet->getActiveSheet()
->setCellValue('G2',"Filter Total")
->setCellValue('G3',"Grand Total")
->setCellValue('G4',"Percent");

//set the autofilter
$spreadsheet->getActiveSheet()->setAutoFilter("A".$firstRow.":R".$totalrow);

//set the header first, so the result will be treated as an xlsx file.
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');

//make it an attachment so we can define filename
header('Content-Disposition: attachment;filename="'.$salesId.'".xlsx"');

//create IOFactory object
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
//save into php output
$writer->save('php://output');

}

?> 

我从代码中删除了尽可能多的内容,所以不会太多,但是可以根据需要添加所有内容。

每个销售人员ID我需要1个电子表格。谢谢

0 个答案:

没有答案