4月 18, 2022

PHPExcel 快速入門

一個已停止開發的專案PHPExcel(後繼者為 PhpSpreadsheet),尚能應付基本 Excel 讀寫操作(測試環境 PHP 7.2.9)。
require_once('PHPExcel-1.8/Classes/PHPExcel/IOFactory.php');

/* Write file */
$xlsWriter = new PHPExcel();
$xlsWriter->setActiveSheetIndex(0);
$xlsWriter->getActiveSheet()->SetCellValue('A1', '_VALUE_');
$xlsWriter->getActiveSheet()->SetCellValue('B1', '_VALUE_');
$writerObj = PHPExcel_IOFactory::createWriter($xlsWriter, 'Excel2007');
$writerObj->save("output.xlsx");

/* Read file, dump sheet into array, also see rangeToArray() */
$xlsReader = PHPExcel_IOFactory::createReader('Excel2007');
$xlsReader->setReadDataOnly(true);
$readerObj = $xlsReader->load("example.xlsx");
$sheetAry  = $readerObj->getActiveSheet()->toArray(null,true,true,true);
echo print_r($sheetAry,true);   // show array formatted

/** toArray() definition
 * @param  $nullValue          Value returned if a cell doesn't exist
 * @param  $calculateFormulas  Should formulas be calculated?
 * @param  $formatData         Should formatting be applied to cell values?
 * @param  $returnCellRef      True, Return rows and columns indexed(A1,B1,C1...)
*/

沒有留言:

張貼留言