標籤:
public function test(){ $exts = ‘xlsx‘; //匯入PHPExcel類庫,因為PHPExcel沒有用命名空間,只能inport匯入 import("Org.Util.PHPExcel");//匯入Excel類 我這裡是ThinkPHP架構寫法 根據自己的架構修改 $fileName = "tel.xls";//excel檔案路徑 $objReader = \PHPExcel_IOFactory::createReader(‘Excel5‘); $objPHPExcel = $objReader->load($fileName,$encode=‘utf-8‘); $drawing = new \PHPExcel_Writer_Excel2007_Drawing(); $drawingHashTable = new \PHPExcel_HashTable(); $drawingHashTable->addFromSource($drawing->allDrawings($objPHPExcel)); for ($i = 0; $i < $drawingHashTable->count(); ++$i) { $memoryDrawing = $drawingHashTable->getByIndex($i); if ($memoryDrawing instanceof \PHPExcel_Worksheet_MemoryDrawing) { $filename = ‘./Public/‘ . $memoryDrawing->getCoordinates() . ‘_‘ . $memoryDrawing->getHashCode() . ‘.jpg‘; // 將圖片存到指定的目錄 imagejpeg($memoryDrawing->getImageResource(), $filename); // 獲得該圖片所在的儲存格 $cell = $memoryDrawing->getWorksheet()->getCell($memoryDrawing->getCoordinates()); // 將該儲存格的值設定為儲存格的文本加片的 img 標籤 $cell->setValue($cell->getValue() . $filename); } } $sheet = $objPHPExcel->getSheet(0); $highestRow = $sheet->getHighestRow(); // 取得總行數 $highestColumn = $sheet->getHighestColumn(); // 取得總列數 for($i=2;$i<=$highestRow;$i++) { $data[$i][‘truename‘] = $objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue(); $data[$i][‘account‘]= $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue(); $data[$i][‘image‘][] = $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue(); $data[$i][‘image‘][] = $objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue(); $data[$i][‘image‘][] = $objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue(); } print_r($data);die; }
PHP讀取Excel資料寫入資料庫(包含圖片和文字)