excel上傳--phpExcel讀取xls、xlsx

$tmp_file = $_FILES ['excel'] ['tmp_name']; //臨時文件
$name = $_FILES['excel']['name']; //上傳文件名

根據上傳類型作不一樣處理
if ($file_type == 'xls') {
$reader = PHPExcel_IOFactory::createReader('Excel5'); //設置以Excel5格式(Excel97-2003工做簿)
}
if ($file_type == 'xlsx') {
$reader = new PHPExcel_Reader_Excel2007();
}

讀excel文件
$PHPExcel = $reader->load($tmp_file, 'utf-8'); // 載入excel文件
$sheet = $PHPExcel->getSheet(0); // 讀取第一個工做表
$highestRow = $sheet->getHighestRow(); // 取得總行數
$highestColumm = $sheet->getHighestColumn(); // 取得總列數

把Excel數據保存數組中
$data = array();
for ($rowIndex = 1; $rowIndex <= $highestRow; $rowIndex++) { //循環讀取每一個單元格的內容。注意行從1開始,列從A開始
for ($colIndex = 'A'; $colIndex <= $highestColumm; $colIndex++) {
$addr = $colIndex . $rowIndex;
$cell = $sheet->getCell($addr)->getValue();
if ($cell instanceof PHPExcel_RichText) { //富文本轉換字符串
$cell = $cell->__toString();
}
$data[$rowIndex][$colIndex] = $cell;
}
}
echo $data;根據須要的數據作處理。。。
相關文章
相關標籤/搜索