PHPExcel使所有工作表上的活动保持活动状态

时间:2016-03-21 12:21:03

标签: php codeigniter phpexcel

在我的项目中,我尝试使用PHPExcel导出包含多个工作表的Excel文件。在那里我需要锁定所有工作表中的一些字段。

但以下程序仅适用于第一张。如何使其适用于所有工作表。

function export() {
    if ($this -> session -> userdata('username') && $this -> session -> userdata('role') != 200) {
        if (!empty($_POST['btnExport'])) {
            $exportRecord = $this -> expmodel -> getExportData();
            $this -> load -> library('excel');
            $this -> load -> library('zip');


            $sheetno = 0;
            $loopcount = 0;
            $newsheet = $this -> excel -> createSheet($sheetno);
            $this -> excel -> setActiveSheetIndex(0);


            $rowCount = 1;
            foreach ($exportRecord as $export) {

                foreach ($export as $val) {
                    if (!isset($val))
                        $value = NULL;
                    elseif ($val != "")
                        $value = strip_tags($val);
                    else
                        $value = "";
                    $workObj = new PHPExcel_Worksheet();
                    $workObj -> protectCells($column . $rowCount, 'PHP');
                    $newsheet -> setCellValue($column . $rowCount, $value);
                    $column++;
                }
                $rowCount += 1;
                if (isset($exportRecord[$loopcount + 1]['tra_cp_name'])) {
                    if ($exportRecord[$loopcount + 1]['tra_cp_name'] != $exportRecord[$loopcount]['tra_cp_name']) {
                        $sheetno += 1;
                        $newone += 1;
                        $rowCount = 1;
                    }
                }

                if ($newone > 0) {
                    $newsheet = $this -> excel -> createSheet($sheetno);
                    $newSheet = 1;
                }
                $loopcount += 1;
            }
            $filename = 'export' . $filecount . '.xls'; 
            $writeObj = PHPExcel_IOFactory::createWriter($this -> excel, 'Excel5');
            $filecount += 1;
            $filearray[] = $filename;
            $writeObj -> save('./excel/' . $filename);
            $downloadedFileCount = sizeof($filearray);
            for ($i = 0; $i < $downloadedFileCount; $i++) {
                $this -> zip -> read_file('./excel/' . $filearray[$i]);
            }
            $this -> zip -> download('excursion_files.zip');
        }


    }
    else {
        $this -> session -> sess_destroy();
        redirect('backoffice', 'refresh');
    }
}

任何帮助都可以得到赞赏。

1 个答案:

答案 0 :(得分:0)

$workObj = new PHPExcel_Worksheet();
$workObj -> protectCells($column . $rowCount, 'PHP');

您正在创建一个新工作表并保护其上的单元格,但从未将该工作表附加到您的工作簿

也许您应该在$newsheet

上设置保护
相关问题