创建一个新的工作表,并在组织ID更改时添加相应的数据。电子表格

时间:2019-06-20 15:52:16

标签: php phpspreadsheet

使用此代码,我得到一个工作簿,其中所有数据都放在一个工作表中,我需要为每个组织ID创建一个工作表,该字段为“ ompCustomerOrganizationID”,并将具有该组织ID的所有数据放入新工作表中,然后移动到下一个。 因此,我将得到一本工作簿,其中包含10个工作表,每个组织ID一个

我认为我应该使用addsheet($ myworksheet,0)方法。需要一个名称和一个工作表号。名称应为“ ompCustomerOrganizationID”,工作表编号应从0开始,并随着OrganizationID的更改而递增。

但是如何创建循环并检查“ ompCustomerOrganizationID”中的更改。

//call the autoload
require 'vendor/autoload.php';
//load phpspreadsheet class using namespaces
use PhpOffice\PhpSpreadsheet\Spreadsheet;
//call iofactory instead of xlsx writer
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Fill;


// the connection 
$connect =odbc_connect("removed");
if(!$connect) {
    exit("Connection Failed: " . $connect);
}

//the sql query
$sql="select ompCustomerOrganizationID,ompSalesOrderID,ompOrderDate, ompRequestedShipDate,ompCustomerPO, ompOrderTotalBase, ompShippingMethodID
from M1_KF.dbo.salesorders
left outer join M1_KF.dbo.Organizations on cmoOrganizationID=ompCustomerOrganizationID

 where  ompClosed !=-1 AND ompCustomerOrganizationID in ('10464','11265','10805','11956','11976','10041','11790','10168','10250','10876')

 order by ompCustomerOrganizationID ,omporderdate";
//run the query
$result =odbc_exec($connect,$sql);
if(!$result){
exit("Error in SQL");
}  
//Loop starts here ????????????

$exrow=3;
//make a new spreadsheet object
$spreadsheet = new Spreadsheet();
//get current active sheet (first sheet)
$sheet = $spreadsheet->getActiveSheet();

//set default font
$spreadsheet->getDefaultStyle()
    ->getFont()
    ->setName('Arial')
    ->setSize(10);

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


//Column headings

$spreadsheet->getActiveSheet()
    ->setCellValue('A2',"CUSTOMER")
    ->setCellValue('B2',"ORDER ID")
    ->setCellValue('C2',"ORDER DATE")
    ->setCellValue('D2',"SHIP DATE")
    ->setCellValue('E2',"PO NUMBER")
    ->setCellValue('F2',"TOTAL")
    ->setCellValue('G2',"SHIP");

 while ($row = odbc_fetch_array($result))
{
//loop through the data

    $spreadsheet->getActiveSheet()
        ->setCellValue('A'.$exrow ,$row['ompCustomerOrganizationID'])
        ->setCellValue('B'.$exrow ,$row['ompSalesOrderID'])
        ->setCellValue('C'.$exrow ,$row['ompOrderDate'])
        ->setCellValue('D'.$exrow ,$row['ompRequestedShipDate'])
        ->setCellValue('E'.$exrow ,$row['ompCustomerPO'])
        ->setCellValue('F'.$exrow ,$row['ompOrderTotalBase'])
        ->setCellValue('G'.$exrow ,$row['ompShippingMethodID']);
    $exrow++;
}
//define first row and last row
$firstRow=2;
$lastRow=$exrow-1;

odbc_close($connect);

//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="result.xlsx"');

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

0 个答案:

没有答案