【问题标题】:Laravel 4 Excel import failsLaravel 4 Excel 导入失败
【发布时间】:2015-07-15 03:18:19
【问题描述】:

我正在尝试导入 excel 文件,但它总是给我这个错误:

PHPExcel_Exception
Row 2 is out of range (2 - 1)

我正在使用 Laravel 4,这是我的代码:

public function postExcel()
{
    $file = Input::file('file');

    $destinationPath = public_path() .'/uploads/temp/';
    $filename   = str_replace(' ', '_', $file->getClientOriginalName());
    $file->move($destinationPath, $filename);

    $result = Excel::selectSheets('Sheet1')->load($destinationPath)->get();

    echo "<pre>";
    var_dump($result->toArray());
    exit;

}

这是我的虚拟 excel 文件:

我试过用谷歌搜索这个,但对于其他人来说,它似乎只在工作表多于一张时才会发生,但在我的情况下并非如此。

【问题讨论】:

  • 哪一行导致错误?
  • 另外,您似乎是load()ing public/uploads/temp 文件夹,而不是单个文件。
  • $result = Excel::selectSheets('Sheet1')-&gt;load($destinationPath)-&gt;get(); 行...当我将其更改为:$result = Excel::selectSheetsByIndex(0)-&gt;load($destinationPath, function($reader) { $reader-&gt;noHeading(); })-&gt;get(); 它没有给出错误但它返回空数组...是 excel 文件问题吗?
  • 我不熟悉这个库,但在 load() 文件之前 selectSheets() 似乎很奇怪,我猜你应该先做 load($destinationPath . $filename)。跨度>
  • 哦,我的错!对不起,我忘了添加文件名,现在可以了!非常感谢!

标签: php laravel-4 import-from-excel maatwebsite-excel


【解决方案1】:

您需要load($destinationPath . $filename),而不是load($destinationPath) - 该目录不是 Excel 文件。 :-)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-27
    • 1970-01-01
    • 2019-03-10
    • 1970-01-01
    • 2021-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多