PHP 读取EXCEL
Posted 浅痕zlx
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP 读取EXCEL相关的知识,希望对你有一定的参考价值。
phpExcel 自己下载。
PHP读取EXCEL
public function import_Excel($file_name){ include_once LIB_ROOT_PATH."3rdParty/phpexcel/PHPExcel.php"; $objReader = PHPExcel_IOFactory::createReader(‘Excel2007‘); $objPHPExcel = $objReader->load($file_name,$encode=‘utf-8‘); $sheet = $objPHPExcel->getSheet(0); $highestRow = $sheet->getHighestRow();//取得总行数 $highestColumn = $sheet->getHighestColumn();//取得总列数 $data = array(); for($i=2;$i<=$highestRow;$i++){ for($j=‘A‘;$j<=$highestColumn;$j++){ $data[$i][] = $objPHPExcel->getActiveSheet()->getCell("$j$i")->getValue(); } } return $data; }
PHP导出EXCEL
public function data_excel(){ include_once LIB_ROOT_PATH."3rdParty/phpexcel/PHPExcel.php"; $objPHPExcel = new PHPExcel(); $objPHPExcel->setActiveSheetIndex(0) ->setCellValue(‘A1‘, ‘相近字‘) ->setCellValue(‘B1‘, ‘可能的字‘); $i = 0; foreach ($temp as $key => $value) { $i++; $objPHPExcel->getActiveSheet(0)->setCellValue(‘A‘.($i+1), $key); $objPHPExcel->getActiveSheet(0)->setCellValue(‘B‘.($i+1), $value); } // sheet命名 $objPHPExcel->getActiveSheet()->setTitle(‘易错字统计‘); $objPHPExcel->setActiveSheetIndex(0); // excel头参数 header(‘Content-Type: application/vnd.ms-excel‘); header(‘Content-Disposition: attachment;filename="(‘.date(‘Ymd-His‘).‘).xlsx"‘); //日期为文件名后缀 header(‘Cache-Control: max-age=0‘); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, ‘Excel2007‘); //excel5为xls格式,excel2007为xlsx格式 $objWriter->save(‘php://output‘); exit; }
以上是关于PHP 读取EXCEL的主要内容,如果未能解决你的问题,请参考以下文章