簡體   English   中英

在laravel中從Excel文件中讀取單元格

[英]Read cells from Excel file in laravel

我在Laravel 5上使用Laravel Excel v2.1。*

https://github.com/Maatwebsite/Laravel-Excel

Route::get('/', function () {

    $rows = Excel::load('storage\\app\\demo.xlsx')->ignoreEmpty()->skip(1)->get();
  echo "<pre>";
        print_r($rows->toArray());
        echo "</pre>";
  });

如果我的excel文件有第一行作為標題然后值,那么它的工作正常。

在此輸入圖像描述

但我有excel文件,其中excel文件的第一行有合並的單元格。所以有任何選項來逐個單元格讀取

但我的excel文件第一行有

在此輸入圖像描述

您必須以不同方式讀取此文件,不能使用簡單的導入方法。 您必須按每個坐標讀取它,本機PHPExcel方法的示例:

http://docs.typo3.org/typo3cms/extensions/phpexcel_library/1.7.4/manual.html#_Toc237519900

也許這可以幫到你。 這來自Reiah Paul Sam所寫的鏈接。 這是一個讀取excel文件的所有單元格的循環。

$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load('storage\\app\\demo.xlsx');
$objWorksheet = $objPHPExcel->getActiveSheet();
echo '<table>' . "\n";
foreach ($objWorksheet->getRowIterator() as $row) {
    echo '<tr>' . "\n";

    $cellIterator = $row->getCellIterator();

    $cellIterator->setIterateOnlyExistingCells(false); // This loops all cells,
    // even if it is not set.
    // By default, only cells
    // that are set will be
    // iterated.
    foreach ($cellIterator as $cell) {

        echo '<td>' . $cell->getValue() . '</td>' . "\n";

    }

     echo '</tr>' . "\n";

}

echo '</table>' . "\n";

請注意,您可能需要在php.ini中啟用zip擴展名

我希望這有幫助。 最好的祝福。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM