PHPExcel将列结构化为行

时间:2016-11-16 11:59:02

标签: php excel phpexcel

我有一个Jobs列表,其中可以包含多个Visits

在PHPExcel中,我目前正在打印作业,并将访问列为以下列:enter image description here

所以我根据访问工作的方式来训练我需要多少列。

但是出于过滤目的,我希望这些访问列显示为行,而不是:enter image description here

我在这里指定了访问号码,因此每次访问都会在其自己的行上。

这是我的代码(目前打印如第一张图片所示):

       // create the worksheet
        $worksheet_two = $objPHPExcel->createSheet(1); //Setting index when creating
        $worksheet_two->setTitle("Reactive Jobs Not Complete");
        $worksheet_two->getStyle('A1:AL1')->getFont()->setBold(true);

        // work out the amount of visit columns needed based on how many visits there are
        $supplierCols = 0;
        foreach ($worksheet_two_jobs as $worksheet_two_job) {
            $suppliers = count(array_unique(Hash::extract($worksheet_two_job['Visit'], '{n}.Supplier.name')));
            $supplierCols = max($suppliers, $supplierCols);
        }


        // build up headers
        $headers = array();
        $headers[] = __('Client');
        $headers[] = __('Job Ref');
        $headers[] = __('Order Number');

        for ($i = 0; $i < $supplierCols; $i++) {
                $key = $i + 1;
                $headers[] = __('Visit %s Subcontractor', $key);
                $headers[] = __('Visit %s Start Date', $key);
                $headers[] = __('Visit %s Start Time', $key);
                $headers[] = __('Visit %s End Date', $key);
                $headers[] = __('Visit %s End Time', $key);
            }

        // insert headers into worksheet
        $worksheet_two->fromArray($headers);


        // build up rows
        $rowID = 2;
        foreach ($worksheet_two_jobs as $worksheet_two_job) {
            $columnID = 'A';
            $row = array();
            $row[] = $worksheet_two_job['Client']['name'];
            $row[] = $worksheet_two_job['Job']['job_ref'];
            $row[] = $worksheet_two_job['Job']['order_number'];

            for ($i = 0; $i < $supplierCols; $i++) {
                    $row[] = !empty($worksheet_two_job['Visit'][$i]) ? $worksheet_two_job['Visit'][$i]['Supplier']['name'] : null;

                    $row[] = !empty($worksheet_two_job['Visit'][$i]) ? $worksheet_two_job['Visit'][$i]['start_date'] : null;
                    $row[] = !empty($worksheet_two_job['Visit'][$i]) ?  $worksheet_two_job['Visit'][$i]['start_date'] : null;
                    $row[] = !empty($worksheet_two_job['Visit'][$i]) ? $worksheet_two_job['Visit'][$i]['end_date'] : null;
                    $row[] = !empty($worksheet_two_job['Visit'][$i]) ?  $worksheet_two_job['Visit'][$i]['end_date']) : null;
            }
}

    // insert rows
    $worksheet_two->fromArray($row, NULL, $columnID. $key);

如何才能实现第二张图片呢?

0 个答案:

没有答案