在不同的列中将输出写入Excel

时间:2018-07-09 07:47:41

标签: php excel phpexcel

要生成Excel文件,该文件将显示以下作业输出:

enter image description here

但是从我编写的代码中,我在A和B列中依次获取值。我已经从该站点引用了一些代码,但是没有用。

我的php函数代码是:

$jobs = $this->db->get('jobs')->result_array();
        $csv = array();
        foreach ($jobs as &$job) {

            $csv[] = array('');
            $train_details = $this->db->select('name, number')->get_where('trains', array('id' => $job['train']))->row_array();

            $job['train_name'] = $train_details['name'] . ' ( ' . $train_details['number'] . ' )';
            $job['job_coaches'] = $this->db->get_where('job_coaches', array('jobid' => $job['id']))->result_array();
            $csv[] = array('trainName' => $job['train_name'], date('d-M-Y h:i:s', strtotime($job['start_time'])));

            foreach ($job['job_coaches'] as $key => &$coach) {
                $coach_type = $this->Generalmodel->getfromid('coaches', 'type', $coach['coachid']);
                $coach['coachType'] = $coach_type . ' ( ' . $coach['coach_number'] . ' ) ';
                $coach['coach_itemCategories'] = $this->db->get_where('job_coaches_item_categories', array('jobid' => $job['id'], 'coach' => $coach['coachid']))->result_array();
                $csv[] = array('coachtype' => $coach['coachType']);

                foreach ($coach['coach_itemCategories'] as $k => &$itemCategories) {

                    $category_name = $this->Generalmodel->getfromid('item_categories', 'name', $itemCategories['item_category']);
                    $itemCategories['categoryName'] = $category_name . ' ' . $itemCategories['no'];
                    $csv[] = array('catename' => $itemCategories['categoryName']);

                    $itemCategories['item_categories_items'] = $this->db->get_where('job_coaches_item_categories_items', array('jobid' => $job['id'], 'coachid' => $coach['coachid'], 'job_item_category' => $itemCategories['item_category']))->result_array();
                    foreach ($itemCategories['item_categories_items'] as &$item) {
                        $item['item_name'] = $this->Generalmodel->getfromid('items', 'name', $item['item']);
                        if ($item['item_value'] == 'true') {
                            $problem = 'No problem';
                        } else {
                            $problem = ucfirst($item['problem_status']);
                        }

                        $csv[] = array($item['item_name'], $problem);
                    }
                    $csv[] = array('');
                }
            }
            $csv[] = array('');
            $csv[] = array('');
        }

        foreach ($csv as $ins => $cv) {

            if (!empty($cv['trainName'])) {
                $change = $ins + 1;
                $objPHPExcel->getActiveSheet()->getStyle('A' . $change)->applyFromArray(array('font' => array('size' => 13, 'bold' => true)));

                $objPHPExcel->getActiveSheet()->getRowDimension($change)->setRowHeight(20);
            }

            if (!empty($cv['coachtype'])) {
                $change1 = $ins + 1;
                $objPHPExcel->getActiveSheet()->getStyle('A' . $change1)->applyFromArray(array('font' => array('size' => 11, 'bold' => true)));

                $objPHPExcel->getActiveSheet()->getRowDimension($change1)->setRowHeight(20);
            }

            if (!empty($cv['catename'])) {
                $change2 = $ins + 1;
                $objPHPExcel->getActiveSheet()->getStyle('A' . $change2)->applyFromArray(array('font' => array('size' => 10, 'bold' => true)));

                $objPHPExcel->getActiveSheet()->getRowDimension($change2)->setRowHeight(15);
            }
        }

        $objPHPExcel->getActiveSheet()->fromArray($csv);
        $train_details = $this->db->get_where('trains', array('id' => $train))->row_array();
        $title = $train_details['name'];
        $objPHPExcel->getActiveSheet()->setTitle($title);

        $filename = 'Job_list_' . $daterange . '.xlsx';
        header('Content-type: application/vnd.ms-excel');
        header('Content-Disposition: attachment; filename="' . $filename . '";');
        header('Cache-Control: max-age=0');


        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

        $objWriter->save('php://output');

我应该做些什么更改,以使我在一个列空间中并排完成两个作业?

0 个答案:

没有答案