如何在PHPexcel中限制行数组

时间:2014-02-11 18:42:15

标签: php html mysql phpexcel

任何人都可以帮助如何限制特定名称(数组)是否为空,重新编码也是空的...因为我在我的Excel报告中遇到了这种问题。

尽管我在表单中只填写了一行,但重复代码从1行保存到10行。

enter image description here

当前代码:

<?php
if(isset($_POST['send'])){
    include('connection.php');
    require_once 'Classes\PHPExcel\IOFactory.php';
    $filename = 'worksheet.xlsx';
    $position = $_POST['position'];
    $division = $_POST['division'];
    $title = $position;

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

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


    $repcode1 = isset($_POST['repcode']) ? $_POST['repcode'] : "";
    $aic1 = isset($_POST['aic1']) ? $_POST['aic1'] : "";
    $name1 = isset($_POST['name1']) ? $_POST['name1'] : "";
    $optA1 = isset($_POST['optA1']) ? $_POST['optA1'] : "";
    $optB1 = isset($_POST['optB1']) ? $_POST['optB1'] : "";
    $optC1 = isset($_POST['optC1']) ? $_POST['optC1'] : "";
    $optD1 = isset($_POST['optD1']) ? $_POST['optD1'] : "";
    $total1 = isset($_POST['total1']) ? $_POST['total1'] : "";
    $remarks1 = isset($_POST['remarks1']) ? $_POST['remarks1'] : "";

    $repcode2 = isset($_POST['repcode']) ? $_POST['repcode'] : "";
    $aic2 = isset($_POST['aic2']) ? $_POST['aic2'] : "";
    $name2 = isset($_POST['name2']) ? $_POST['name2'] : "";
    $optA2 = isset($_POST['optA2']) ? $_POST['optA2'] : "";
    $optB2 = isset($_POST['optB2']) ? $_POST['optB2'] : "";
    $optC2 = isset($_POST['optC2']) ? $_POST['optC2'] : "";
    $optD2 = isset($_POST['optD2']) ? $_POST['optD2'] : "";
    $total2 = isset($_POST['total2']) ? $_POST['total2'] : "";
    $remarks2 = isset($_POST['remarks2']) ? $_POST['remarks2'] : "";

    $repcode3 = isset($_POST['repcode']) ? $_POST['repcode'] : "";
    $aic3 = isset($_POST['aic3']) ? $_POST['aic3'] : "";
    $name3 = isset($_POST['name3']) ? $_POST['name3'] : "";
    $optA3 = isset($_POST['optA3']) ? $_POST['optA3'] : "";
    $optB3 = isset($_POST['optB3']) ? $_POST['optB3'] : "";
    $optC3 = isset($_POST['optC3']) ? $_POST['optC3'] : "";
    $optD3 = isset($_POST['optD3']) ? $_POST['optD3'] : "";
    $total3 = isset($_POST['total3']) ? $_POST['total3'] : "";
    $remarks3 = isset($_POST['remarks3']) ? $_POST['remarks3'] : "";

    $repcode4 = isset($_POST['repcode']) ? $_POST['repcode'] : "";
    $aic4 = isset($_POST['aic4']) ? $_POST['aic4'] : "";
    $name4 = isset($_POST['name4']) ? $_POST['name4'] : "";
    $optA4 = isset($_POST['optA4']) ? $_POST['optA4'] : "";
    $optB4 = isset($_POST['optB4']) ? $_POST['optB4'] : "";
    $optC4 = isset($_POST['optC4']) ? $_POST['optC4'] : "";
    $optD4 = isset($_POST['optD4']) ? $_POST['optD4'] : "";
    $total4 = isset($_POST['total4']) ? $_POST['total4'] : "";
    $remarks4 = isset($_POST['remarks4']) ? $_POST['remarks4'] : "";

    $repcode5 = isset($_POST['repcode']) ? $_POST['repcode'] : "";
    $aic5 = isset($_POST['aic5']) ? $_POST['aic5'] : "";
    $name5 = isset($_POST['name5']) ? $_POST['name5'] : "";
    $optA5 = isset($_POST['optA5']) ? $_POST['optA5'] : "";
    $optB5 = isset($_POST['optB5']) ? $_POST['optB5'] : "";
    $optC5 = isset($_POST['optC5']) ? $_POST['optC5'] : "";
    $optD5 = isset($_POST['optD5']) ? $_POST['optD5'] : "";
    $total5 = isset($_POST['total5']) ? $_POST['total5'] : "";
    $remarks5 = isset($_POST['remarks5']) ? $_POST['remarks5'] : "";

    $repcode6 = isset($_POST['repcode']) ? $_POST['repcode'] : "";
    $aic6 = isset($_POST['aic6']) ? $_POST['aic6'] : "";
    $name6 = isset($_POST['name6']) ? $_POST['name6'] : "";
    $optA6 = isset($_POST['optA6']) ? $_POST['optA6'] : "";
    $optB6 = isset($_POST['optB6']) ? $_POST['optB6'] : "";
    $optC6 = isset($_POST['optC6']) ? $_POST['optC6'] : "";
    $optD6 = isset($_POST['optD6']) ? $_POST['optD6'] : "";
    $total6 = isset($_POST['total6']) ? $_POST['total6'] : "";
    $remarks6 = isset($_POST['remarks6']) ? $_POST['remarks6'] : "";

    $repcode7 = isset($_POST['repcode']) ? $_POST['repcode'] : "";
    $aic7 = isset($_POST['aic7']) ? $_POST['aic7'] : "";
    $name7 = isset($_POST['name7']) ? $_POST['name7'] : "";
    $optA7 = isset($_POST['optA7']) ? $_POST['optA7'] : "";
    $optB7 = isset($_POST['optB7']) ? $_POST['optB7'] : "";
    $optC7 = isset($_POST['optC7']) ? $_POST['optC7'] : "";
    $optD7 = isset($_POST['optD7']) ? $_POST['optD7'] : "";
    $total7 = isset($_POST['total7']) ? $_POST['total7'] : "";
    $remarks7 = isset($_POST['remarks7']) ? $_POST['remarks7'] : "";

    $repcode8 = isset($_POST['repcode']) ? $_POST['repcode'] : "";
    $aic8 = isset($_POST['aic8']) ? $_POST['aic8'] : "";
    $name8 = isset($_POST['name8']) ? $_POST['name8'] : "";
    $optA8 = isset($_POST['optA8']) ? $_POST['optA8'] : "";
    $optB8 = isset($_POST['optB8']) ? $_POST['optB8'] : "";
    $optC8 = isset($_POST['optC8']) ? $_POST['optC8'] : "";
    $optD8 = isset($_POST['optD8']) ? $_POST['optD8'] : "";
    $total8 = isset($_POST['total8']) ? $_POST['total8'] : "";
    $remarks8 = isset($_POST['remarks8']) ? $_POST['remarks8'] : "";

    $repcode9 = isset($_POST['repcode']) ? $_POST['repcode'] : "";
    $aic9 = isset($_POST['aic9']) ? $_POST['aic9'] : "";
    $name9 = isset($_POST['name9']) ? $_POST['name9'] : "";
    $optA9 = isset($_POST['optA9']) ? $_POST['optA9'] : "";
    $optB9 = isset($_POST['optB9']) ? $_POST['optB9'] : "";
    $optC9 = isset($_POST['optC9']) ? $_POST['optC9'] : "";
    $optD9 = isset($_POST['optD9']) ? $_POST['optD9'] : "";
    $total9 = isset($_POST['total9']) ? $_POST['total9'] : "";
    $remarks9 = isset($_POST['remarks9']) ? $_POST['remarks9'] : "";

    $repcode10 = isset($_POST['repcode']) ? $_POST['repcode'] : "";
    $aic10 = isset($_POST['aic10']) ? $_POST['aic10'] : "";
    $name10 = isset($_POST['name10']) ? $_POST['name10'] : "";
    $optA10 = isset($_POST['optA10']) ? $_POST['optA10'] : "";
    $optB10 = isset($_POST['optB10']) ? $_POST['optB10'] : "";
    $optC10 = isset($_POST['optC10']) ? $_POST['optC10'] : "";
    $optD10 = isset($_POST['optD10']) ? $_POST['optD10'] : "";
    $total10 = isset($_POST['total10']) ? $_POST['total10'] : "";
    $remarks10 = isset($_POST['remarks10']) ? $_POST['remarks10'] : "";

    $result = array(
    array(
        $aic1,
        $repcode,
        $name1,
        $optA1,
        $optB1,
        $optC1,
        $optD1,
        $total1,
        $remarks1
    ),
    array(
        $aic2,
        $repcode,
        $name2,
        $optA2,
        $optB2,
        $optC2,
        $optD2,
        $total2,
        $remarks2
    ),
    array(
        $aic3,
        $repcode,
        $name3,
        $optA3,
        $optB3,
        $optC3,
        $optD3,
        $total3,
        $remarks3
    ),
    array(
        $aic4,
        $repcode,
        $name4,
        $optA4,
        $optB4,
        $optC4,
        $optD4,
        $total4,
        $remarks4
    ),
    array(
        $aic5,
        $repcode,
        $name5,
        $optA5,
        $optB5,
        $optC5,
        $optD5,
        $total5,
        $remarks5
    ),
    array(
        $aic6,
        $repcode,
        $name6,
        $optA6,
        $optB6,
        $optC6,
        $optD6,
        $total6,
        $remarks6
    ),
    array(
        $aic7,
        $repcode,
        $name7,
        $optA7,
        $optB7,
        $optC7,
        $optD7,
        $total7,
        $remarks7
    ),
    array(
        $aic8,
        $repcode,
        $name8,
        $optA8,
        $optB8,
        $optC8,
        $optD8,
        $total8,
        $remarks8
    ),
    array(
        $aic9,
        $repcode,
        $name9,
        $optA9,
        $optB9,
        $optC9,
        $optD9,
        $total9,
        $remarks9
    ),
    array(
        $aic10,
        $repcode,
        $name10,
        $optA10,
        $optB10,
        $optC10,
        $optD10,
        $total10,
        $remarks10
    )
);


        $headings = array(
            'AIC',
            'RepCode',
            'NAME',
            'Gen Info. & Technical Knowledge',
            'Communication Ability',
            'Attitude Towards Profession',
            'Appearance',
            'TOTAL',
            'Remarks'
        );
        $points = array(
            ' ',
            ' ',
            ' ',
            '(3 pts)',
            '(3 pts)',
            '(2 pts)',
            '(2 pts)',
            '(10 pts)',
            ' '
        );
        $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:I14')
             ->getAlignment()
             ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A1:I1')->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($points, null, 'A2');
        $row = 3;
        foreach($result as $rows){
            $objPHPExcel->getActiveSheet()->fromArray($rows, null, 'A' . $row);
            $row++;
        }


    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="'.$title.'.xlsx"');
    header('Cache-Control: max-age=0');

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

1 个答案:

答案 0 :(得分:1)

这样的东西
    foreach($result as $rows){
        if (count(array_filter($rows)) > 0) {
            $objPHPExcel->getActiveSheet()->fromArray($rows, null, 'A' . $row);
            $row++;
        }
    }

也许?

修改

或者

    foreach($result as $rows){
        if (!empty($rows[2])) {
            $objPHPExcel->getActiveSheet()->fromArray($rows, null, 'A' . $row);
            $row++;
        }
    }