使用csv库导出到excel

时间:2014-03-17 15:50:02

标签: ruby-on-rails ruby-on-rails-3 csv export-to-excel

我有以下代码导出到excel

在我的控制器上:

def users_report
    @users = Kid.where(confirmation_token: nil).paginate(:page => params[:page], :per_page => 30)
    @userxls = Kid.where(confirmation_token: nil)
    respond_to do |format|
        format.html
        format.xls { send_data @userxls.to_csv({col_sep: "\t"})  }
    end
end

在我的模特身上:

  def self.to_csv(options = {})
        CSV.generate(options) do |csv|
        csv << ["Name", "Surname", "E-mail", "Birthday", "School", "Class", "Native Language", "Practised Language", "Conversations same Native", "Convserations different Native", "Message same Native", "Message different Native", "Posts", "Videos watched", "Clossed/Finished calls", "Missed calls", "Connections per Week", "Nb of foreign friends", "Nb of friends same country", "Activation Date", "Email Parent", "Parent Activated"]
            all.each do |kid|
                kids = Array.new

                kid.name = kid.name rescue "No name"
                kid.surname = kid.surname rescue "No surname"
                kid.email = kid.email rescue "No email"
                kid.birthday = kid.birthday rescue "No age"
                kid.school.name = kid.school.name rescue "No School"
                kid.courses.first.name = kid.courses.first.name rescue "No Course"
                kid.native_languages = kid.native_languages rescue "No native language"
                kid.practice_languages = kid.practice_languages rescue "No practise language"
                kid.number_of_native_conversations = kid.number_of_native_conversations rescue "0"
                kid.number_of_foreign_conversations = kid.number_of_foreign_conversations rescue "0"
                kid.number_of_native_messages = kid.number_of_native_messages rescue "0"
                kid.number_of_foreign_messages = kid.number_of_foreign_messages rescue "0"
                kid.number_of_activity_posts = kid.number_of_activity_posts rescue "0"
                kid.number_of_finished_closed_calls = kid.number_of_finished_closed_calls rescue "0"
                kid.number_of_missed_calls = kid.number_of_missed_calls rescue "0"
                kid.avg_of_connections_week = kid.avg_of_connections_week rescue "0"
                kid.number_of_foreign_friends = kid.number_of_foreign_friends rescue "0"
                kid.number_of_friends_same_country = kid.number_of_friends_same_country rescue "0"
                kid.confirmed_at = kid.confirmed_at.try(:strftime, "%d/%m/%Y")
                kid.tutor.email = kid.tutor.email rescue 'No parent email'
                kid.tutor.confirmed = kid.tutor.confirmed rescue 'No parent email'



                kids << [kid.name, kid.surname, kid.email, kid.birthday, kid.school.name, kid.courses.first.name, kid.native_languages, kid.practice_languages, kid.number_of_native_conversations, kid.number_of_foreign_conversations, kid.number_of_native_messages, kid.number_of_foreign_messages, kid.number_of_activity_posts, kid.number_of_finished_closed_calls, kid.number_of_missed_calls, kid.avg_of_connections_week, kid.number_of_foreign_friends, kid.number_of_friends_same_country, kid.confirmed_at.try(:strftime, "%d/%m/%Y"), kid.tutor.email, kid.tutor.confirmed? ]
                kids.each do |k|
                    csv << k
                end
            end
      end
  end

我知道这段代码很糟糕,但我遇到了以下问题:

1)我可以在我的csv数组中插入选举,因为当我打开文件时,我会得到像#kid&lt; 3248293028&gt;这样的内容,而不是名字,姓氏,电子邮件,bla bla bla,这个我为什么要插入首先在kids数组上然后使用每个数组插入csv数组。

2)在我的模特中,我有几种方法,比如年龄,以获得孩子的年龄:

def age
    now = Time.now.utc.to_date
    (now.year - self.birthday.year - (self.birthday.to_date.change(:year => now.year) > now ? 1 : 0)) rescue (now.year - self.birthday.year)
  end

但是t0_csv方法没有看到这个方法,我和其他方法有同样的问题,如:

sentence_native_languages_of sentence_practise_languages_of number_of_native_conversations number_of_foreign_conversations number_of_native_messages 等等等,所有这些都存在于模型中,但to_csv永远不会看到这种方法。

为什么,请帮助我理解。

提前致谢。


更新


这是我的to_csv方法无法看到的方法之一

def number_of_native_conversations
    na_messages = 0

    KidConversation.all_for(self).each do |conversation|
      na_messages += 1 unless conversation.foreign_conversation_for? self
    end

    return na_messages
  end

0 个答案:

没有答案