Export to xls using php

时间:2017-05-16 09:21:56

标签: php csv export

I want to lock the first three columns: Student id , School id, and Student Name, so that, the respective teacher can add only marks obtained in the exported file. How can I do that?

 public function actionStddetails(){
    $api = new ApiRequest();
    $session = Yii::$app->session;
    $url = '/api/student/viewstudent';
    $payload = "{'subject_id':'245','esd_id':'2','class_id':'5','qfe_id':'1','exam_id':'3','ref_id':'1','wac':'2','school_id':'2'}";
    $url = '/api/examscore/studentlist';
    $result = $api->request($url, $payload);
    $stddtls = $result->Table1;
    $csv_header='';
    $csv_header .= 'Student id,'.'School id,'.'Student Name,'.'Marks Obtained,'."\n";
    // loop over the rows, outputting them
    $csv_row ='';
    foreach ($stddtls as $stddtl){
        $csv_row .= '"' . $stddtl->stud_id . '",'.'"' . $stddtl->school_id . '",'.'"' . $stddtl->stud_name. '",'."\n";
    }
    /* Download as CSV File */
    header('Content-type: application/csv');
    header('Content-Disposition: attachment; filename=toy_csv.csv');
    echo $csv_header . $csv_row;

}

2 个答案:

答案 0 :(得分:0)

使用CSV文件无法做到这一点,因为它只是纯文本 - 仅此而已。如果要对特定字段进行写保护,则必须使用其他方法。

你可以,例如创建一个web界面,其中只能更改标记的值,而只显示其他值,但不能修改。

答案 1 :(得分:0)

它适用于此。但是使用xls文档

public function actionStddetails(){
    $api = new ApiRequest();
    $session = Yii::$app->session;
    $url = '/api/student/viewstudent';
    $payload = "{'subject_id':'245','esd_id':'2','class_id':'5',
   'qfe_id':'1','exam_id':'3','ref_id':'1',
   'wac':'2','school_id':'2'}";
    $url = '/api/examscore/studentlist';
    $result = $api->request($url, $payload);
    $stddtls = $result->Table1;
    //var_dump($stddtls);exit;
    try {
        $objPHPExcel = new PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);

        // Initialise the Excel row number
        $rowCount = 1;
        //start of printing column names as names of MySQL fields

        $column = 'A';
        $result = ['Student id','School id','Student Name','Marks 
        Obtained'];
        for ($i = 0; $i < count($result); $i++)
        {
            $objPHPExcel->getActiveSheet()->setCellValue($column.$rowCount, 
          $result[$i]);
            $column++;
        }
        $rowCount = 2;
        //$column = 'A';
        foreach ($stddtls as $stddtl)
        {
            $objPHPExcel->getActiveSheet()->setCellValue('A'.$rowCount, 
                          $stddtl->stud_id);
             $objPHPExcel->getActiveSheet()->setCellValue('B'.$rowCount, 
                          $stddtl->school_id);
             $objPHPExcel->getActiveSheet()->setCellValue('C'.$rowCount, 
                          $stddtl->stud_name);
            $objPHPExcel->getActiveSheet()->setCellValue('D'.$rowCount, 
                           "");
             $rowCount++;
        }
        $count = count($stddtls)+1;
        $objSheet = $objPHPExcel->getActiveSheet();
        $objSheet->getProtection()->setPassword('hello');
        $objSheet->getProtection()->setSheet(true);
        $objSheet->getStyle('D1:D'.$count)->getProtection()-
         >setLocked(\PHPExcel_Style_Protection::PROTECTION_UNPROTECTED);
        $objSheet->getProtection()->setSheet(true);
        // Redirect output to a client’s web browser (Excel5)
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="results.xls"');
        header('Cache-Control: max-age=0');
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 
                    'Excel5');
        $objWriter->save('php://output');
        }
        catch(\yii\base\Exception $e){
         var_dump($e);
        }

     }