草庐IT

php - Laravel-Excel CSV 分隔符不起作用

coder 2024-04-28 原文

我遇到了同样的问题。使用 Laravel 4。这是来 self 的 Controller 的代码:

public function uploadData()
{
        $file = Input::file('data')->getRealPath();

        \Excel::load($file, function($reader) {
                echo "<pre>";
                $reader->setDelimiter('|');
                print_r($reader->get());
        });
}

输出未在管道分隔符上拆分,而是被视为一个长字符串。这是一个示例:

Maatwebsite\Excel\Collections\RowCollection Object
(
    [title:protected] => Worksheet
    [items:protected] => Array
        (
            [0] => Maatwebsite\Excel\Collections\CellCollection Object
                (
                    [title:protected] => 
                    [items:protected] => Array
                        (
                            [upc_numberitem_numberqty_availunit_pricedescriptioncolorqty_on_ponext_avail_dt] => 040176424354|53607-3037|0|14.50|PACK-N-GO DUFFELS 20?"|BIRDS ON A WIRE/LEAF GREEN||
                        )

                )

            [1] => Maatwebsite\Excel\Collections\CellCollection Object
                (
                    [title:protected] => 
                    [items:protected] => Array
                        (
                            [upc_numberitem_numberqty_availunit_pricedescriptioncolorqty_on_ponext_avail_dt] => 040176414850|53607-3054|0|14.50|PACK-N-GO DUFFELS 20?"|BLACK/BLACK/BLACK||
                        )

                )
)

我是不是做错了什么?为什么不在管道字符上解析行?

如果我更改 config/csv.php 中的 delimiter 设置,文件将被正确解析,结果如下所示:

Maatwebsite\Excel\Collections\RowCollection Object
(
    [title:protected] => Worksheet
    [items:protected] => Array
        (
            [0] => Maatwebsite\Excel\Collections\CellCollection Object
                (
                    [title:protected] => 
                    [items:protected] => Array
                        (
                            [upc_number] => 40176424354
                            [item_number] => 53607-3037
                            [qty_avail] => 0
                            [unit_price] => 14.5
                            [description] => PACK-N-GO DUFFELS 20?"
                            [color] => BIRDS ON A WIRE/LEAF GREEN
                            [qty_on_po] => 
                            [next_avail_dt] => 
                        )

                )

            [1] => Maatwebsite\Excel\Collections\CellCollection Object
                (
                    [title:protected] => 
                    [items:protected] => Array
                        (
                            [upc_number] => 40176414850
                            [item_number] => 53607-3054
                            [qty_avail] => 0
                            [unit_price] => 14.5
                            [description] => PACK-N-GO DUFFELS 20?"
                            [color] => BLACK/BLACK/BLACK
                            [qty_on_po] => 
                            [next_avail_dt] => 
                        )

                )

为什么它不能使用 setDelimiter() 方法?

最佳答案

试试这个。

Config::set('excel.csv.delimiter', '|');

阅读更多 https://github.com/Maatwebsite/Laravel-Excel/issues/262

关于php - Laravel-Excel CSV 分隔符不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29324567/

有关php - Laravel-Excel CSV 分隔符不起作用的更多相关文章

  1. ruby-on-rails - 如果 Object::try 被发送到一个 nil 对象,为什么它会起作用? - 2

    如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象

  2. ruby-on-rails - "assigns"在 Ruby on Rails 中有什么作用? - 2

    我目前正在尝试学习RubyonRails和测试框架RSpec。assigns在此RSpec测试中做什么?describe"GETindex"doit"assignsallmymodelas@mymodel"domymodel=Factory(:mymodel)get:indexassigns(:mymodels).shouldeq([mymodel])endend 最佳答案 assigns只是检查您在Controller中设置的实例变量的值。这里检查@mymodels。 关于ruby-o

  3. ruby - 字符串文字前面的 * 在 ruby​​ 中有什么作用? - 2

    这段代码似乎创建了一个范围从a到z的数组,但我不明白*的作用。有人可以解释一下吗?[*"a".."z"] 最佳答案 它叫做splatoperator.SplattinganLvalueAmaximumofonelvaluemaybesplattedinwhichcaseitisassignedanArrayconsistingoftheremainingrvaluesthatlackcorrespondinglvalues.Iftherightmostlvalueissplattedthenitconsumesallrvaluesw

  4. ruby - 为什么这个 eval 在 Ruby 中不起作用 - 2

    你能解释一下吗?我想评估来自两个不同来源的值和计算。一个消息来源为我提供了以下信息(以编程方式):'a=2'第二个来源给了我这个表达式来评估:'a+3'这个有效:a=2eval'a+3'这也有效:eval'a=2;a+3'但我真正需要的是这个,但它不起作用:eval'a=2'eval'a+3'我想了解其中的区别,以及如何使最后一个选项起作用。感谢您的帮助。 最佳答案 您可以创建一个Binding,并将相同的绑定(bind)与每个eval相关联调用:1.9.3p194:008>b=binding=>#1.9.3p194:009>eva

  5. ruby-on-rails - Spring 不起作用。 [未初始化常量 Spring::SID::DL] - 2

    我无法运行Spring。这是错误日志。myid-no-MacBook-Pro:myid$spring/Users/myid/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/spring-0.0.10/lib/spring/sid.rb:17:in`fiddle_func':uninitializedconstantSpring::SID::DL(NameError)from/Users/myid/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/spring-0.0.10/li

  6. ruby-on-rails - 如何以递归方式将 YAML 文件扁平化为 JSON 对象,其中键是点分隔的字符串? - 2

    例如,如果我有YAML文件en:questions:new:'NewQuestion'other:recent:'Recent'old:'Old'这最终会变成一个json对象,例如{'questions.new':'NewQuestion','questions.other.recent':'Recent','questions.other.old':'Old'} 最佳答案 由于问题是关于在Rails应用程序上使用YAML文件进行i18n,因此值得注意i18ngem提供了一个辅助模块I18n::Backend::Flatten完全像

  7. ruby-on-rails - Simple_form 必填字段不起作用 - Ruby on Rails - 2

    我在RoR应用程序中有一个提交表单,是使用simple_form构建的。当字段为空白时,应用程序仍会继续下一步,而不会提示错误或警告。默认情况下,这些字段应该是required:true;但即使手动编写也行不通。该应用有3个步骤:NewPost(新View)->Preview(创建View)->Post。我的Controller和View的摘录会更清楚:defnew@post=Post.newenddefcreate@post=Post.new(params.require(:post).permit(:title,:category_id))ifparams[:previewButt

  8. ruby-on-rails - 这个 C 和 PHP 程序员如何学习 Ruby 和 Rails? - 2

    按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭9年前。我来自C、php和bash背景,很容易学习,因为它们都有相同的C结构,我可以将其与我已经知道的联系起来。然后2年前我学了Python并且学得很好,Python对我来说比Ruby更容易学。然后从去年开始,我一直在尝试学习Ruby,然后是Rails,我承认,直到现在我还是学不会,讽刺的是那些打着简单易学的烙印,但是对于我这样一个老练的程序员来说,我只是无法将它

  9. ruby-on-rails - Heroku Action 缓存似乎不起作用 - 2

    我一直在Heroku上尝试不同的缓存策略,并添加了他们的memcached附加组件,目的是为我的应用程序添加Action缓存。但是,当我在我当前的应用程序上查看Rails.cache.stats时(安装了memcached并使用dalligem),在执行应该缓存的操作后,我得到current和total_items为0。在Controller的顶部,我想缓存我有的Action:caches_action:show此外,我修改了我的环境配置(对于在Heroku上运行的配置)config.cache_store=:dalli_store我是否可以查看其他一些统计数据,看看它是否有效或我做错

  10. ruby-on-rails - Rake 预览在 Octopress 中不起作用 - 2

    我在我的机器上安装了ruby​​版本1.9.3,并且正在为我的个人网站开发一个octopress项目。我为我的gems使用了rvm,并遵循了octopress.org记录的所有步骤。但是我在我的rake服务器中发现了一些错误。这是我的命令日志。Tin-Aung-Linn:octopresstal$ruby--versionruby1.9.3p448(2013-06-27revision41675)[x86_64-darwin12.4.0]Tin-Aung-Linn:octopresstal$rakegenerate##GeneratingSitewithJekyllidenticals

随机推荐