如何限制phpexcel中的循环

时间:2014-02-23 18:20:43

标签: php html mysql phpexcel

任何人都可以帮助我如何限制循环,如果变量$name为空,即使其他数组在该数组中不为空,它也不会显示在excel生成的报告中。

我想要做的是,如果该循环中的特定数组变量$name是空的,即使其他变量不是它们也不会包含在生成的excel报告中....有谁知道怎么做?请帮帮我。

当前代码:

<?php
if(isset($_POST['send'])){
    include('connection.php');
    require_once 'Classes\PHPExcel\IOFactory.php';
    $filename = 'qualification_standards.xlsx';
    $repcode = $_POST['repcode'];
    $position = $_POST['t_position'];
    $town = $_POST['t_town'];
    $title = 'Qualification-Standards'.'_'.$repcode.'_'.$position.'_'.$town;

    $objReader = PHPExcel_IOFactory::createReader('Excel2007');
    $objReader->setReadDataOnly(true);

    $objPHPExcel = $objReader->load($filename);
    $objWorksheet = $objPHPExcel->getActiveSheet();
    $objWorksheet = $objPHPExcel->setActiveSheetIndex(0);


    $result = array();
       for ( $n = 1; $n <=15; $n++ )
            {
        $no = 0+$n;
        $aic = isset($_POST['aic'.$n]) ? $_POST['aic'.$n] : "";
        $repcode = isset($_POST['repcode']) ? $_POST['repcode'] : "";
        $name = isset($_POST['name'.$n]) ? $_POST['name'.$n] : "";
        $optA = isset($_POST['optA'.$n]) ? $_POST['optA'.$n] : "";
        $optB = isset($_POST['optB'.$n]) ? $_POST['optB'.$n] : "";
        $optC = isset($_POST['optC'.$n]) ? $_POST['optC'.$n] : "";
        $optD = isset($_POST['optD'.$n]) ? $_POST['optD'.$n] : "";
        $total = isset($_POST['total'.$n]) ? $_POST['total'.$n] : "";

        $result[] = array(
              $no,
              "PRO13325-".$aic,
              $repcode,
              $name,
              $optA,
              $optB,
              $optC,
              $optD,
              $total
            );
        }


          $headings = array(array('QUALIFICATION STANDARDS'),array(
            NULL,
            NULL,
            NULL,
            NULL,
            NULL,
            NULL,
            NULL,
            NULL,
            NULL), array(
            'No.',
            'AIC',
            'RepCode',
            'Name of Candidates',
            'Educational Attainment',
            'Experience',
            'In-Service Trainings & Seminars Attended',
            'Eligibility',
            'Total'
        )
        );
        $fone = "___________________________";
        $ftwo = "Signature of Rater";
        $footer = array(array($fone),array($ftwo));

        $points = array(
            ' ',
            ' ',
            ' ',
            ' ',
            '(20 pts)',
            '(20 pts)',
            '(20 pts)',
            '(20 pts)',
            '(80 pts)'
        );

        $styleArray = array(
         'borders' => array(
         'allborders' => array(
         'style' => PHPExcel_Style_Border::BORDER_THIN
             )
          )
        );

        $objPHPExcel->getActiveSheet()->getStyle('A3:I19')->applyFromArray($styleArray);
        unset($styleArray);

        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setAutoSize(true);

        $objPHPExcel->getActiveSheet()
             ->getStyle('A1:I21')
             ->getAlignment()
             ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()
             ->getStyle('D5:D19')
             ->getAlignment()
             ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);

        $objPHPExcel->getActiveSheet()->getStyle('A1:I1')->getFont()->setSize(22);
        $objPHPExcel->getActiveSheet()->mergeCells('A1:I2');
        $objPHPExcel->getActiveSheet()->getStyle('A20:I20')->getFont()->setSize(17);
        $objPHPExcel->getActiveSheet()->getStyle('A21:I21')->getFont()->setSize(13);
        $objPHPExcel->getActiveSheet()->mergeCells('A20:I20');
        $objPHPExcel->getActiveSheet()->mergeCells('A21:I21');
        $objPHPExcel->getActiveSheet()->getStyle('B5:B19')->getNumberFormat()->setFormatCode('0000');
        $objPHPExcel->getActiveSheet()->getStyle('A3:I3')->applyFromArray(
            array(
                'font' => array(
                    'bold' => true
                ),
                'alignment' => array(
                    'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
                ),
                'borders' => array(
                    'top' => array(
                        'style' => PHPExcel_Style_Border::BORDER_THIN
                    )
                ),
                'fill' => array(
                    'type' => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR,
                    'rotation' => 90,
                    'startcolor' => array(
                        'argb' => 'FFA0A0A0'
                    ),
                    'endcolor' => array(
                        'argb' => 'FFFFFFFF'
                    )
                )
            )
    );
        $objPHPExcel->getActiveSheet()->fromArray($headings, null, 'A1');
        $objPHPExcel->getActiveSheet()->fromArray($footer, null, 'A20');
        $objPHPExcel->getActiveSheet()->fromArray($points, null, 'A4');
        $row = 5;
        foreach($result as $rows){
        if (!empty($rows[4])) {
            $objPHPExcel->getActiveSheet()->fromArray($rows, null, 'A' . $row);
            $row++;
        }
    }


    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header(sprintf('Content-Disposition: attachment;filename="%s.xlsx"',preg_replace('/\\r\\n?|\\n/', '', $title)));
    header('Cache-Control: max-age=0');

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    $objWriter->save('php://output');
    exit;
}?>  

1 个答案:

答案 0 :(得分:0)

 for ( $n = 1; $n <=15; $n++ ){

    $name = isset($_POST['name'.$n]) ? $_POST['name'.$n] : "";
    if(!$name)
    {
        continue;
    } 

    $no = 0+$n;
    $aic = isset($_POST['aic'.$n]) ? $_POST['aic'.$n] : "";
    $repcode = isset($_POST['repcode']) ? $_POST['repcode'] : "";
    // $name = isset($_POST['name'.$n]) ? $_POST['name'.$n] : "";
    $optA = isset($_POST['optA'.$n]) ? $_POST['optA'.$n] : "";
    $optB = isset($_POST['optB'.$n]) ? $_POST['optB'.$n] : "";
    $optC = isset($_POST['optC'.$n]) ? $_POST['optC'.$n] : "";
    $optD = isset($_POST['optD'.$n]) ? $_POST['optD'.$n] : "";
    $total = isset($_POST['total'.$n]) ? $_POST['total'.$n] : "";

    $result[] = array(
        $no,
        "PRO13325-".$aic,
        $repcode,
        $name,
        $optA,
        $optB,
        $optC,
        $optD,
        $total
    );
}