아래 글에서 소개한 내용처럼 셀에 값을 쓰기 전에 개별적으로 서식을 설정할 수 있다.
그런데 엑셀에 값을 쓸 때 활용하기 유용한 메서드인 fromArray() 는 배열 변수 그대로 전부 넣기 때문에 세부적인 셀 서식 설정이 불가능하다.
구글링 결과 다른 방법이 없다는것을 확인했고 결국 메서드 자체를 수정해야 했다.
해당 메서드 파일
lib/PHPExcel/Worksheet.php
// public function fromArray($source = null, $nullValue = null, $startCell = 'A1', $strictNullComparison = false) // 기존 public function fromArray($source = null, $nullValue = null, $startCell = 'A1', $strictNullComparison = false, $formattedCell = null) // 수정 { if (is_array($source)) { // Convert a 1-D array to 2-D (for ease of looping) if (!is_array(end($source))) { $source = array($source); } // start coordinate list ($startColumn, $startRow) = PHPExcel_Cell::coordinateFromString($startCell); // Loop through $source foreach ($source as $rowData) { $currentColumn = $startColumn; foreach ($rowData as $cellValue) { // 추가 시작 if ($formattedCell != null && $currentColumn == $formattedCell) { $this->getStyle($currentColumn . $startRow)->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_TEXT ); } // 추가 끝 if ($strictNullComparison) { if ($cellValue !== $nullValue) { // Set cell value $this->getCell($currentColumn . $startRow)->setValue($cellValue); } } else { if ($cellValue != $nullValue) { // Set cell value $this->getCell($currentColumn . $startRow)->setValue($cellValue); } } ++$currentColumn; } ++$startRow; } } else { throw new PHPExcel_Exception("Parameter \$source should be an array."); } return $this; }
파라미터에 서식을 설정할 셀의 열 번호를 추가하였고, 본문에서는 FORMAT_TEXT(@)로 설정하였다.
파라미터를 배열로 변경해서 열 정보와 서식(format) 정보를 쌍으로 넘기면 다양한 셀 서식 설정이 가능하다.
끝.