phpexcel怎麼讀取excel檔案?phpexcel讀取xls檔案的例子

來源:互聯網
上載者:User
  1. error_reporting(E_ALL);
  2. date_default_timezone_set('Asia/ShangHai');
  3. /** PHPExcel_IOFactory */
  4. require_once '../Classes/PHPExcel/IOFactory.php';
  5. // Check prerequisites
  6. if (!file_exists("31excel5.xls")) {
  7. exit("not found 31excel5.xls.\n");
  8. }
  9. $reader = PHPExcel_IOFactory::createReader('Excel5'); //設定以Excel5格式(Excel97-2003活頁簿)
  10. $PHPExcel = $reader->load("31excel5.xls"); // 載入excel檔案
  11. $sheet = $PHPExcel->getSheet(0); // 讀取第一個工作表
  12. $highestRow = $sheet->getHighestRow(); // 取得總行數
  13. $highestColumm = $sheet->getHighestColumn(); // 取得總列數
  14. $highestColumm= PHPExcel_Cell::columnIndexFromString($colsNum); //字母列轉換為數字列 如:AA變為27
  15. /** 迴圈讀取每個儲存格的資料 */
  16. for ($row = 1; $row <= $highestRow; $row++){//行數是以第1行開始
  17. for ($column = 0; $column < $highestColumm; $column++) {//列數是以第0列開始
  18. $columnName = PHPExcel_Cell::stringFromColumnIndex($column);
  19. echo $columnName.$row.":".$sheet->getCellByColumnAndRow($column, $row)->getValue()."
    ";
  20. }
  21. }
  22. ?>
複製代碼

例2,簡化後的讀取excel檔案的方法

  1. error_reporting(E_ALL);
  2. date_default_timezone_set('Asia/ShangHai');
  3. /** PHPExcel_IOFactory */
  4. require_once '../Classes/PHPExcel/IOFactory.php';
  5. // Check prerequisites
  6. if (!file_exists("31excel5.xls")) {
  7. exit("not found 31excel5.xls.\n");
  8. }
  9. $reader = PHPExcel_IOFactory::createReader('Excel5'); //設定以Excel5格式(Excel97-2003活頁簿)
  10. $PHPExcel = $reader->load("31excel5.xls"); // 載入excel檔案
  11. $sheet = $PHPExcel->getSheet(0); // 讀取第一個工作表
  12. $highestRow = $sheet->getHighestRow(); // 取得總行數
  13. $highestColumm = $sheet->getHighestColumn(); // 取得總列數
  14. /** 迴圈讀取每個儲存格的資料 */
  15. for ($row = 1; $row <= $highestRow; $row++){//行數是以第1行開始
  16. for ($column = 'A'; $column <= $highestColumm; $column++) {//列數是以A列開始
  17. $dataset[] = $sheet->getCell($column.$row)->getValue();
  18. echo $column.$row.":".$sheet->getCell($column.$row)->getValue()."
    ";
  19. }
  20. }
  21. ?>
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.