将子表输出到PhpExcel的数据库表

时间:2015-12-15 16:06:08

标签: mysql codeigniter phpexcel

我有一对多关系表,目前我的问题是我难以将excel文件格式化为以下格式。下表仅用于说明我遇到的问题。

+----+--------------+--------------+-------------+---------------+ | id | company_name | company_code | employee_id | employee_name | +----+--------------+--------------+-------------+---------------+ | 1 | orange | 0001 | 1 | John | | | | | 2 | Tom | | | | | 3 | Mary | | 2 | apple | 0002 | 4 | Tim | | 3 | pear | 0003 | 5 | Jane | | | | | 6 | Paul | +----+--------------+--------------+-------------+---------------+

目前我得到的结果是

+----+--------------+--------------+-------------+---------------+ | id | company_name | company_code | employee_id | employee_name | +----+--------------+--------------+-------------+---------------+ | 1 | orange | 0001 | 1 | John | | 1 | orange | 0001 | 2 | Tom | | 1 | orange | 0001 | 3 | Mary | | 2 | apple | 0002 | 4 | Tim | | 3 | pear | 0003 | 5 | Jane | | 3 | pear | 0003 | 6 | Paul | +----+--------------+--------------+-------------+---------------+

控制器:

function _createExcel($query,$filename)
{
    $objPHPExcel = new PHPExcel();
    $objPHPExcel->getProperties()->setTitle("export")->setDescription("none");

    $objPHPExcel->setActiveSheetIndex(0);


    $fields = $query->list_fields();
    $col = 0;

    // bold for first row (heading)
    $objPHPExcel->getActiveSheet()->getStyle('1:1')->getFont()->setBold(true);

    // for headings
    foreach($fields as $field)
    {
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 1, $field);
        $col++;
    }

    //print_r($query->result());

    // for data
    $row = 3;
    foreach($query->result() as $data)
    {
        $col = 0;
        foreach ($fields as $field)
        {
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $data->$field);
            $col++;
        }
        $row++;
    }

    $objPHPExcel->setActiveSheetIndex(0);
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

    header('Content-Type: application/vnd.ms-excel');
    header('Content-Disposition: attachment;filename="'.$filename.' - '.date('dMy').'.xls"');
    header('Cache-Control: max-age=0');
    $objWriter->save('php://output');
}

function generateExcelReport($reportType)
{
    switch ($reportType)
    {
        case "pc_bc":
        {
            $this->load->model('publicationClaim_model');
            $query = $this->publicationClaim_model->getPublicationClaimQuery('bookChapter');
            $filename = "Publication Claim - Book Chapter";
            $this->_createExcel($query,$filename);
            break;
        }
        default:
        {
            echo "error";
        }
    }
}

型号:

function getPublicationClaimQuery($claimType)
{
    $this->db->select('*');
    $this->db->from('claim');
    $this->db->join('claim_evidence', 'claim.claim_id = claim_evidence.claim_id');
    $this->db->where('claim.claim_type', $claimType);
    $query = $this->db->get();

    return $query;
}

0 个答案:

没有答案