【问题标题】:PhpSpreadsheet import data from excel to database symfony 3.4PhpSpreadsheet 将数据从 excel 导入数据库 symfony 3.4
【发布时间】:2019-08-10 03:32:36
【问题描述】:

我想知道如何使用 PhpSpreadsheet symfony 3, 我应该使用这个捆绑包:roromix/SpreadsheetBundle, 我可以有一个小例子如何使用它从 exemple_file.xlst 读取行

谢谢

【问题讨论】:

标签: symfony-3.4 phpspreadsheet


【解决方案1】:

我建议直接使用 PHPSpreadsheet 包。

$spreadsheet = PhpSpreadsheet\IOFactory::load('exemple_file.xlst' );
$worksheet = $spreadsheet->getActiveSheet();  // get active worksheet
$rows = []; //empty array of rows
foreach ($worksheet->getRowIterator() AS $row) {
   $cells = $row->getCellIterator();
    $cells->setIterateOnlyExistingCells(FALSE); // iterates through all cells, including empty ones
   $cellData = [];//
   foreach ($cells as $cell) {
      $cellData[] = $cell->getValue();
   }
   $rows[] = $cells; 
}

这将创建包含所有工作表数据的二维数组“行”,然后您可以使用这些数据导入数据库。或者直接在for循环中逐行导入。

【讨论】:

  • 你的意思是我不必用 composer 安装一个包,直接使用这个 PHPSpreadsheet 包我应该在我的控制器中添加什么
  • @ImadAmzil 用 composer (github.com/PHPOffice/PhpSpreadsheet) 安装 PHPspreadsheet,包 roromix/SpreadsheetBundle 只是基于 PhpSpreadsheet,这就是为什么我推荐使用 PHPOffice/PhpSpreadsheet 而不是 roromix/SpreadsheetBundle
猜你喜欢
  • 2018-02-14
  • 2020-12-30
  • 2011-06-24
  • 1970-01-01
  • 2011-10-15
  • 1970-01-01
  • 2016-02-09
  • 2020-02-27
  • 1970-01-01
相关资源
最近更新 更多