PHPExcel 讀取文字+圖片,保存數據,儲存圖片的例子

今天在項目開發時,忽然遇到了一個問題,就是一個excel須要保存文字和圖片,把文字和圖片分別存入數據庫,而後就google了一下,通過分析,弄出了一個簡單的案例,本身在這裏mark一下。php

思路以下:先獲取圖片,保存到本地,而後把圖片替換成圖片名稱,而後再遍歷數據,存儲數據數據庫

去PHPExcel下載:https://phpexcel.codeplex.com/ui

$xlsFile = 'a.xlsx';
require_once 'PHPExcel/Reader/Excel2007.php';
$PHPReader = new PHPExcel_Reader_Excel2007();
//$objReader->setReadDataOnly(true);
$PHPExcel = $PHPReader->load($xlsFile);


$objWorksheet = $PHPExcel->getActiveSheet();

foreach ($objWorksheet->getDrawingCollection() as $drawing) {
    //for XLSX format
    $string = $drawing->getCoordinates();
    $coordinate = PHPExcel_Cell::coordinateFromString($string);
    if ($drawing instanceof PHPExcel_Worksheet_Drawing){
        $filename = $drawing->getPath();
        $drawing->getDescription();
        copy($filename, 'uploads/' . $drawing->getDescription());
        $cell = $objWorksheet->getCell($string);
        $cell->setValue($drawing->getDescription());
    }   
}

$currentSheet = $PHPExcel->getSheet(0);
$allColumn = $currentSheet->getHighestColumn();
$allRow = $currentSheet->getHighestRow();
for($rowIndex=1;$rowIndex<=$allRow;$rowIndex++){
    for($colIndex='A';$colIndex<=$allColumn;$colIndex++){
        $addr = $colIndex.$rowIndex;
        $cell = $currentSheet->getCell($addr)->getValue();
        if($cell instanceof PHPExcel_RichText){
            $cell = $cell->__toString();
            echo $cell;
        }   
    }   
}
相關文章
相關標籤/搜索