PHP讀取Excel資料寫入資料庫(包含圖片和文字)

來源:互聯網
上載者:User

標籤:

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資料寫入資料庫(包含圖片和文字)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.