php 8.1 phpexcel 대체
기존 phpexcel 을 사용중이였는데 버전업을 진행하며 이제는 더이상 phpexcel를 사용할 수 없게되었다.
파일을 읽는건 문제 없지만 엑셀을 출력하기위해 엑셀을 쓰는 과정에서 오류가 있었다.
다음은 오류중 하나이다
Message: Return type of PHPExcel_WorksheetIterator::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice
그냥 간단하게 타입명시 오류다.
일일이 고치기도 힘들기에 이제는 더이상 phpexcel을 가지고 갈 수 없는 환경이 되어버렸다.
일반적인 대안으로는 php spread sheet가 있지만 현재 이 서버에는 설치 할 수 없는 상황이다.(루트권한이 없음)
그래서 좀 찾아본 결과 SimpleXLSXGen 이라는게 있었다.
composer로 설치하는 spreadsheet와 다르게 라이브러리를 다운로드 받아서 사용할 수 있었다.
https://github.com/shuchkin/simplexlsxgen
아래는 직접 사용한 코드다.
function ext_xlsx(){
try{
$ext_type = $this->input->post('type');
$user = @$this->session->userdata['ID'];
$and_where = " and USER=? ";
$binds['user'] = $user;
require_once '/www/application/libraries/SimpleXLSXGen.php';
$row = $this->phs_model->sel_ucg_data_detail($and_where,$binds);
// 헤더 설정
$headers = ['작품코드', '작품명', '부제목','가수'];
if($this->config->item('base_path') == ""){
echo "error";
exit;
}
$file_path = $this->config->item('base_path')."/data/${user}_excel";
$tmp_filepath = $this->config->item('base_path')."/data/";
$file_list = scandir($tmp_filepath);
if($tmp_filepath==""){
echo 'error';
exit;
}
foreach ($file_list as $item) {
if ($item == '' || $item == '.' || $item == '..') continue;
if(is_file($tmp_filepath.$item) && pathinfo($item, PATHINFO_EXTENSION)=='xlsx'){
unlink($tmp_filepath.$item);
}
}
$fileIndex = 1;
$rows = [];
$rows[] = $headers; // 헤더 추가
$prev_no = 0;
$no_change = 0;
foreach ($row as $entry){
if($prev_no!=$entry->NO){
if (($no_change % 100) == 0 && $no_change!=0 && $ext_type!='all') {
$xlsx = SimpleXLSXGen::fromArray($rows);
$filename = $file_path . $fileIndex . '.xlsx';
$xlsx->saveAs($filename);
$fileIndex++;
$rows = [$headers]; // 헤더만 남겨두고 초기화
}
$prev_no = $entry->NO;
$no_change++;
}
$fields = [
$entry->NO,
$entry->MAIN,
$entry->SUB,
$entry->ARTIST
];
$rows[] = $fields;
}
if (count($rows) > 1) { // 헤더를 제외한 데이터가 있으면
$xlsx = SimpleXLSXGen::fromArray($rows);
$filename = $file_path . $fileIndex . '.xlsx';
$xlsx->saveAs($filename);
}
$arr_result['result'] = "ok";
echo(json_encode($arr_result));
exit;
}catch (Exception $e){
$arr_result['result'] = "error";
$arr_result['msg'] = $e;
echo(json_encode($arr_result));
exit;
}
}
특정 경로의 라이브러리를 require_once로 가져와서 사용했다.
phpexcel과 사용법은 크게 다르지 않았다.
코드 자체는 서버의 특정경로의 기존 엑셀 데이터는 삭제후 새로 엑셀을 생성하도록 했다.
phpecel과 spreadsheet를 사용하지 못하는 상황에서 꽤나 유용한 대안이 되었다.