나만 알 수 없어서 만든 블로그

CI4 codeigniter4에 PHPEcxel 사용하기 본문

PHP/고급

CI4 codeigniter4에 PHPEcxel 사용하기

코딩낙타 2021. 1. 21. 14:04

난 해냈다 결국 해냈다!!!!

 

나만 알 수 없어서 적어둔다

 

1. github.com/PHPOffice/PHPExcel

 

PHPOffice/PHPExcel

ARCHIVED. Contribute to PHPOffice/PHPExcel development by creating an account on GitHub.

github.com

zip 파일 다운받음

(Classes 폴더 중요!!!)

 

2. 아래 폴더 내용을 [ThirdParty]폴더로 옮김

 

ThirdParty 폴더에 넣기!

3.  Config > Autoload.php 

 

 Autoload.php 에 추가

/**
		 * -------------------------------------------------------------------
		 * Class Map
		 * -------------------------------------------------------------------
		 * The class map provides a map of class names and their exact
		 * location on the drive. Classes loaded in this manner will have
		 * slightly faster performance because they will not have to be
		 * searched for within one or more directories as they would if they
		 * were being autoloaded through a namespace.
		 *
		 * Prototype:
		 *
		 *   $Config['classmap'] = [
		 *       'MyClass'   => '/path/to/class/file.php'
		 *   ];
		 */
		$classmap = [
			'PHPExcel' => APPPATH.'ThirdParty/PHPExcel/PHPExcel.php'
		];

		//--------------------------------------------------------------------
		// Do Not Edit Below This Line
		//--------------------------------------------------------------------

		$this->psr4     = array_merge($this->psr4, $psr4);
		$this->classmap = array_merge($this->classmap, $classmap);

		unset($psr4, $classmap);
	}

	//--------------------------------------------------------------------

4. PHPExcel을 사용할 컨트롤러에 use와 함께 선언!

 

5. 컨트롤러에 코드 넣고 끝!

				//input type=file의 name
                $avatar = $this->request->getFile('input type=file의 name');

				//파일이름과 경로를 구한다.
                $pathFilename = $avatar->getPathname();
                
                $objPHPExcel = new PHPExcel();
                // $objPHPExcel = PHPExcel_IOFactory::load($pathFilename);

                try {

                    // 업로드 된 엑셀 형식에 맞는 Reader객체를 만든다.
                    $objReader = PHPExcel_IOFactory::createReaderForFile($pathFilename);

                    // 읽기전용으로 설정
                    // $objReader->setReadDataOnly(true);

                    // 엑셀파일을 읽는다
                    $objExcel = $objReader->load($pathFilename);

                    // 첫번째 시트를 선택
                    $objExcel->setActiveSheetIndex(0);
                    $objWorksheet = $objExcel->getActiveSheet();
                    $rowIterator = $objWorksheet->getRowIterator();

                    foreach ($rowIterator as $row) {
                        $cellIterator = $row->getCellIterator();
                        $cellIterator->setIterateOnlyExistingCells(false);
                    }

                    $maxRow = $objWorksheet->getHighestRow();

                    // echo $maxRow . "<br>";
                    $slangTxt = "";
                    for ($i = 2; $i <= $maxRow; $i++) { 
                        // $i = 0은 0번째줄부터 출력

                        $a = $objWorksheet->getCell('A' . $i)->getValue(); // A열
                        // $b = $objWorksheet->getCell('B' . $i)->getValue(); // B열 
                        // $c = $objWorksheet->getCell('C' . $i)->getValue(); // C열 
                        // $d = $objWorksheet->getCell('D' . $i)->getValue(); // D열
                        // $e = $objWorksheet->getCell('E' . $i)->getValue();  // E열 
                        // $f = $objWorksheet->getCell('F' . $i)->getValue(); // F열
                        // $g = $objWorksheet->getCell('G' . $i)->getValue(); // G열 
                        // $h = $objWorksheet->getCell('H' . $i)->getValue(); // H열 
                        // // 날짜 형태의 셀을 읽을때는 toFormattedString를 사용한다.
                        // $h = PHPExcel_Style_NumberFormat::toFormattedString($h, 'YYYY-MM-DD');
                        //   echo $a . " / " . $b. " / " . $c . " / " . $d . " / " . $e . " / " . $f . " / " . $g . " <br>\n";
                        $a  = addslashes($a);
                        $slangTxt .= $a . ",";
                        // $b  = addslashes($b);
                        // $c  = addslashes($c);
                        // $d  = addslashes($d);
                        // $e  = addslashes($e);
                        // $f  = addslashes($f);
                        // $g  = addslashes($g);
                        // var_dump($a);
                        // $query = "insert into 삽입할 테이블 (continent,country_code,country,city_code,city,use_yn) values ('$b','$c','$d','$e','$f','$g')";
                        // mysql_query($query) or die("Insert Error !");
                    }
                    // echo $maxRow - 1 . " Data inserting finished !";
                } catch (exception $e) {
                    echo '엑셀파일을 읽는도중 오류가 발생하였습니다.!';
                }

'PHP > 고급' 카테고리의 다른 글

Mysqpl DB랑 연결하고 나온 결과를 어떻게 할까?  (0) 2020.08.26
$_FILES 이것은 무엇인가...?  (0) 2020.07.17
move_uploaded_file()란?  (1) 2020.07.17