PhpSpreadsheet

PhpSpreadsheet is the next version of PHPExcel. It breaks compatibility to dramatically improve the code base quality (namespaces, PSR compliance, use of latest PHP language features, etc.).

Because all efforts have shifted to PhpSpreadsheet, PHPExcel will no longer be maintained. All contributions for PHPExcel, patches and new features, should target PhpSpreadsheet develop branch.

Git:

https://github.com/PHPOffice/PhpSpreadsheet

Doc:

https://phpspreadsheet.readthedocs.io/en/develop/

API Doc:

https://phpoffice.github.io/PhpSpreadsheet/master/PhpOffice/PhpSpreadsheet/Cell/DataType.html

Example:

	public function download($form_id){
		$form = $this->form_m->item($form_id);

		$users = $this->form_fields_m->responsed_users_full($form_id);
		//logg($users);

		$fields = $this->config->item('member_download_fields');
		$options = $this->config->item('member_options');
		//logg($fields);

		$spreadsheet = new Spreadsheet();
		$sheet = $spreadsheet->getActiveSheet();

		//Write heading
		$row_index = 1;
		$col_index = 1;
		foreach ($fields as $key => $field) {
			$sheet->setCellValueByColumnAndRow($col_index, $row_index, $field[0]);
			
			$col_index++;
		}
		$row_index++;

		foreach ($users as $key => $user) {
			$col_index = 1;
			foreach ($fields as $key2 => $field) {
				$value = val($user, $key2);

				if($key2 == 'area'){
					$value = val($options['area'], $value);
				}

				if($key2 == 'gender'){
					$value = val($options['gender'], $value);
				}

				if($key2 == 'occupation'){
					$value = val($options['occupation'], $value);
				}

				if($key2 == 'work_env'){
					$value = val($options['work_env'], $value);
				}

				if($key2 == 'face_q'){
					$value = val($options['face_q'], $value);
				}

				if($key2 == 'skin_q'){
					$value = val($options['skin_q'], $value);
				}

				if($key2 == 'hair_q'){
					if($value != false){
						$value = $this->convert_json($value, $options['hair_q']);
					}
				}

				if($key2 == 'drama'){
					if($value != false){
						$value = $this->convert_json($value, $options['drama']);
					}
				}

				if(in_array($key, ['phone']) == true){
					$sheet->setCellValueExplicitByColumnAndRow($col_index, $row_index, $value, DataType::TYPE_STRING);
				}else{
					$sheet->setCellValueByColumnAndRow($col_index, $row_index, $value);
				}

				$col_index++;
			}

			$row_index++;
		}
		
		$writer = new Xlsx($spreadsheet);
		$file_name = 'survey_members.xlsx';
		$writer->save(FCPATH . 'uploads_temp/' . $file_name);

		$this->success_response(base_url() . 'uploads_temp/' . $file_name);
	}

	private function convert_json($json, $options){
		if($json == false){
			return null;
		}

		$json = json_decode($json);
		$data = [];
		foreach ($json as $key => $id) {
			$value = val($options, $id);
			if($value != false){
				$data[] = $value;
			}
		}

		return implode(';', $data);
	}