将Excel导入到Rails应用程序中 [英] Import Excel into Rails app

查看:85
本文介绍了将Excel导入到Rails应用程序中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个适合个人使用的小型Rails应用程序,并希望能够上传Excel文件以便稍后进行验证并添加到数据库中。以前我用csv文件工作过,但这已经变得不切实际了。

有谁知道使用roo或电子表格gem上传文件的教程,将内容显示给用户,然后添加到数据库(验证之后)?我知道这是非常具体的,但我想通过这一步一步工作。



到目前为止,我只有一个'导入'视图:

 <%form_for:dump,:url => {:controller =>students,:action =>student_import} ,:html => {:multipart => true} do | f | - %GT; 
选择一个Excel文件:
<%= f.file_field:excel_file - %>
<%= submit_tag'提交' - %>
<%end - %>

但是不知道如何在控制器中访问这个上传的文件。

欢迎任何建议/帮助。
Thanks $ / $>

解决方案


  • https://rubygems.org/gems/spreadsheet
  • https://github.com/zdavatz/spreadsheet/blob/master/GUIDE.md



  • 电子表格库专为读写电子表格文档而设计,从版本0.6.0开始,仅支持Microsoft Excel兼容的电子表格,电子表格是一种组合/完整重写Daniel J. Berger的Spreadsheet :: Excel Library和Hannes Wyss的ParseExcel Library。Spreadsheet可以读取,编写和修改Spreadsheet Documents。



    编辑

    要获取上传的文件,您有两种选择:

    1.(推荐)使用文件上传插件,如回形针 a nd它将处理位和螺栓。

    2.按照以下方式使用IO对象 params [:dump] [:excel_file] http://guides.rails.info/form_helpers.html#what-gets-uploaded


    I am creating a small rails app for personal use and would like to be able to upload excel files to later be validated and added to the database. I had this working previously with csv files, but this has since become impractical.

    Does anyone know of a tutorial for using the roo or spreadsheet gem to upload the file, display the contents to the user and then add to the database (after validating)? I know this is quite specific, but I want to work through this step by step.

    All I have so far is an 'import' view:

    <% form_for :dump, :url=>{:controller=>"students", :action=>"student_import"}, :html => { :multipart => true } do |f| -%>
        Select an Excel File :
        <%= f.file_field :excel_file -%>
        <%= submit_tag 'Submit' -%>
    <% end -%>
    

    But have no idea how to access this uploaded file in the controller.

    Any suggestions/help would be welcomed. Thanks

    解决方案

    "The Spreadsheet Library is designed to read and write Spreadsheet Documents. As of version 0.6.0, only Microsoft Excel compatible spreadsheets are supported. Spreadsheet is a combination/complete rewrite of the Spreadsheet::Excel Library by Daniel J. Berger and the ParseExcel Library by Hannes Wyss. Spreadsheet can read, write and modify Spreadsheet Documents."

    EDIT
    To get the uploaded file you have two options:
    1. (recommended) Use something a file upload plugin like paperclip and it will handle the bits and bolts.
    2. use the IO object params[:dump][:excel_file] as per: http://guides.rails.info/form_helpers.html#what-gets-uploaded

    这篇关于将Excel导入到Rails应用程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆