簡體   English   中英

使用Symfony 4中的PhpOffice \\ PhpSpreadsheet從Excel文件讀取數據

[英]Reading data from an excel file using PhpOffice\PhpSpreadsheet in symfony 4

我使用symfony 4開發了一個應用程序,我需要從excel文件中讀取數據。 我將PhpOffice \\ PhpSpreadsheet集成到我的項目中,但是現在找不到使用它的文檔。 我發現這個網站可以啟動,但是它並不能滿足我的所有需求。 https://phpspreadsheet.readthedocs.io/en/latest/topics/reading-and-writing-to-file/

如果有人有其他曲目或知道如何使用PhpSpreadsheet從excel文件中讀取數據。

public function excelReader(){

    $reader = new Xlsx();
    $reader->setReadDataOnly(TRUE);
    $spreadsheet = $reader->load("test.xlsx");
    $worksheet = $spreadsheet->getActiveSheet();

    $highestRow = $worksheet->getHighestRow(); 
    $highestColumn = $worksheet->getHighestColumn(); 
    $highestColumnIndex = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($highestColumn);
$res = array();
    for($row=1; $row < $highestRow ; $row++){
        for($col = 1; $col <= $highestColumnIndex; $col++){
            $value = $worksheet->getCellByColumnAndRow($col,$row)->getValue();
            array_push($res,$value);
        }
    }


    return $this->render('table/excel.html.twig', [
        'list' => $res,
    ]);

 }

在此代碼中,我嘗試加載,然后讀取放入控制器文件夾中的test.xlsx文件。 我收到以下錯誤:文件“ test.xlsx”不存在。

嘗試創建文件夾{symfony文件夾} / public / uploads /並將test.xlsx放入該文件夾。 然后在您的代碼中更改此行: $spreadsheet = $reader->load("uploads/test.xlsx"); (出於安全原因,文件夾{symfony文件夾} / src / Controller /僅用於代碼。)

在{symfony目錄} / vendor / phpoffice / phpspreadsheet / samples / Reader /下有讀取電子表格的示例。

暫無
暫無

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

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