天天看點

PHPEXCEL導入EXCEL

目錄結構:

PHPEXCEL導入EXCEL

PHP源碼:

//header("Content-Type: text/html; charset=utf-8");

    include 'Classes/PHPExcel/IOFactory.php';

    $inputFileName = 'qq.xls';

    date_default_timezone_set('PRC');

    // 讀取excel檔案

    try {

    $inputFileType = PHPExcel_IOFactory::identify($inputFileName);

    $objReader = PHPExcel_IOFactory::createReader($inputFileType);

    $objPHPExcel = $objReader->load($inputFileName);

    } catch(Exception $e) {

    die('讀取錯誤'.pathinfo($inputFileName,PATHINFO_BASENAME).': '.$e->getMessage());

    }

    // 确定要讀取的sheet,什麼是sheet,看excel的右下角,真的不懂去百度吧

    $sheet = $objPHPExcel->getSheet(0);

    $highestRow = $sheet->getHighestRow();

    $highestColumn = $sheet->getHighestColumn();

    // 擷取一行的資料

    for ($row = 1; $row <= $highestRow; $row++){

    // Read a row of data into an array

    $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);

    //這裡得到的rowData都是一行的資料,得到資料後自行處理,我們這裡隻打出來看看效果

   echo '<pre>';

    print_r($rowData);

    }

運作結果:

PHPEXCEL導入EXCEL